robobun
e7672b2d04
Add string fast path for postMessage and structuredClone ( #21926 )
...
## Summary
Implements a string fast path optimization for `postMessage` and
`structuredClone` operations that provides significant performance
improvements for string-only data transfer, along with various bug fixes
and infrastructure improvements.
## Key Performance Improvements
**postMessage with Workers:**
- **Small strings (11 chars):** ~5% faster (572ns vs 599ns)
- **Medium strings (14KB):** **~2.7x faster** (528ns vs 1.40μs)
- **Large strings (3MB):** **~660x faster** (540ns vs 356μs)
**Compared to Node.js postMessage:**
- Similar performance for small strings
- Competitive for medium strings
- **~455x faster** for large strings (540ns vs 245μs)
## Implementation Details
The optimization adds a **string fast path** that bypasses full
structured cloning serialization when:
- Input is a pure string (`value.isString()`)
- No transfer list or message ports are involved
- Not being stored persistently
### Core Changes
**String Thread-Safety Utilities (`BunString.cpp/h`):**
- `isCrossThreadShareable()` - Checks if string can be safely shared
across threads
- `toCrossThreadShareable()` - Converts strings to thread-safe form via
`isolatedCopy()`
- Handles edge cases: atoms, symbols, substring slices, external buffers
**Serialization Fast Path (`SerializedScriptValue.cpp`):**
- New `m_fastPathString` field stores string data directly
- Bypasses full object serialization machinery for pure strings
- Creates isolated copies for cross-thread safety
**Deserialization Fast Path:**
- Directly returns JSString from stored string data
- Avoids parsing serialized byte streams
**Updated Flags System (`JSValue.zig`, `Serialization.cpp`):**
- Replaces boolean `forTransfer` with structured `SerializedFlags`
- Supports `forCrossProcessTransfer` and `forStorage` distinctions
**Structured Clone Infrastructure:**
- Moved `structuredClone` implementation to dedicated
`StructuredClone.cpp`
- Added `jsFunctionStructuredCloneAdvanced` for testing with custom
flags
- Improved class serialization compatibility checks (`isForTransfer`,
`isForStorage`)
**IPC Improvements (`ipc.zig`):**
- Fixed race conditions in `SendQueue` by deferring cleanup to next tick
- Proper fd ownership handling with `bun.take()`
- Cached IPC serialize/parse functions for better performance
**BlockList Thread Safety Fixes (`BlockList.zig`):**
- Fixed potential deadlocks by moving mutex locking inside methods
- Added atomic `estimated_size` counter to avoid lock during GC
- Corrected pointer handling in comparison functions
- Improved GC safety in `rules()` method
## Benchmark Results
```
❯ bun-21926 bench/string-postmessage.mjs # This branch
postMessage(11 chars string) 572.24 ns/iter
postMessage(14 KB string) 527.55 ns/iter ← ~2.7x faster
postMessage(3 MB string) 539.70 ns/iter ← ~660x faster
❯ bun-1.2.20 bench/string-postmessage.mjs # Previous
postMessage(11 chars string) 598.76 ns/iter
postMessage(14 KB string) 1.40 µs/iter
postMessage(3 MB string) 356.38 µs/iter
❯ node bench/string-postmessage.mjs # Node.js comparison
postMessage(11 chars string) 569.63 ns/iter
postMessage(14 KB string) 1.46 µs/iter
postMessage(3 MB string) 245.46 µs/iter
```
**Key insight:** The fast path achieves **constant time performance**
regardless of string size (~540ns), while traditional serialization
scales linearly with data size.
## Test Coverage
**New Tests:**
- `test/js/web/structured-clone-fastpath.test.ts` - Fast path memory
usage validation
- `test/js/web/workers/structuredClone-classes.test.ts` - Comprehensive
class serialization tests
- Tests ArrayBuffer transferability
- Tests BunFile cloning with storage/transfer restrictions
- Tests net.BlockList cloning behavior
- Validates different serialization contexts (default, worker, window)
**Enhanced Tests:**
- `test/js/web/workers/structured-clone.test.ts` - Multi-function
testing
- Tests `structuredClone`, `jscSerializeRoundtrip`, and cross-process
serialization
- Validates consistency across different serialization paths
- `test/js/node/cluster.test.ts` - Better error handling and debugging
**Benchmarks:**
- `bench/string-postmessage.mjs` - Worker postMessage performance
comparison
- `bench/string-fastpath.mjs` - Fast path vs traditional serialization
comparison
## Bug Fixes
**BlockList Threading Issues:**
- Fixed potential deadlocks when multiple threads access BlockList
simultaneously
- Moved mutex locks inside methods rather than holding across entire
function calls
- Added atomic size tracking for GC compatibility
- Fixed comparison function pointer handling
**IPC Race Conditions:**
- Fixed race condition where `SendQueue._onAfterIPCClosed()` could be
called on wrong thread
- Deferred cleanup operations to next tick using task queue
- Improved file descriptor ownership with proper `bun.take()` usage
**Structured Clone Compatibility:**
- Enhanced class serialization with proper transfer/storage mode
checking
- Fixed edge cases where non-transferable objects were incorrectly
handled
- Added better error reporting for unsupported clone operations
## Technical Notes
- Thread safety ensured via `String.isolatedCopy()` for cross-VM
transfers
- Memory cost calculation updated to account for string references
- Maintains full compatibility with existing structured clone semantics
- Does not affect object serialization or transfer lists
- Proper cleanup and error handling throughout IPC pipeline
---------
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com >
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Meghan Denny <meghan@bun.sh >
2025-08-20 00:25:00 -07:00
Jarred Sumner
cd6785771e
run prettier and add back format action ( #13722 )
2024-09-03 21:32:52 -07:00
Meghan Denny
996847bcad
ci: disable bun-jsc.test.ts 'profile async' on windows for now ( #13363 )
2024-08-17 02:50:48 -07:00
Dylan Conway
5bd344281f
fix(TextEncoder): domjit crash in encode ( #13320 )
...
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com >
2024-08-15 03:35:58 -07:00
Dylan Conway
bfca627dfa
fix node:vm and DOMJIT ( #13185 )
2024-08-09 00:33:49 -07:00
Jarred Sumner
92bd629e60
Support promises in profile method in bun:jsc ( #12165 )
2024-07-06 20:24:21 -07:00
dave caruso
688ddbda74
feat(bundler): implement enum inlining / more constant folding ( #12144 )
...
Co-authored-by: paperdave <paperdave@users.noreply.github.com >
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com >
Co-authored-by: Jarred-Sumner <Jarred-Sumner@users.noreply.github.com >
2024-07-03 04:23:17 -07:00
Ashcon Partovi
d105b048b1
Use Buildkite for CI ( #11477 )
2024-06-27 14:56:07 -07:00
dave caruso
072f2f15ea
ci: run windows tests and also run them concurrently ( #7758 )
2024-01-12 17:02:20 -08:00
Dylan Conway
af23dab833
Create domjit.test.ts ( #4173 )
2023-08-15 22:13:13 -07:00
Jarred Sumner
99de971359
Fix incorrect name
2023-07-17 04:21:00 -07:00
Jarred Sumner
edeb3b48e8
Fix test
2023-07-16 22:46:06 -07:00
Jarred Sumner
209dc981c0
Implement Workers ( #3645 )
...
* copy files
* format
* options
* Introduce `Worker`, `onmessage`, `onerror`, and `postMessage` globals
* Stub `Worker.prototype.ref` & `Worker.prototype.unref`
* Update web_worker.zig
* Worker works
* Add "mini" mode
* add wakeup
* Partially fix the keep-alive issue
* clean up refer behavior
* Implement `serialize` & `deserialize` in `bun:jsc` & add polyfill for `node:v8`
* Types & docs
* Update globals.d.ts
* Add mutex
* Fixes
---------
Co-authored-by: Dylan Conway <dylan.conway567@gmail.com >
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com >
2023-07-16 21:15:24 -07:00
Jarred Sumner
6074d4e378
workaround
2023-05-23 18:07:23 -07:00
Jarred Sumner
5b38c55c3d
Support setting a timezone with process.env.TZ and Bun.env.TZ ( #3018 )
...
* Support setting a timezone via `process.env.TZ`
* Implement `setTimeZone` in `bun:jsc` module
* [breaking] `bun:test` now defaults to `Etc/UTC` timezone
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com >
2023-05-23 00:40:12 -07:00
Alex Lam S.L
47865fe82a
fix gc-related flaky test failures ( #2402 )
2023-03-15 18:51:22 -07:00
Ashcon Partovi
f7e4eb8369
Reorganize tests ( #2332 )
2023-03-07 12:22:34 -08:00