Skip to content

Scan static JSONPaths without pest - #1729

Open
fcostaoliveira wants to merge 2 commits into
RedisJSON:masterfrom
fcostaoliveira:perf/jsonpath-static-path-scanner
Open

fcostaoliveira wants to merge 2 commits into
RedisJSON:masterfrom
fcostaoliveira:perf/jsonpath-static-path-scanner

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

What

json_path::compile now tries a small hand-written scanner before pest. It accepts $ followed by any chain of .key, [n], .[n], ['key'] and ["key"]. On success the query root is QueryRoot::Static(Vec<StaticStep>), and PathCalculator::calc_static evaluates it with direct get_key / get_index lookups.

  • Entry points: every lib.rs entry point dispatches on QueryRoot. That includes calc_once, calc_once_with_paths, visit_once_with_paths, calc_once_paths and the new exists. pop_last, pop_last_object_key, is_static and size have static arms.
  • Fallback: a rejected path allocates nothing and takes the existing pest path, so behaviour and error messages are unchanged. That covers filters, wildcards, slices, unions, recursive descent, functions and syntax errors.
  • Query::root changes type. Nothing in the repository reads it outside json_path.

E2E results: RedisJSON command throughput

Throughput of each benchmarked command, master vs this PR.

Graviton4 (arm64 Neoverse-V2, AWS m8g.4xlarge). The official redis:8.12.0 server is pinned to one core and loads the RedisJSON module built from master 7ecc513 (ijson 379dadf) or from the PR head. memtier_benchmark runs on 14 other cores with pipeline 1: 8 connections × 2 threads for jsonsl-1 and yelp, 25 × 4 for 10-item, 25 × 8 for session. Each run lasts 30 s; every build gets 6 interleaved runs, and the table shows the median ops/s. p is an exact permutation test (6 vs 6). The server core was saturated (0.99 cores) in every cell except some multipath-5 cells (0.96). A byte-identical copy of master ran as an A/A control. Δ = patched / base − 1; bold means p < 0.05. k is the key.

Datasets:

  • jsonsl-1: 200,000 keys, one 946 B array each.
  • yelp: 20,000 keys, one 38,034 B object each (10 businesses).
  • 10-item: 50,000 keys, one 2,875 B object each (items array of 10 objects).
  • session: 100,000 keys, one 447 B session object each, Zipfian key popularity.
Command Dataset master ops/s P2 ops/s Δ p
JSON.GET k $.[7] jsonsl-1 85,531 123,449 +44.3 % 0.013
JSON.SET k $.[0] '"foo"' jsonsl-1 86,142 103,717 +20.4 % 0.013
JSON.GET k $.businesses[?(@.avg_rating>=4.5)].name yelp 46,616 45,753 -1.9 % (no significant change) 0.089
JSON.GET k $ yelp 21,412 22,287 +4.1 % 0.013
JSON.GET k $..name yelp 34,016 33,190 -2.4 % (no significant change) 0.320
90 % JSON.GET k / 10 % JSON.SET k $ '{"userId":"user-k","organizationId":"org-id","role":"admin",…}' (≈405 B document) session 103,188 120,060 +16.4 % 0.013
JSON.GET k $.items[?(@.qty>5)].name 10-item 59,117 57,024 -3.5 % (no significant change) 0.093
JSON.GET k $.items[*].name $.items[*].qty $.items[*].price $.items[*].attrs.color $.items[*].attrs.size ¹ 10-item 40,856 40,707 -0.4 % (no significant change) 0.132
JSON.TYPE k $.items[*].attrs 10-item 97,485 94,687 -2.9 % 0.048
JSON.OBJLEN k $.items[*].attrs 10-item 84,691 84,410 -0.3 % (no significant change) 0.675
90 % JSON.STRAPPEND k $.items[*].name '"-x"' / 10 % JSON.SET k $.items[*].name '"Item"' 10-item 65,899 72,488 +10.0 % ² 0.284
50 % JSON.SET k $.items[*].extra [1,2,3,4,5,6,7,8] / 50 % JSON.ARRAPPEND k $.items[*].extra '"tag"' 10-item 58,789 63,586 +8.2 % (no significant change) 0.071

¹ multipath-5: 8 of 36 cells ran at 0.96 server cores (3 master, 3 P2, 2 P1+P2), so the base side of this row may be slightly client-limited (up to about 4 %).

² Not usable against master: on this suite the A/A control (master vs its byte-identical copy) moved +12.1 % (p 0.019). master's six runs split into two groups (57–62k and 70–74k ops/s) from per-run server system time, while user CPU per request was flat.

The JSON.TYPE row (p 0.048) falls inside that suite's A/A band (−2.8 %), and its user CPU per request went down (5.15 → 4.38 µs), so it is not counted as a regression.

A/A control (master vs its copy): every suite moved by at most ±6.5 % with p ≥ 0.076, except the STRAPPEND/SET suite (+12.1 %, p 0.019).

used_memory stayed within 0.1 % of master on every suite.

Why

