Skip to content

Commit 4de8df3

Browse files
Documentation: Add debugging and profiling articles
1 parent 94762ae commit 4de8df3

File tree

9 files changed

+153
-1
lines changed

9 files changed

+153
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Debugging
2+
3+
Debug your Swift applications running on JavaScript environment using DWARF-based debugging tools, including Chrome DevTools extensions and command-line debuggers.
4+
5+
## Overview
6+
7+
These debugging tools are DWARF-based, so you need to build your application with DWARF sections enabled.
8+
9+
Build your application with debug configuration using:
10+
11+
```bash
12+
swift package --swift-sdk $SWIFT_SDK_ID js -c debug
13+
```
14+
15+
Alternatively, you can omit the `-c debug` flag since the `js` plugin builds with debug configuration by default:
16+
17+
```bash
18+
swift package --swift-sdk $SWIFT_SDK_ID js
19+
```
20+
21+
## Chrome DevTools
22+
23+
When debugging a web browser application, Chrome DevTools provides a powerful debugging environment. It allows you to set breakpoints and step through your Swift source code at the source level.
24+
25+
There are two DWARF extensions available for Chrome DevTools: an enhanced extension specifically designed for Swift, and the official C/C++ extension. For Swift development, the enhanced extension is recommended.
26+
27+
> Important: The two extensions cannot coexist. You must use only one extension at a time.
28+
29+
### Enhanced DWARF Extension for Swift
30+
31+
For the best Swift debugging experience, use the enhanced DWARF extension that provides:
32+
33+
- Breakpoint setting and Swift code inspection
34+
- Human-readable call stack frames
35+
- Swift variable value inspection
36+
37+
![Chrome DevTools with Swift support](chrome-devtools-swift.png)
38+
39+
To install this enhanced extension:
40+
41+
1. If you have the official "C/C++ DevTools Support (DWARF)" extension installed, uninstall it first
42+
2. Download the extension ZIP file from [GitHub Releases](https://github.com/GoodNotes/devtools-frontend/releases/tag/swift-0.2.3.0)
43+
3. Go to `chrome://extensions/` and enable "Developer mode"
44+
4. Drag and drop the downloaded ZIP file into the page
45+
46+
When you close and reopen the DevTools window, DevTools will suggest reloading itself to apply settings.
47+
48+
> Note: There is a known issue where some JavaScriptKit types like `JSObject` and `JSValue` are not shown in pretty format in the variables view.
49+
50+
### Official DWARF Extension
51+
52+
Alternatively, you can use the official [`C/C++ DevTools Support (DWARF)`](https://goo.gle/wasm-debugging-extension) extension. However, it has limitations for Swift development:
53+
54+
- Function names in the stack trace are mangled (you can demangle them using `swift demangle` command)
55+
- Variable inspection is unavailable since Swift depends on its own mechanisms instead of DWARF's structure type feature
56+
57+
![Chrome DevTools](chrome-devtools.png)
58+
59+
See [the DevTools team's official introduction](https://developer.chrome.com/blog/wasm-debugging-2020) for more details about the extension.

Sources/JavaScriptKit/Documentation.docc/Articles/Deploying-Pages.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Once you've built your application with JavaScriptKit, you'll need to deploy it
1111
Build your application using [Vite](https://vite.dev/) build tool:
1212

1313
```bash
14-
# Build the Swift package for WebAssembly
14+
# Build the Swift package for WebAssembly with release configuration
1515
$ swift package --swift-sdk wasm32-unknown-wasi js -c release
1616

1717
# Create a minimal HTML file (if you don't have one)

Sources/JavaScriptKit/Documentation.docc/Articles/JavaScript-Environment-Requirements.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# JavaScript Environment Requirements
22

3+
Understand the JavaScript features required by JavaScriptKit and ensure your target environment supports them.
4+
35
## Required JavaScript Features
46

57
The JavaScript package produced by the JavaScriptKit packaging plugin requires the following JavaScript features:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Profiling Memory Usage
2+
3+
Profile memory usage of your Swift applications running on JavaScript environment to identify memory leaks and understand memory allocation patterns.
4+
5+
## Overview
6+
7+
Memory profiling helps you understand how your application uses memory, identify memory leaks, and optimize memory allocation patterns. This guide covers tools and techniques for profiling memory usage in Swift applications running on JavaScript environment.
8+
9+
When profiling memory usage, start with JavaScript execution environment's heap profiler. If `WebAssembly.Memory` objects are the bottleneck, use specialized WebAssembly memory profilers that understand the Wasm instance's memory allocator.
10+
11+
## Chrome DevTools Heap Snapshots
12+
13+
Start by using Chrome DevTools' built-in heap profiler to understand overall memory usage in your application. See [Chrome DevTools documentation on heap snapshots](https://developer.chrome.com/docs/devtools/memory-problems/heap-snapshots) for detailed instructions.
14+
15+
## wasm-memprof
16+
17+
If `WebAssembly.Memory` objects are the bottleneck, JavaScript engine's default profiler typically doesn't track allocations within WebAssembly's memory space, so you won't get good insights. In such cases, use a profiler like [wasm-memprof](https://github.com/kateinoigakukun/wasm-memprof) that understands the Wasm instance's memory allocator.
18+
19+
wasm-memprof is a heap profiler specifically designed for WebAssembly applications. It provides detailed memory allocation tracking with Swift symbol demangling support, helping you identify memory leaks and understand memory usage patterns within WebAssembly's linear memory.
20+
21+
![wasm-memprof visualization](https://raw.githubusercontent.com/kateinoigakukun/wasm-memprof/main/docs/demo.png)
22+
23+
### Setup
24+
25+
Add wasm-memprof to your application before instantiating your Wasm program:
26+
27+
```javascript
28+
import { WMProf } from "wasm-memprof";
29+
import { SwiftDemangler } from "wasm-memprof/plugins/swift-demangler.js";
30+
31+
const swiftDemangler = SwiftDemangler.create();
32+
globalThis.WebAssembly = WMProf.wrap(globalThis.WebAssembly, {
33+
demangler: swiftDemangler.demangle.bind(swiftDemangler),
34+
});
35+
```
36+
37+
Check the [wasm-memprof repository](https://github.com/kateinoigakukun/wasm-memprof) for detailed documentation and examples.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Profiling Performance Issues
2+
3+
Profile your Swift applications running on JavaScript environment to identify performance bottlenecks using Chrome DevTools and other profiling tools.
4+
5+
## Overview
6+
7+
Profiling helps you understand where your application spends time. This guide covers tools and techniques for profiling Swift applications running on JavaScript environment, focusing on performance profiling. For memory profiling, see <doc:Profiling-Memory-Usage>.
8+
9+
## Chrome DevTools Performance Tab
10+
11+
Chrome DevTools provides a built-in Performance profiler that can help you identify performance bottlenecks in your WebAssembly application. See [Chrome DevTools documentation on performance profiling](https://developer.chrome.com/docs/devtools/performance) for detailed instructions.
12+
13+
![Chrome DevTools Performance Tab](chrome-devtools-perf.png)
14+
15+
> Note: WebAssembly function names may appear mangled in the performance timeline. You can use `swift demangle` to decode them, or use the enhanced DWARF extension mentioned in <doc:Debugging> for better symbol resolution.
16+
17+
### V8 Compilation Pipeline Considerations
18+
19+
When DevTools is open, V8's WebAssembly compilation pipeline behaves differently:
20+
21+
- **With DevTools open**: V8 replaces optimized TurboFan code with unoptimized Liftoff code for debugging purposes. This means performance measurements taken while DevTools is open will not reflect actual production performance.
22+
23+
- **With Performance tab recording**: When you click the "Record" button in the Performance tab, code gets recompiled with TurboFan again for profiling. However, the initial tier-down to Liftoff when DevTools opens can still affect measurements.
24+
25+
> Important: Do not measure FPS or performance metrics while DevTools is open, as the results will not be representative of actual production performance. Close DevTools before taking performance measurements, or use the Performance tab's recording feature which recompiles code appropriately for profiling.
26+
27+
See [V8's WebAssembly compilation pipeline documentation](https://v8.dev/docs/wasm-compilation-pipeline#debugging) for more details.
28+
29+
## Performance Best Practices
30+
31+
### Build Configuration
32+
33+
For profiling, use a release build with debug information. You have two options:
34+
35+
**DWARF mode** (recommended for easier symbol resolution):
36+
37+
```bash
38+
swift package --swift-sdk $SWIFT_SDK_ID js -c release --debug-info-format dwarf
39+
```
40+
41+
DWARF mode preserves DWARF structures, which may disable some wasm-opt optimizations. This can result in performance characteristics that differ from actual release builds. However, the DevTools extension can automatically demangle function names, making results easier to read.
42+
43+
**Name section mode** (recommended for accurate performance characteristics):
44+
45+
```bash
46+
swift package --swift-sdk $SWIFT_SDK_ID js -c release --debug-info-format name
47+
```
48+
49+
Name section mode uses WebAssembly's [name section](https://webassembly.github.io/spec/core/appendix/custom.html#name-section) and applies the same optimizations as regular release builds, providing more accurate performance characteristics. However, you'll need to manually demangle function names using `swift demangle` when analyzing results.
50+
51+
> Note: The limitation of name section mode requiring manual demangling may be alleviated in the future through improvements to the DevTools extension.
246 KB
Loading
382 KB
Loading
903 KB
Loading

Sources/JavaScriptKit/Documentation.docc/Documentation.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ Check out the [examples](https://github.com/swiftwasm/JavaScriptKit/tree/main/Ex
5353

5454
- <doc:Deploying-Pages>
5555
- <doc:JavaScript-Environment-Requirements>
56+
- <doc:Debugging>
57+
- <doc:Profiling-Performance-Issues>
58+
- <doc:Profiling-Memory-Usage>
5659

5760
### BridgeJS
5861

0 commit comments

Comments
 (0)