|
| 1 | +--- |
| 2 | +layout: default-layout |
| 3 | +title: About Adaptive WebAssembly (Wasm) Loading |
| 4 | +keywords: Dynamsoft Barcode Reader, FAQ, JavaScript, tech basic, wasm, loading |
| 5 | +description: How to enable -SIMD-Pthread Wasm for accelerated deep learning computation? |
| 6 | +needAutoGenerateSidebar: false |
| 7 | +--- |
| 8 | + |
| 9 | +# About Adaptive WebAssembly (Wasm) Loading |
| 10 | + |
| 11 | +## What is adaptive Wasm loading? |
| 12 | + |
| 13 | +Dynamsoft Barcode Reader(JavaScript) includes **three optimized WebAssembly (Wasm) variants** — *-Baseline*, *-Pthread*, and *-SIMD-Pthread* — which can be **dynamically loaded based on the runtime environment**. |
| 14 | +This adaptive loading mechanism ensures the SDK automatically selects the **most compatible and highest-performing** Wasm module available in each browser, further improving performance in modern environments. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Comparison of the three Wasm variants |
| 19 | + |
| 20 | +| Feature | Baseline Wasm | SIMD Wasm | SIMD + Pthread Wasm | |
| 21 | +| -------- | -------------- | ---------- | -------------------- | |
| 22 | +| **Parallelism** | Single-threaded | Single Instruction Multiple Data (CPU vector instruction set) | Multi-threaded(via Web Workers + SharedArrayBuffer) + SIMD | |
| 23 | +| **Performance Characteristics** | Simple, limited by single-core performance | Leverages CPU vectorized parallelism to speed up data processing | Combines SIMD vectorization and multi-core acceleration for maximum performance | |
| 24 | +| **Compatibility** | Supported in all Wasm environments | Requires browser support for Wasm SIMD instruction set | Requires browser support for both Wasm SIMD and Wasm threads (cross-origin isolation) | |
| 25 | +| **Minimum Supported Browser Versions** | Chrome 78+<br>Edge 79+<br>Safari 14.5+<br>Firefox 68+ | Chrome 91+<br>Edge 91+<br>Safari 16.4+<br>Firefox 89+ | Chrome 91+<br>Edge 91+<br>Safari 16.4+<br>Firefox 89+ | |
| 26 | +| **Wasm Size** | 5588 KB | 6974 KB | 8225 KB | |
| 27 | +| **Transfer Size** | 2.11 MB | 2.55 MB | 2.81 MB | |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## How to enable -SIMD-Pthread Wasm for accelerated deep learning computation? |
| 32 | + |
| 33 | +To unlock multi-threaded performance with the **-SIMD-Pthread Wasm** variant, configure your server to enable **cross-origin isolation** by adding the following HTTP headers to all responses: |
| 34 | + |
| 35 | +```text |
| 36 | +Cross-Origin-Opener-Policy: same-origin |
| 37 | +Cross-Origin-Embedder-Policy: require-corp |
| 38 | +``` |
| 39 | + |
| 40 | +Then, ensure that all SDK resources are **served under the same origin**. |
| 41 | + |
| 42 | +Once correctly configured, the SDK will automatically detect the environment and load the `-SIMD-Pthread Wasm` to leverage multi-core acceleration. |
| 43 | + |
| 44 | +>[!TIP] |
| 45 | +>You can verify whether your site is correctly isolated by checking the browser console for cross-origin isolation status or by calling window.crossOriginIsolated in DevTools. |
| 46 | +
|
| 47 | +## How to manually specify which Wasm variant to load? |
| 48 | + |
| 49 | +By default, the SDK automatically determines the most suitable WebAssembly (Wasm) variant to load based on the browser’s capabilities. |
| 50 | +However, developers can **manually override** this behavior and explicitly specify which Wasm module to load through the `wasmLoadOptions` property. |
| 51 | + |
| 52 | +### Example |
| 53 | + |
| 54 | +```javascript |
| 55 | +Dynamsoft.Core.CoreModule.wasmLoadOptions = { |
| 56 | + wasmType: "ml-simd-pthread", |
| 57 | + pthreadPoolSize: 5, |
| 58 | +}; |
| 59 | +``` |
| 60 | + |
| 61 | +### Supported Wasm Types |
| 62 | + |
| 63 | +```javascript |
| 64 | +type WasmType = |
| 65 | + | "baseline" // Basic single-threaded variant |
| 66 | + | "ml-simd" // SIMD-optimized variant |
| 67 | + | "ml-simd-pthread" // Multi-threaded + SIMD optimized variant |
| 68 | + | "auto"; // Automatically select based on environment |
| 69 | +``` |
| 70 | + |
| 71 | +>[!NOTE] |
| 72 | +>Setting wasmType to "auto" (default) allows the SDK to automatically choose the optimal Wasm based on runtime capability detection. |
| 73 | +> |
| 74 | +>When using "ml-simd-pthread", ensure that cross-origin isolation is properly configured as described in [How to enable `-SIMD-Pthread Wasm` for accelerated deep learning computation](#how-to-enable--simd-pthread-wasm-for-accelerated-deep-learning-computation). |
| 75 | +> |
| 76 | +>If the specified Wasm variant is not supported in the current browser, the SDK will gracefully fall back to a compatible variant. |
| 77 | +
|
| 78 | +## Why isn’t -SIMD-Pthread Wasm enabled by default on iOS? |
| 79 | + |
| 80 | +Due to iOS’s strict memory allocation and management limitations, loading `-SIMD-Pthread Wasm` can sometimes lead to “out of memory” errors on older devices or iOS versions. |
| 81 | +To ensure stability and compatibility, the SDK does not load `-SIMD-Pthread Wasm` by default on iOS. Instead, it automatically falls back to the most suitable Baseline or SIMD variant depending on the environment. |
0 commit comments