mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
perf: use jsonStringifyFast for faster JSON serialization (#25733)
## Summary Apply the same optimization technique from PR #25717 (Response.json) to other APIs that use JSON.stringify internally: - **IPC message serialization** (`ipc.zig`) - used for inter-process communication - **console.log with %j format** (`ConsoleObject.zig`) - commonly used for debugging - **PostgreSQL JSON/JSONB types** (`PostgresRequest.zig`) - database operations - **MySQL JSON type** (`MySQLTypes.zig`) - database operations - **Jest %j/%o format specifiers** (`jest.zig`) - test output formatting - **Transpiler tsconfig/macros** (`JSTranspiler.zig`) - build configuration ### Root Cause When calling `JSONStringify(globalObject, value, 0)`, the space parameter `0` becomes `jsNumber(0)`, which is NOT `undefined`. This causes JSC's FastStringifier (SIMD-optimized) to bail out: ```cpp // In WebKit's JSONObject.cpp FastStringifier::stringify() if (!space.isUndefined()) { logOutcome("space"_s); return { }; // Bail out to slow path } ``` Using `jsonStringifyFast` which passes `jsUndefined()` triggers the fast path. ### Expected Performance Improvement Based on PR #25717 results, these changes should provide ~3x speedup for JSON serialization in the affected APIs. ## Test plan - [x] Debug build compiles successfully - [x] Basic functionality verified (IPC, console.log %j, Response.json) - [x] Existing tests pass 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -101,7 +101,8 @@ pub fn writeBind(
|
||||
.jsonb, .json => {
|
||||
var str = bun.String.empty;
|
||||
defer str.deref();
|
||||
try value.jsonStringify(globalObject, 0, &str);
|
||||
// Use jsonStringifyFast for SIMD-optimized serialization
|
||||
try value.jsonStringifyFast(globalObject, &str);
|
||||
const slice = str.toUTF8WithoutRef(bun.default_allocator);
|
||||
defer slice.deinit();
|
||||
const l = try writer.length();
|
||||
|
||||
Reference in New Issue
Block a user