Repository navigation
Lower JSONPath filters once per container - #1730
Open
fcostaoliveira wants to merge 3 commits into
Open
fcostaoliveira wants to merge 3 commits into
fcostaoliveira wants to merge 3 commits into
Conversation
A filter such as `$.items[?(@.qty>5)].name` was interpreted from the pest tree for every candidate element: the operands, the operator and the literal were re-read and the literal re-parsed per element, and each candidate built its own PathCalculatorData and result Vec. `lower_filter` now turns a filter into a `FastFilter` once per container: OR-of-AND groups of tests (operand, operator, operand), with parenthesized groups and negation. Operands are `@`-relative singular paths, resolved with direct `get_key` / `get_index`, or literals parsed once. Comparisons reuse `TermEvaluationResult`, so semantics are unchanged. Anything outside that subset (functions, regex, non-singular operands, arithmetic) returns `None` and runs through the existing interpreter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1730 +/- ##
==========================================
+ Coverage 88.24% 88.84% +0.60%
==========================================
Files 18 18
Lines 6871 7066 +195
==========================================
+ Hits 6063 6278 +215
+ Misses 808 788 -20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Lowering walks the filter's parse tree about as far as interpreting one candidate does, so on a container with one candidate it is pure overhead: the jsonpath benchmark row root-single-candidate rose from 6,685 to 9,037 instructions (+35%). Lower only when the container has at least two candidates, the same condition that enables the root operand cache. Fold the existence and comparison forms of a lowered test into one enum, share the decimal literal parse with the interpreter, and test each lowered shape against the general evaluator, in json_path and through the module. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A filter that references `$`, uses `~` (regex or keys) or calls a function or method was still lowered on each container of two or more candidates, and the attempt allocated before failing: about 2.3k instructions per container, +21% on a two-row array with `$`. A scan of the filter's text, which skips quoted strings, now sends these filters straight to the interpreter. The scan only rejects: a filter it lets through is lowered as before, and a filter it rejects needlessly (an unquoted `$` in a field name) is evaluated by the interpreter, which handles every filter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
What
JSONPath filters such as
$.items[?(@.qty>5)].nameare now lowered once per container, not interpreted from the pest tree for every candidate element.lower_filterturns the filter into aFastFilter:!negation.> >= < <= == !=.@-relative singular path (@,@.k,@.a.b,@[n],@['k']), resolved with directget_key/get_index; ortrue/false/null), parsed once.fast_filterevaluates each candidate with the existingTermEvaluationResultcomparisons, so ordering, type and number semantics are unchanged.Noneand runs through the existing interpreter. That covers functions, regex=~,in/nin/subsetofand the like,$-rooted or non-singular operands, and arithmetic.E2E results: RedisJSON command throughput
Throughput of each benchmarked command,
mastervs this PR.Graviton4 (arm64 Neoverse-V2, AWS m8g.4xlarge). The official
redis:8.12.0server is pinned to one core and loads the RedisJSON module built frommaster7ecc513 (ijson379dadf) 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 ofmasterran as an A/A control. Δ = patched / base − 1; bold means p < 0.05.kis the key.Datasets:
itemsarray of 10 objects).JSON.GET k $.[7]JSON.SET k $.[0] '"foo"'JSON.GET k $.businesses[?(@.avg_rating>=4.5)].nameJSON.GET k $JSON.GET k $..nameJSON.GET k/ 10 %JSON.SET k $ '{"userId":"user-k","organizationId":"org-id","role":"admin",…}'(≈405 B document)JSON.GET k $.items[?(@.qty>5)].nameJSON.GET k $.items[*].name $.items[*].qty $.items[*].price $.items[*].attrs.color $.items[*].attrs.size¹JSON.TYPE k $.items[*].attrsJSON.OBJLEN k $.items[*].attrsJSON.STRAPPEND k $.items[*].name '"-x"'/ 10 %JSON.SET k $.items[*].name '"Item"'JSON.SET k $.items[*].extra [1,2,3,4,5,6,7,8]/ 50 %JSON.ARRAPPEND k $.items[*].extra '"tag"'¹ 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 (mastervs 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.A/A control (
mastervs 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_memorystayed within 0.1 % ofmasteron every suite.Why
For each candidate element, the interpreter:
Pairs;PathCalculatorDataand resultVecfor every@operand.On a 10-item document this costs more than the path compile itself.
Results
The benchmarks ran on RedisJSON v8.12.0 with and without this patch on one arm64 Neoverse-V2 host (Graviton4). The rebase onto
masterwas checked for correctness (below); the timings were not re-run on master.json_pathmicro (ns per call, median of 6):$.items[?(@.qty>5)].name, 10-item doc$.businesses[?(@.avg_rating>=4.5)].name, ~30 KB docAllocations per filter GET: 34 → 28 (8,968 → 8,824 B).
Server, µs of server CPU per request. Redis 8.12.0, server pinned to one core, 6 interleaved runs, plus an A/A control. p-values are from a permutation test.
JSON.GET k $.items[?(@.qty>5)].name(50K keys, 10-item docs)$.[7],$, session playbook, multipath, recursive descent, wildcardJSON.TYPEWith #1729 the filter GET is −19.9 %. The two patches are independent and apply in either order.
Correctness (on this branch, rebased on
master7ecc513)cargo test --workspacegives 453 passed and 0 failed, the same asmaster.tests/pytest/tests.shagainstredis-server8.12.0 passes 377 of 377 in each of general,--use-aof,--use-slavesand--env oss-cluster, the same asmaster.master:@path,&&/||/!/parentheses, and syntax errors;exists, thepop_last*sequences, and so on;cargo fmt --checkandcargo clippy -p json_pathare clean.🤖 Generated with Claude Code
Note
Medium Risk
Changes core filter evaluation for a common query shape; unsupported constructs must fall back identically, and differential tests cover parity but edge cases around lowering eligibility matter.
Overview
Adds a filter fast path for JSONPath: when an array/object has more than one child, eligible filters are lowered once into a
FastFilter(OR-of-AND groups of comparisons, existence tests, negation, and parentheses) instead of re-walking the pest parse tree for every candidate.Lowering only covers singular
@paths (single key/index steps) plus scalar literals compared with> >= < <= == !=. A lightweightfilter_needs_interpretertext scan skips lowering early for$,~, and function-call patterns. Anything else—including regex, arithmetic,$operands, wildcards, and..—falls back to the existingevaluate_filterinterpreter unchanged.Per-candidate evaluation on the fast path uses direct key/index resolution and the same
TermEvaluationResultcomparison helpers, so semantics should match the interpreter for the supported subset. Single-element containers skip lowering (cost/benefit).Also extracts
TermEvaluationResult::parse_decimalfor shared literal parsing, and adds Rust/pytest tests that fast-path and interpreter paths agree (plus pre-scan unit tests).Reviewed by Cursor Bugbot for commit d77885f. Bugbot is set up for automated code reviews on this repo. Configure here.