$ and single-element paths are the most common paths in practice, for example JSON.GET key $ and JSON.SET key $.[0] .... Each command compiled them through the full grammar:

  • $.[7] cost 12 allocations (3.5 KB) before the lookup;
  • $ alone cost 12 allocations, and is 0 now.

Results

The benchmarks ran on RedisJSON v8.12.0 with and without this patch on one arm64 Neoverse-V2 host (Graviton4). The rebase onto master was checked for correctness (below); the timings were not re-run on master.

json_path micro (ns per call, median of 6):

Row base patched Δ
compile $ 658 16 −97.6 %
compile $.[7] 939 28 −97.0 %
GET $.[7] (compile + calc + serialize), 1-level doc 1,202 87 −92.8 %
GET $ small doc 2,877 1,958 −31.9 %
GET $ 10-item doc 6,535 5,435 −16.8 %
GET $ large doc (~30 KB) 29,456 26,966 −8.5 %
filter / wildcard / recursive-descent paths (fall back to pest) within ±1 %

Allocations per call:

Row base patched
compile $ 12 / 3,536 B 0 / 0 B
GET $.[7] 16 / 3,864 B 3 / 168 B

Server, µs of server CPU per request. Redis 8.12.0, server pinned to one core, memtier on other cores, 6 interleaved runs, plus an A/A control. p-values are from a permutation test.

Benchmark base patched Δ
JSON.GET k $.[7] (200K keys) 9.28 7.49 −19.3 % (p 0.013)
JSON.SET k $.[0] "..." (200K keys) 10.48 8.63 −17.6 % (p 0.045)
session caching playbook (90 % JSON.GET k $, 10 % JSON.SET k $) 8.73 7.82 −10.4 % (p 0.013)
JSON.GET k (~30 KB doc) 45.46 43.40 −4.5 % (p 0.013)
guards: filter, multipath, recursive descent, wildcard JSON.TYPE no significant change

Correctness (on this branch, rebased on master 7ecc513)

  • Unit tests: cargo test --workspace gives 453 passed and 0 failed, the same as master.
  • Flow tests: tests/pytest/tests.sh against redis-server 8.12.0 passes 377 of 377 in each of general, --use-aof, --use-slaves and --env oss-cluster, the same as master.
  • Differential test against unpatched master:
    • two seeded corpora of about 20,000 paths each: the JSONPath literals in this repo's sources and tests, plus random static chains, near-misses, filters and syntax errors;
    • run on 7 documents (serde_json and ijson);
    • compared through every public entry point: compile errors, calc_once, calc_once_with_paths, calc_once_paths, visit_once_with_paths (first and all), exists, create/calc, create_with_generator/calc_paths, is_static, size, and the pop_last* sequences;
    • 0 differences in 534,716 checks, with about 4,000 paths per corpus taking the static scanner.
  • cargo fmt --check and cargo clippy -p json_path are clean.

🤖 Generated with Claude Code


Note

Medium Risk
Touches core compile/eval for the most common JSON paths; behavior is intended to match pest, but parity depends on careful scanner edge-case handling (indexes, quoted keys).

Overview
json_path compilation now fast-paths simple root-relative paths via a hand-written scan_static_path scanner before invoking pest. Accepted paths are stored as QueryRoot::Static(Vec<StaticStep>) and evaluated with PathCalculator::calc_static (direct key/index walks) instead of walking a pest parse tree.

Query.root is now a QueryRoot enum (Static vs Pest). Public APIs in lib.rs (calc_once, exists, calc_once_with_paths, visit_once_with_paths, calc_once_paths) and query helpers (pop_last, pop_last_object_key, is_static, size) branch on that representation. Non-matching inputs still use pest unchanged—no allocations on reject.

Unit and pytest tests cover overflowing indexes and quoted keys that must stay on the pest path (e.g. leading space after ").

Reviewed by Cursor Bugbot for commit 80edb6e. Bugbot is set up for automated code reviews on this repo. Configure here.

`$` and chains of `.key`, `[n]`, `.[n]`, `['key']` and `["key"]` are the most
common paths (JSON.GET/JSON.SET on the root or one element), but every
command compiled them through the full pest grammar, costing a dozen
allocations for a path like `$.[7]`.

`compile` now tries `scan_static_path` first. On success the query root is
`QueryRoot::Static(Vec<StaticStep>)`, evaluated by `PathCalculator::calc_static`
with direct lookups (the user path tracker is filled in the same order).
The `lib.rs` entry points, including `exists`, dispatch on `QueryRoot`, and
`pop_last*`, `is_static` and `size` have static arms. A rejected path
allocates nothing and goes through pest unchanged, so error messages stay
the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.47%. Comparing base (7ecc513) to head (80edb6e).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1729      +/-   ##
==========================================
+ Coverage   88.24%   88.47%   +0.23%     
==========================================
  Files          18       18              
  Lines        6871     7022     +151     
==========================================
+ Hits         6063     6213     +150     
- Misses        808      809       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Cover the saturating index parse and the leading-space quoted key that
the scanner leaves to pest, both as unit tests and through the module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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