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
Alistair Smith
784271f85e
SQLite in Bun.sql ( #21640 )
...
### What does this PR do?
Support sqlite in the Bun.sql API
Fixes #18951
Fixes #19701
### How did you verify your code works?
tests
---------
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com >
2025-08-19 23:15:53 -07:00
taylor.fish
41b1efe12c
Rename disabled parameter in Output.scoped ( #21769 )
...
It's very confusing.
(For internal tracking: fixes STAB-977)
2025-08-11 20:19:34 -07:00
Alistair Smith
04883a8bdc
revert fe28e00d53.
...
This reverts commit fe28e00d53 .
2025-08-05 16:10:29 -07:00
Alistair Smith
fe28e00d53
feat: add Bun.SQL API with initial SQLite support
2025-08-05 16:04:11 -07:00
Jarred Sumner
a5af485354
Refactor h2_frame_parser to use GC-visited fields ( #21573 )
...
### What does this PR do?
Instead of holding a strong for the options object passed with the
handlers, we make each of the callbacks kept alive by the handlers and
it detaches once the detachFromJS function is called.
This should fix #21570 , which looks like it was caused by wrapper
functions for AsyncLocalStorage getting collected prematurely.
fixes #21254
fixes #21553
fixes #21422
### How did you verify your code works?
Ran test/js/node/http2/node-http2.test.js
2025-08-02 20:38:49 -07:00
pfg
7c4c360431
Make getIfPropertyValueExistsImpl accept a slice ( #21554 )
...
Previously it accepted `property: anytype` but now it's `[]const u8`
because that was the only allowed value, so it makes it easier to see
what type it accepts in autocomplete.
Also updates the doc comment, switches it to use ZIG_EXPORT, and updates
the cppbind doc comment
2025-08-01 19:26:55 -07:00
pfg
0cf2b71ff1
expect.toHaveReturnedWith/toHaveLastReturnedWith/toHaveNthReturnedWith ( #21363 )
...
Fixes #10380
DRAFT: not reviewed
---------
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-08-01 15:09:03 -07:00
pfg
40bff9fea8
Support functions in cppbind ( #21439 )
...
Fixes #21434 : Makes sure 'bun install' is executed before running
2025-08-01 15:07:51 -07:00
taylor.fish
07cd45deae
Refactor Zig imports and file structure (part 1) ( #21270 )
...
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-07-22 17:51:38 -07:00
pfg
60faa8696f
Auto cpp->zig bindings ( #20881 )
...
Co-authored-by: pfgithub <6010774+pfgithub@users.noreply.github.com >
Co-authored-by: Ben Grant <ben@bun.sh >
Co-authored-by: Claude <noreply@anthropic.com >
2025-07-21 16:26:07 -07:00
pfg
83760fc446
Sort imports in all files ( #21119 )
...
Co-authored-by: taylor.fish <contact@taylor.fish >
2025-07-21 13:26:47 -07:00
Meghan Denny
875604a42b
safety: a lot more exception checker progress ( #20956 )
2025-07-16 00:11:19 -07:00
Jarred Sumner
d8b37bf408
Shrink MimeType list ( #21004 )
...
Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com >
Co-authored-by: Meghan Denny <meghan@bun.sh >
2025-07-15 22:37:09 -07:00
Meghan Denny
6c5b863530
safety: a lot more exception checker progress ( #20817 )
2025-07-10 15:34:51 -07:00
Jarred Sumner
55a9cccac0
bun.sh -> bun.com ( #20909 )
2025-07-10 00:10:43 -07:00
Jarred Sumner
454316ffc3
Implement "node:module"'s findSourceMap and SourceMap class ( #20863 )
...
Co-authored-by: Claude <claude@anthropic.ai >
Co-authored-by: Claude <noreply@anthropic.com >
Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com >
2025-07-07 23:08:12 -07:00
Jarred Sumner
e1957228f3
[internal] Add error when .classes.ts files are invalid
2025-07-06 21:07:38 -07:00
Jarred Sumner
1d48f91b5e
Enable ReadableStream as stdin for Bun.spawn ( #20582 )
...
Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com >
Co-authored-by: Cursor Agent <cursoragent@cursor.com >
Co-authored-by: jarred <jarred@bun.sh >
Co-authored-by: pfg <pfg@pfg.pw >
2025-06-27 19:42:03 -07:00
190n
346e97dde2
fix bugs found by exception scope verification ( #20285 )
...
Co-authored-by: 190n <7763597+190n@users.noreply.github.com >
2025-06-18 23:08:19 -07:00
Meghan Denny
809992229f
node:net rework ( #18962 )
...
Co-authored-by: nektro <5464072+nektro@users.noreply.github.com >
2025-05-28 17:04:37 -07:00
Jarred Sumner
69be630aea
WebKit Upgrade ( #19839 )
...
Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com >
Co-authored-by: Ben Grant <ben@bun.sh >
Co-authored-by: 190n <7763597+190n@users.noreply.github.com >
2025-05-22 21:12:43 -07:00
Jarred Sumner
14b439a115
Fix formatters not running in CI + delete unnecessary files ( #19433 )
2025-05-08 23:22:16 -07:00
pfg
00a3cbd977
more child-process ( #18688 )
...
Co-authored-by: pfgithub <6010774+pfgithub@users.noreply.github.com >
2025-05-06 22:12:24 -07:00
Dylan Conway
a0819e9d02
fix more node:timers tests ( #19432 )
...
Co-authored-by: 190n <ben@bun.sh >
2025-05-02 20:50:02 -07:00
chloe caruso
32c1dcb70d
dev server: unref source maps ( #19371 )
...
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com >
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
2025-05-02 04:13:54 -07:00
Meghan Denny
9e201eff9e
node:net: implement BlockList ( #19277 )
2025-05-01 16:09:44 -07:00
Meghan Denny
2a2247bbb6
js: fix serialization of non-transferable objects ( #19351 )
2025-04-29 18:23:26 -07:00
chloe caruso
26e296a7e8
devserver: fix cases where source maps would not remap ( #19289 )
2025-04-28 17:56:35 -07:00
Jarred Sumner
93ff4d97da
Add assertion about returning pointers in constructors ( #19332 )
2025-04-28 02:12:59 -07:00
Dylan Conway
465379d96a
add Timeout.prototype.close, _idleTimeout and _onTimeout ( #19318 )
2025-04-28 00:25:25 -07:00
Ciro Spaciari
59b2a60790
compat(node:http) more passing ( #19236 )
...
Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com >
Co-authored-by: 190n <ben@bun.sh >
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com >
2025-04-24 19:26:55 -07:00
chloe caruso
3349c995b5
no usingnamespace, organize jsc namespace, enable -fincremental ( #19122 )
...
Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com >
2025-04-22 16:34:15 -07:00
Jarred Sumner
032713c58c
Fix several lints ( #19121 )
2025-04-19 05:41:34 -07:00
chloe caruso
7d7512076b
remove more usingnamespace ( #19042 )
2025-04-17 19:04:05 -07:00
chloe caruso
4ec410e0d7
internal: make @import("bun") work in zig ( #19096 )
2025-04-17 12:32:47 -07:00
chloe caruso
be77711a4e
delete usingnamespace in bindings generator ( #19020 )
2025-04-15 12:14:47 -07:00
chloe caruso
c1dc5f1b73
remove some usingnamespaces ( #18765 )
2025-04-10 14:16:30 -07:00
Don Isaac
44f252539a
fix: mark JSPromise.rejectedPromiseValue as deprecated ( #18549 )
...
### What does this PR do?
`JSPromise.rejectedPromiseValue` does not notify the VM about the promise it creates, meaning unhandled rejections created this way do not trigger `unhandledRejection`. This is leading to accidental error suppression in (likely) a lot of places. Additionally it returns a `JSValue` when really it should be returning a `*JSPromise`, making Zig bindings more type-safe.
This PR renames `rejectedPromiseValue` to `dangerouslyCreateRejectedPromiseValueWithoutNotifyingVM` and marks it as deprecated. It does _not_ modify code calling this function, meaning no behavior changes should occur. We should slowly start replacing its usages with `rejectedPromise`
## Changelog
- Rename `rejectedPromiseValue` to `dangerouslyCreateRejectedPromiseValueWithoutNotifyingVM`
- Mark `JSPromise.asValue` as deprecated. It takes a `*JSGlobalObject` but never uses it. New code should use `toJS()`
- Refactors `blob` to make null checks over `destination_blob.source` a release assertion
- `ErrorBuilder.reject` uses `rejectedPromiseValue` when 1.3 feature flag is enabled
2025-04-09 13:27:51 -07:00
chloe caruso
9a329c04cc
pass test-module-globalpaths-nodepath.js ( #18879 )
2025-04-08 21:32:19 -07:00
Jarred Sumner
ec87a27d87
Introduce Bun.redis - a builtin Redis client for Bun ( #18812 )
2025-04-08 03:34:00 -07:00
Meghan Denny
2bbdf4f950
codegen: fix this ModuleLoader enum ( #18769 )
2025-04-03 19:49:06 -07:00
Meghan Denny
f505cf6f66
js: $isPromise* fixes ( #18763 )
2025-04-03 18:42:25 -07:00
chloe caruso
c29933f823
implement require.extensions attempt 2 ( #18686 )
2025-04-01 14:31:16 -07:00
Jarred Sumner
4806e84cc1
Revert "remove many usingnamespace, introduce new ref count and ref leak debugging tools ( #18353 )"
...
This reverts commit a199b85f2b . It does not compile on Windows.
2025-04-01 08:35:51 -07:00
chloe caruso
a199b85f2b
remove many usingnamespace, introduce new ref count and ref leak debugging tools ( #18353 )
2025-03-31 17:17:38 -07:00
Jarred Sumner
f38d35f7c9
Revert #18562 #18478 ( #18610 )
2025-03-28 20:23:49 -07:00
chloe caruso
70ddfb55e6
implement require.extensions ( #18478 )
2025-03-27 14:58:24 -07:00
Don Isaac
75144ab881
fix: reflect-metadata import order ( #18086 )
...
Co-authored-by: DonIsaac <22823424+DonIsaac@users.noreply.github.com >
2025-03-26 16:19:45 -07:00
Jarred Sumner
8e246e1e67
Add precompiled header ( #18321 )
2025-03-20 19:27:46 -07:00