Skip to content

Benchmarks, and 2–40× faster mutation, queries and JSON parsing - #8

Merged
mackoj merged 9 commits into
mainfrom
benchmarks
Nov 13, 2026
Merged

mackoj merged 9 commits into
mainfrom
benchmarks

Conversation

@mackoj

@mackoj mackoj commented Nov 13, 2026

Copy link
Copy Markdown
Owner

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:

  • JSON parsing and printing;
  • path parsing in both Dialects;
  • queries on a generated 1.4 MB store and on cts.json;
  • JSON Pointer, mutations, and Codable.

It also has a p50 comparison tool and scripts/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)

Benchmark Before After Mallocs
Replace every $..price (5,000) 419 ms 10.0 ms 115 K → 100 K
Remove filtered books 111 ms 2.4 ms 23 K → 13 K
RFC 9535, all 8 expressions 117 ms 15.3 ms 863 K → 83 K
Jayway, all 8 expressions 66.7 ms 9.3 ms 377 K → 19 K
RFC 9535 $..author 9.9 ms 1.3 ms 95 K → 17
Jayway $..author 4.2 ms 0.83 ms 30 K → 16
RFC 9535 filter 11.1 ms 2.3 ms 110 K → 13 K
Every node of cts.json 2.0 ms 0.39 ms 20 K → 17
Parse JSON, 1.4 MB 26.4 ms 7.9 ms 310 K → 120 K
Parse cts.json 2.9 ms 0.9 ms 34 K → 14 K
Print JSON 15.3 ms 9.8 ms
Encode books 35.7 ms 30.6 ms
nodes(in:) with normalized paths 1.0 ms 0.87 ms unchanged

Unchanged: path parsing, JSON Pointer resolution, Codable decoding. Peak memory is unchanged too, since the documents themselves dominate it.

What was slow

  1. Mutation was O(n²). update bound 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 where JSONValue: ExpressibleByNilLiteral turns nil into .null (cond ? x : nil). Both are fixed and commented.
  2. Normalized paths were built for every visited node, including under values(in:) and in filter subqueries, which discard them. They are now built only for nodes(in:).
  3. The JSON parser built strings one scalar at a time. A string with no escapes is now decoded once from its bytes, and integers are read without making a String.
  4. The printer appended one scalar at a time. Only ASCII is ever escaped, so the runs between escapes are copied whole.
  5. OrderedDictionary hashed every key while iterating. Keys and values are now parallel arrays, indexed only past 8 members; most JSON objects are small. == and hash stay order-insensitive.
  6. NormalizedPath.appending allocated twice; now once.
  7. Regexes were compiled once per node. Each evaluation now owns a RegexCache. It holds no global state, so the library stays pure.

Safety

  • The public API is unchanged: diagnose-api-breaking-changes main reports none.
  • New tests cover what each change could break: value semantics when mutating a copy, escapes next to multi-byte text (parsing and both printers), invalid UTF-8 repair, Int.min, OrderedDictionary across the 8-member boundary, and regex reuse with different flags or under match/search. Each new test also passes on the old code.
  • No Sextant port or compliance case was changed. A Sextant test calls JaywayValue.evaluate directly, so its new regexes: parameter has a default.

Checks

  • swift build -c release, swift test (368 tests, up from 362)
  • swift-format 6.3 lint (including Benchmarks), yamllint, git diff --check main
  • Linux (Swift 6.0–6.2) and DocC: left to CI

Not in this PR:

  • Codable decoding (25 ms, 245 K mallocs): it would need its own rewrite of the decoder containers.
  • A benchmark CI job: it needs jemalloc, and CI timings are noisy. swift-arazzo doesn't have one either.

@mackoj mackoj mentioned this pull request Nov 13, 2026
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
Six benchmarks from Benchmarks/ (#8) on the generated 1.4 MB document:
parsing, the 8 RFC 9535 and Jayway queries, a descendant scan, and
replace and remove. The numbers are #8's p50s, so this lands after it.
@mackoj
mackoj merged commit 1889e65 into main Nov 13, 2026
16 checks passed
@mackoj
mackoj deleted the benchmarks branch November 13, 2026 23:37
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant