Skip to content

Skip scalar descendants in JSONPath recursive descent - #1717

Merged
AvivDavid23 merged 2 commits into
RedisJSON:masterfrom
g4lb:perf/json-path-recursive-scalars
Nov 30, 2026
Merged

AvivDavid23 merged 2 commits into
RedisJSON:masterfrom
g4lb:perf/json-path-recursive-scalars

Conversation

@g4lb

@g4lb g4lb commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

After $.., a key, *, list, slice or filter selector matches only inside an object or array. For these selectors, calc_full_scan now skips scalar children. The results, order and paths do not change.

Benchmark (2000-element document) Before After Change
$..author (IValue) 867 µs 462 µs -46.7%
$..author (serde_json) 881 µs 497 µs -43.6%
$..author, calc_once_paths 1.17 ms 632 µs -45.8%
$..* (IValue) 1.26 ms 871 µs -31.1%
  • cargo test -p json_path passes.
  • Old and new results matched on 225k random documents and paths.

Note

Low Risk
Single localized change in traversal logic with explicit semantic justification and broad equivalence testing; no API or output contract changes intended.

Overview
Recursive descent ($..) no longer walks into scalar child nodes when continuing the scan under objects and arrays. In calc_full_scan, descendants that are not objects or arrays are skipped before calc_internal and further calc_full_scan calls, because selectors after .. cannot match inside scalars.

This is a behavior-preserving optimization: match sets, order, and paths stay the same (validated against a large random corpus), with roughly 30–47% faster benchmarks on paths like $..author and $..* on large documents.

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

@CLAassistant

CLAassistant commented Nov 29, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Comment thread json_path/src/json_path.rs Outdated
calc_data: &mut PathCalculatorData<'_, 'j, S, UPTG::PT>,
) -> ControlFlow<()> {
// These selectors match nothing on a scalar, so scalar descendants can be skipped.
let needs_container = matches!(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs_container is always true here, since the grammar only allows literal | all | [bracket] after .. and bracket is exactly these six rules, so please drop the peek() check or change the comment to say it guards selectors that might be added later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 5f2cb79.

Comment thread json_path/src/json_path.rs Outdated
val.get_type(),
SelectValueType::Object | SelectValueType::Array
);
if !is_container && needs_container {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If needs_container goes away, this can be an unconditional if !is_container { continue; }.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 5f2cb79.

Comment thread json_path/src/json_path.rs Outdated
calc_data,
)?;
self.calc_full_scan(pairs.clone(), val, path_tracker, calc_data)?;
if is_container {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check can never be false, because scalar children already hit the continue anove, so please call calc_full_scan directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 5f2cb79.

@AvivDavid23 AvivDavid23 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the addition! Please check the comments

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.13%. Comparing base (0805574) to head (239e184).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1717      +/-   ##
==========================================
- Coverage   88.14%   88.13%   -0.02%     
==========================================
  Files          18       18              
  Lines        6851     6861      +10     
==========================================
+ Hits         6039     6047       +8     
- Misses        812      814       +2     

☔ 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.

@AvivDavid23

Copy link
Copy Markdown
Contributor

@g4lb can you rebase your branch to master?

@g4lb

g4lb commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

@AvivDavid23 Done. I rebased it on master.

@g4lb
g4lb force-pushed the perf/json-path-recursive-scalars branch from 5f2cb79 to 239e184 Compare November 30, 2026 11:52

@AvivDavid23 AvivDavid23 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution! approving

@AvivDavid23
AvivDavid23 merged commit d219b62 into RedisJSON:master Nov 30, 2026
65 of 66 checks passed
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.

3 participants