Repository navigation
Rewrite the JSON parser around a SIMD structural index #33032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
79 commits
Select commit
Hold shift + click to select a range
9c8ef48
json: rewrite the parser around a SIMD structural index
Jarred-Sumner 917a702
json: fix unterminated containers, optimize the kernel TU in debug bu…
Jarred-Sumner 565f2ed
json: write the structural index into spare capacity; registry manife…
Jarred-Sumner bf70e96
json: make the scalar indexer bit-identical to the SIMD kernel
Jarred-Sumner b48fb56
json: index large documents through a resumable kernel in 1MB chunks
Jarred-Sumner a09ed0b
json: keep the duplicate-key spill map per object
Jarred-Sumner ad8c72f
json: give each nesting level of large objects its own duplicate-key map
Jarred-Sumner 35bc891
[autofix.ci] apply automated fixes
autofix-ci[bot] 54f070e
ast: compact JSON-only object/array nodes (E::ObjectSimple, E::ArrayS…
Jarred-Sumner 4fa6d66
json: store simple-container children as arena slices
Jarred-Sumner 9a49340
json: simple-container children live on one per-document row tape
Jarred-Sumner 6ae2748
json: cut simple-mode stage 2 per-element overhead
Jarred-Sumner d19559a
highway: chunked JSON index wrapper takes the initialized dirty bitmap
Jarred-Sumner 920dbdb
json: appease clippy (is_multiple_of, raw-pointer borrow, Copy IndexE…
Jarred-Sumner a9b36cb
ast: teach the generic Expr accessors about the JSON simple containers
Jarred-Sumner 0f23ecd
install: parse npm registry manifests with the simple JSON AST
Jarred-Sumner 3c5f026
json: document why tsconfig/package.json stay on the full AST; add wa…
Jarred-Sumner 6b35f73
json: materialize simple trees at the boundary; recover value locatio…
Jarred-Sumner e8fc438
json: the simple AST's row tape owns its memory; no arena
Jarred-Sumner 3e10b3b
json: every UTF-8 entry point parses the simple AST; classic trees ar…
Jarred-Sumner a663ee0
json: delete the classic half of stage 2
Jarred-Sumner 363ec60
json: decode escapes straight to WTF-8, and convert WTF-8 to JS strin…
Jarred-Sumner 5393062
json: read-only consumers consume the row AST directly
Jarred-Sumner 6212c7f
json: name the immutable AST's nodes EObjectJSON/EArrayJSON; let the …
Jarred-Sumner 03b5d1b
json: fix the issues review surfaced; record value locations in the t…
Jarred-Sumner da7dfd7
json: move the generic JSON helpers out of the resolver
Jarred-Sumner 2f43770
json: address the remaining review feedback
Jarred-Sumner 8437bdd
json: fix the criterion bench, allowlist ceilings, and the remaining …
Jarred-Sumner ccb26b3
json: name the row-AST entry points after their dialect; skip duplica…
Jarred-Sumner d267c46
[autofix.ci] apply automated fixes
autofix-ci[bot] 573c6a3
json: delete the dead and duplicated code the rewrite left behind
Jarred-Sumner 95a11a4
[autofix.ci] apply automated fixes
autofix-ci[bot] 2030d1a
json: fail the parse when stage 2 logged an error; add JSON conforman…
Jarred-Sumner 904ca8b
json: review fixes for the conformance tests and create's parse-error…
Jarred-Sumner 4f5781e
json: fix the doc references and dead code the bots flagged
Jarred-Sumner cb17d8c
[autofix.ci] apply automated fixes
autofix-ci[bot] 9ee0074
json: generate the byte-classification table for both indexers; fix t…
Jarred-Sumner eb91348
[autofix.ci] apply automated fixes
autofix-ci[bot] ae84654
json: don't emit unused items into the scalar indexer; drop the write…
Jarred-Sumner 58914fb
json: always rebuild the bench kernel TU (it includes a generated hea…
Jarred-Sumner 68672a5
[autofix.ci] apply automated fixes
autofix-ci[bot] a5e492c
json: bound the gap scan by the previous token; match the true commen…
Jarred-Sumner 3da2046
json: never classify non-ASCII bytes as structural; drop dead ArrayIt…
Jarred-Sumner 3de9841
json: align the loc-recovery line-comment scan with the indexer; doc …
Jarred-Sumner 4529f03
json: auto-quoted values keep raw control characters; simplify the ne…
Jarred-Sumner 4fe1a4e
[autofix.ci] apply automated fixes
autofix-ci[bot] 3171f3e
json: drop a test comment describing the deleted backward comment-ope…
Jarred-Sumner f70de24
json: step over comments that follow exotic whitespace with no gap
Jarred-Sumner 7d7708f
json: two stale comments (simple AST naming; the bundler's materializ…
Jarred-Sumner 15d27fa
json: delete the dead \u{...} validation; fix a stale doc reference
Jarred-Sumner 2482d7c
json: drop a misleading qualifier from skip_unicode_ws's docs
Jarred-Sumner f38148d
json: a minus may be separated from its digits by exotic whitespace o…
Jarred-Sumner 810e11b
json: fix a stale bench comment naming callers that moved to the row API
Jarred-Sumner edd029e
json: report 'Expected number' at the token after the minus; doc accu…
Jarred-Sumner 561da47
json: a separated minus is one value to the source rescanners too
Jarred-Sumner 9093ea8
json: treat comments as whitespace in every dialect inside stage 2; p…
Jarred-Sumner 6507e1d
[autofix.ci] apply automated fixes
autofix-ci[bot] 4369851
json: delete the dead UTF-16 hash branch; test the minus EOF arm; doc…
Jarred-Sumner 245df94
json: reword a misleading test comment
Jarred-Sumner b704369
json: escaped keywords after exotic whitespace; test comment accuracy
Jarred-Sumner 1e8ffca
[autofix.ci] apply automated fixes
autofix-ci[bot] a786ad6
json: fix a stale comment on the re-dispatched scalar arms
Jarred-Sumner 2102cf5
json: guard the materializer's recursion like the printer's
Jarred-Sumner 98c4b9b
json: log a diagnostic at every depth-limit site
Jarred-Sumner f658c86
[autofix.ci] apply automated fixes
autofix-ci[bot] 37a199a
json: delete all but the essential comments
Jarred-Sumner bb8208f
json: length-prefix nested containers in the row hasher, like the top…
Jarred-Sumner 0a6a8ef
Merge remote-tracking branch 'origin/main' into worktree-bridge-cse_0…
Jarred-Sumner 9d41a17
[autofix.ci] apply automated fixes
autofix-ci[bot] a6c7e3e
json: delete remaining non-essential comments
Jarred-Sumner e072778
[autofix.ci] apply automated fixes
autofix-ci[bot] 818f120
json: route a whitespace-separated minus to the number parser in env …
Jarred-Sumner 72c9ae5
json: report the earlier of a stage-2 error and an index error
Jarred-Sumner 4db2504
json: keep pre-comment warnings; skip block comments when guessing in…
Jarred-Sumner c3c5087
[autofix.ci] apply automated fixes
autofix-ci[bot] fe58dea
json: anchor the unterminated-comment error's precedence at the /*
Jarred-Sumner 096c1ff
[autofix.ci] apply automated fixes
autofix-ci[bot] 1d4ce4c
json: a rejected comment before an index error wins; warnings survive…
Jarred-Sumner 32440a9
json: drop the unused warnings snapshot; skip line comments in the in…
Jarred-Sumner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| *.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #!/usr/bin/env bash | ||
| # Fetch the JSON benchmark fixture corpus: real package.json files and real | ||
| # npm registry API responses (both abbreviated install-v1 manifests, which is | ||
| # what `bun install` fetches, and full packuments). | ||
| set -euo pipefail | ||
| cd "$(dirname "$0")" | ||
|
|
||
| ACCEPT_ABBREV='Accept: application/vnd.npm.install-v1+json' | ||
|
|
||
| # package.json files (pretty-printed, human-authored shape) | ||
| pkg() { curl -fsSL "https://unpkg.com/$1@$2/package.json" -o "pkgjson-$1.json"; echo "pkgjson-$1.json"; } | ||
| pkg express 4.19.2 | ||
| pkg react 18.3.1 | ||
| pkg typescript 5.5.4 | ||
| pkg webpack 5.93.0 | ||
| pkg eslint 9.8.0 | ||
| pkg axios 1.7.3 | ||
| pkg next 14.2.5 | ||
| pkg vite 5.3.5 | ||
|
|
||
| # Abbreviated registry manifests (what `bun install` actually parses, minified) | ||
| abbrev() { curl -fsSL -H "$ACCEPT_ABBREV" "https://registry.npmjs.org/$1" -o "manifest-abbrev-$2.json"; echo "manifest-abbrev-$2.json"; } | ||
| abbrev is-number is-number | ||
| abbrev express express | ||
| abbrev react react | ||
| abbrev lodash lodash | ||
| abbrev typescript typescript | ||
| abbrev next next | ||
| abbrev @types/node types-node | ||
| abbrev @babel/core babel-core | ||
|
|
||
| # Full packuments (largest realistic payloads; `bun audit` / `bun pm view` shape) | ||
| full() { curl -fsSL "https://registry.npmjs.org/$1" -o "packument-full-$2.json"; echo "packument-full-$2.json"; } | ||
| full express express | ||
| full axios axios | ||
|
|
||
| ls -la | ||
|
|
||
| # huge real-world manifests bun install parses constantly | ||
| abbrev drizzle-orm drizzle-orm | ||
| abbrev drizzle-kit drizzle-kit | ||
|
claude[bot] marked this conversation as resolved.
Resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // End-to-end benchmark of Bun's JSON parser through its JS-visible entry | ||
| // point, `Bun.JSONC.parse` (src/parsers/json.rs -> Expr -> JSValue), over the | ||
| // corpus in this directory, with `JSON.parse` (JavaScriptCore) as a reference. | ||
| // | ||
| // bun bench/json-corpus/jsonc-parse.mjs # all fixtures | ||
| // bun bench/json-corpus/jsonc-parse.mjs drizzle # filter by name | ||
| // | ||
| // `Bun.JSONC.parse` does strictly more work than `JSON.parse` here: it parses | ||
| // to Bun's AST and then converts that AST to JS values, so it measures the | ||
| // whole pipeline a `.json`/`.jsonc`/tsconfig import or `bun pm` command sees. | ||
| import { readdirSync, readFileSync } from "node:fs"; | ||
| import { join, dirname } from "node:path"; | ||
|
|
||
| const dir = dirname(Bun.fileURLToPath(import.meta.url)); | ||
| const filter = process.argv[2] ?? ""; | ||
| const files = readdirSync(dir) | ||
| .filter(f => f.endsWith(".json") && f.includes(filter)) | ||
| .sort(); | ||
|
|
||
| const runs = +(process.env.RUNS ?? 30); | ||
| function bench(label, fn) { | ||
| fn(); // warmup | ||
| fn(); | ||
| const times = []; | ||
| for (let i = 0; i < runs; i++) { | ||
| const t0 = Bun.nanoseconds(); | ||
| fn(); | ||
| times.push(Bun.nanoseconds() - t0); | ||
| } | ||
| times.sort((a, b) => a - b); | ||
| return { label, best: times[0], median: times[(times.length / 2) | 0] }; | ||
| } | ||
|
|
||
| console.log("bun", Bun.version, Bun.revision.slice(0, 9)); | ||
| console.log( | ||
| "file".padEnd(40), | ||
| "bytes".padStart(10), | ||
| "JSONC med".padStart(12), | ||
| "JSON.parse".padStart(12), | ||
| "JSONC MB/s".padStart(11), | ||
| ); | ||
| for (const f of files) { | ||
| const text = readFileSync(join(dir, f), "utf8"); | ||
| const bytes = Buffer.byteLength(text); | ||
| const jsonc = bench("jsonc", () => Bun.JSONC.parse(text)); | ||
| const jsonp = bench("json", () => JSON.parse(text)); | ||
| const mbs = bytes / (jsonc.median / 1e3) / 1.048576; // MiB/s | ||
| console.log( | ||
| f.padEnd(40), | ||
| String(bytes).padStart(10), | ||
| (jsonc.median / 1e6).toFixed(3).padStart(9) + " ms", | ||
| (jsonp.median / 1e6).toFixed(3).padStart(9) + " ms", | ||
| mbs.toFixed(0).padStart(11), | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #!/usr/bin/env bash | ||
| # Build + run the JSON parser criterion bench (src/parsers/benches/json_parse.rs). | ||
| # | ||
| # A cargo bench binary links the bun Rust crates but not the CMake-built C++ | ||
| # side, so this script compiles the few native pieces the parser actually | ||
| # reaches — mimalloc, simdutf (+ Bun's simdutf__* wrapper), the highway | ||
| # runtime, and the highway JSON structural-index kernel — from the repo's own | ||
| # vendored sources into one small archive, then points RUSTFLAGS at it. | ||
| # Everything else (StackCheck bound, OOM handler, the one legacy lexer highway | ||
| # call) is shimmed inside the bench file itself. | ||
| # | ||
| # Requires `vendor/` and `build/debug/codegen` to exist (run `bun bd` once). | ||
| # | ||
| # scripts/bench-json-rust.sh # whole corpus | ||
| # scripts/bench-json-rust.sh -- 'drizzle' # criterion filter | ||
| set -euo pipefail | ||
| cd "$(dirname "$0")/.." | ||
|
|
||
| if [ ! -d vendor/mimalloc ] || [ ! -d vendor/highway ]; then | ||
| echo "error: vendor/ not populated — run \`bun bd\` (or \`bun run build\`) once first" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| SUP=target/bench-json-cdeps | ||
| mkdir -p "$SUP/wtf" | ||
| CC=${CC:-cc} | ||
| CXX=${CXX:-c++} | ||
| # Pinned simdutf amalgamation for Bun's simdutf__* C wrapper (the real build | ||
| # gets simdutf from WebKit's WTF; the version difference is irrelevant here). | ||
| SIMDUTF_VERSION=v7.3.6 | ||
| if [ ! -f "$SUP/simdutf.cpp" ]; then | ||
| curl -fsSL -o "$SUP/simdutf.cpp" "https://github.com/simdutf/simdutf/releases/download/$SIMDUTF_VERSION/simdutf.cpp" | ||
| curl -fsSL -o "$SUP/simdutf.h" "https://github.com/simdutf/simdutf/releases/download/$SIMDUTF_VERSION/simdutf.h" | ||
| fi | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| printf '#pragma once\n#include "simdutf.h"\n' > "$SUP/wtf/SIMDUTF.h" | ||
|
|
||
| build() { # build <out.o> <cmd...> | ||
| local out=$1 | ||
| shift | ||
| if [ ! -f "$out" ] || [ "$1" = "-f" ]; then "$@" -o "$out"; fi | ||
| } | ||
| MI_FLAGS=(-O2 -fPIC -ftls-model=initial-exec -DNDEBUG -D_GNU_SOURCE -DMI_STATIC_LIB | ||
| -DMI_SKIP_COLLECT_ON_EXIT=1 -DMI_DEFAULT_ALLOW_THP=0 -DMI_NO_SET_VMA_NAME=1) | ||
| build "$SUP/mimalloc.o" $CC "${MI_FLAGS[@]}" -Ivendor/mimalloc/include -c vendor/mimalloc/src/static.c | ||
| build "$SUP/simdutf.o" $CXX -O3 -fPIC -std=c++20 -I"$SUP" -c "$SUP/simdutf.cpp" | ||
| build "$SUP/simdutf_shim.o" $CXX -O3 -fPIC -std=c++20 -I"$SUP" -c src/parsers/benches/support/simdutf_shim.cpp | ||
| for f in abort targets per_target print timer nanobenchmark aligned_allocator; do | ||
| build "$SUP/hwy_$f.o" $CXX -O3 -fPIC -std=c++17 -Ivendor/highway -c "vendor/highway/hwy/$f.cc" | ||
| done | ||
| if [ -f src/jsc/bindings/highway_json.cpp ]; then | ||
| # Always rebuild the kernel under iteration; it's one small TU. | ||
| $CXX -O3 -fPIC -std=c++17 -Ivendor/highway -Isrc/jsc/bindings -c src/jsc/bindings/highway_json.cpp -o "$SUP/highway_json.o" | ||
| fi | ||
|
claude[bot] marked this conversation as resolved.
Resolved
|
||
| rm -f "$SUP/libbun_bench_cdeps.a" | ||
| ar rcs "$SUP/libbun_bench_cdeps.a" "$SUP"/*.o | ||
| ranlib "$SUP/libbun_bench_cdeps.a" | ||
|
|
||
| # A criterion iteration is much slower than a real `bun install` manifest | ||
| # parse, so mimalloc's default 10ms purge delay returns the arena pages to | ||
| # the OS between iterations and the bench turns into a page-fault storm bun | ||
| # never sees in production. Keep pages resident like a busy install process. | ||
| export MIMALLOC_PURGE_DELAY=${MIMALLOC_PURGE_DELAY:-2000} | ||
| export BUN_CODEGEN_DIR=${BUN_CODEGEN_DIR:-$PWD/build/debug/codegen} | ||
| export BUN_JSON_BENCH_FIXTURES=${BUN_JSON_BENCH_FIXTURES:-$PWD/bench/json-corpus} | ||
| export RUSTFLAGS="${RUSTFLAGS:-} -Clink-arg=$PWD/$SUP/libbun_bench_cdeps.a -Clink-arg=-lstdc++ -Clink-arg=-lm -Clink-arg=-ldl -Clink-arg=-lpthread -Clink-arg=-lc" | ||
|
|
||
| # `--test`: run the crate's unit tests (they need the same native archive). | ||
| if [ "${1:-}" = "--test" ]; then | ||
| shift | ||
| exec cargo test -p bun_parsers --lib --release "$@" | ||
| fi | ||
| exec cargo bench -p bun_parsers --bench json_parse "$@" | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.