Repository navigation
Benchmarks, and 2–40× faster mutation, queries and JSON parsing - #8
Merged
Merged
Conversation
3 tasks done
Benchmarks/ is a separate package, as in swift-arazzo, so the library never depends on package-benchmark or Foundation. 25 benchmarks: JSON parsing and printing, path parsing in both Dialects, queries on a generated 1.4 MB store and on the compliance suite, JSON Pointer, mutations and Codable. Tools: p50 compares two markdown runs, and scripts/profile.sh runs Time Profiler on one benchmark and prints its hottest functions.
update(_:_:_:) bound each container while the value still held it, so copy-on-write copied the whole container on every write: replacing all 5,000 prices copied the 5,000-book array 5,000 times. Each container is now moved out before it changes and put back after, also on a throw. Replace every price, large: 412 ms -> 12.3 ms (34x) Remove filtered books, large: 112 ms -> 2.6 ms (43x) A test pins value semantics: mutating a copy leaves the original as is.
Every visited node built its NormalizedPath (an array copy), including under values(in:), firstValue(in:), matches(_:) and every filter and function subquery, which all discard it. The evaluators now start from a nil location unless nodes(in:) asks for locations, and nil propagates. Query RFC 9535, descendants, large: 9.9 ms -> 2.1 ms, 95 K -> 15 mallocs Query Jayway, descendants, large: 4.2 ms -> 1.8 ms, 30 K -> 14 mallocs Query RFC 9535, filter, large: 11.1 ms -> 3.1 ms Query RFC 9535, every node, compliance suite: 2.0 ms -> 0.45 ms Query RFC 9535, every expression, large: 117 ms -> 74 ms nodes(in:) with normalized paths: unchanged
A string without escapes is decoded once from its bytes; with escapes, the unescaped UTF-8 is gathered in a buffer and decoded once, instead of appending each scalar to a String.UnicodeScalarView. Integers are read from the bytes with overflow checks instead of through a String; a fraction or exponent still goes through Double(String), which rounds correctly. Parse JSON, large (1.4 MB): 26.4 ms -> 9.1 ms, 310 K -> 115 K mallocs Parse JSON, compliance suite: 2.9 ms -> 1.0 ms, 34 K -> 13 K mallocs New tests: escapes next to multibyte text, escaped keys, invalid UTF-8 repaired to U+FFFD as before, Int.min and the first integer past it.
Only ASCII bytes are escaped, so writeString copies the runs between them whole, multibyte UTF-8 included, instead of appending each scalar. The pretty printer writes keys and leaves into its output directly instead of building a jsonString for each. Print JSON, large: 15.3 ms -> 10.2 ms Print JSON pretty and sorted, large: 17.1 ms -> 16.4 ms (sorting keys dominates) New test: every escape next to multibyte text, compact and pretty.
…arly The values lived in a Dictionary beside the ordered keys, so every iteration step and the values property hashed the key again. Keys and values are now two parallel arrays. Positions are indexed in a dictionary only past 8 members; below that a linear search costs no more than hashing and saves the allocation, and most JSON objects are small. Equality and hashing stay order-insensitive. Against the previous commit: Query Jayway, descendants, large: 1.76 ms -> 0.84 ms Query RFC 9535, descendants, large: 2.11 ms -> 1.30 ms Query RFC 9535, filter, large: 3.12 ms -> 2.34 ms Parse JSON, large: 9.2 ms -> 7.7 ms Encode books, large: 34.9 ms -> 30.4 ms New tests: order, lookups and removal across the 8-member boundary, order-insensitive equality and hashing at both sizes.
elements + [element] allocated the one-element array and the result. Query RFC 9535, nodes with normalized paths, large: 1.06 ms -> 0.76 ms, 15 K -> 10 K mallocs
A filter calls match, search or =~ once per node, and every call translated the pattern and built a Regex again: compiling costs far more than matching. Each evaluation now owns a RegexCache, keyed by dialect, flags and pattern, and passes it to the function helpers. It lives as long as one values(in:) or nodes(in:) call: nothing is global or shared between threads. Query RFC 9535, every expression, large: 70.8 ms -> 15.0 ms, 363 K -> 83 K mallocs Query Jayway, every expression, large: 49.7 ms -> 9.4 ms, 239 K -> 19 K mallocs New test: one query reusing a pattern with and without flags, and under match and search, keeps each use's semantics.
README, "Benchmarks": what they measure, how to run them, compare a change against main and profile one. AGENTS.md: a change made for speed or memory states its before and after. Benchmarks/ is in the format and lint commands of AGENTS.md and CONSTRAINTS.md.
mackoj
added a commit
that referenced
this pull request
Nov 13, 2026
The install snippet named 0.0.4. It now names 0.1.0, the release that ships #8's speed-ups, and the landing page's benchmark table compares 0.0.4 with 0.1.0.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds package-benchmark benchmarks (Swift.org intro) and fixes what they found. Each commit is one change, and its message gives its own before and after.
Benchmarks
Benchmarks/is a separate package, laid out like swift-arazzo's, so the library never depends on package-benchmark or Foundation. It has 25 benchmarks:cts.json;It also has a
p50comparison tool andscripts/profile.sh, which runs Time Profiler on one benchmark and prints its hottest functions. The README "Benchmarks" section, an AGENTS.md rule (performance PRs state their before and after) and the format commands are updated.Results (p50, release, M-series Mac, against
main)$..price(5,000)$..author$..authorcts.jsoncts.jsonnodes(in:)with normalized pathsUnchanged: path parsing, JSON Pointer resolution, Codable decoding. Peak memory is unchanged too, since the documents themselves dominate it.
What was slow
updatebound each container while the enum still held it, so copy-on-write copied the whole array on every write. Containers are now moved out before they change and put back after, also on a throw. Fixing this turned up two places whereJSONValue: ExpressibleByNilLiteralturnsnilinto.null(cond ? x : nil). Both are fixed and commented.values(in:)and in filter subqueries, which discard them. They are now built only fornodes(in:).String.OrderedDictionaryhashed every key while iterating. Keys and values are now parallel arrays, indexed only past 8 members; most JSON objects are small.==andhashstay order-insensitive.NormalizedPath.appendingallocated twice; now once.RegexCache. It holds no global state, so the library stays pure.Safety
diagnose-api-breaking-changes mainreports none.Int.min,OrderedDictionaryacross the 8-member boundary, and regex reuse with different flags or undermatch/search. Each new test also passes on the old code.JaywayValue.evaluatedirectly, so its newregexes:parameter has a default.Checks
swift build -c release,swift test(368 tests, up from 362)Benchmarks), yamllint,git diff --check mainNot in this PR: