Skip to content

Commit 99d10a6

Browse files
committed
update doc
1 parent 84e8dc7 commit 99d10a6

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"sideEffects": false,
2626
"scripts": {
2727
"build": "npm publish --dry-run",
28-
"prepare": "npm run clean && webpack --bail && tsgo --build tsconfig.dist.cjs.json tsconfig.dist.esm.json && tsimp tools/fix-ext.mts --mjs dist.esm/*.js dist.esm/*/*.js dist.esm/*.d.ts dist.esm/*/*.d.ts && tsimp tools/fix-ext.mts --cjs dist.cjs/*.js dist.cjs/*/*.js dist.cjs/*.d.ts dist.cjs/*/*.d.ts",
28+
"prepare": "npm run clean && ./wasm/build.sh && webpack --bail && tsgo --build tsconfig.dist.cjs.json tsconfig.dist.esm.json && tsimp tools/fix-ext.mts --mjs dist.esm/*.js dist.esm/*/*.js dist.esm/*.d.ts dist.esm/*/*.d.ts && tsimp tools/fix-ext.mts --cjs dist.cjs/*.js dist.cjs/*/*.js dist.cjs/*.d.ts dist.cjs/*/*.d.ts",
2929
"prepublishOnly": "npm run test:dist",
3030
"clean": "rimraf build dist dist.*",
3131
"test": "mocha 'test/**/*.test.ts'",

wasm/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,38 @@ Three-tier dispatch based on string/byte length:
5151
| 51-1000 | WASM | Optimal for medium strings |
5252
| > 1000 | TextEncoder/TextDecoder | SIMD-optimized for bulk |
5353

54+
## Optimization Attempts (2025)
55+
56+
Several optimization approaches were tested for `utf8Count`:
57+
58+
### 1. Bulk Array Copy (intoCharCodeArray)
59+
60+
**Hypothesis**: Replace N `charCodeAt` calls with 1 bulk `intoCharCodeArray` + N array reads.
61+
62+
**Result**: 17-29% slower. GC array allocation overhead outweighs boundary-crossing savings.
63+
64+
### 2. codePointAt Instead of charCodeAt
65+
66+
**Hypothesis**: Simplify surrogate pair handling with `codePointAt`.
67+
68+
**Result**: Slightly slower. `codePointAt` does more internal work to decode surrogates.
69+
70+
### 3. SIMD Processing
71+
72+
**Hypothesis**: Copy to linear memory, then use SIMD to process 8 chars at once.
73+
74+
**Result**: 23-49% slower. The O(n) copy from GC array to linear memory negates SIMD gains.
75+
76+
```
77+
JS String → GC Array (1 call) → Linear Memory (N scalar ops) → SIMD
78+
79+
This kills SIMD
80+
```
81+
82+
### Conclusion
83+
84+
The scalar `charCodeAt` loop is already near-optimal. The `js-string-builtins` implementation is highly optimized, making per-character calls very cheap. The 2-3x speedup over pure JS is about as good as it gets with current WASM capabilities.
85+
5486
## References
5587

5688
- [js-string-builtins proposal](https://github.com/WebAssembly/js-string-builtins)

0 commit comments

Comments
 (0)