Skip to content

Optimize recursive JSONPath traversal - #131

Open
gkorland wants to merge 2 commits into
valkey-io:unstablefrom
gkorland:gkorland-recursive-jsonpath-performance
Open

gkorland wants to merge 2 commits into
valkey-io:unstablefrom
gkorland:gkorland-recursive-jsonpath-performance

Conversation

@gkorland

@gkorland gkorland commented Nov 15, 2026 •

Copy link
Copy Markdown

Recursive JSONPath traversal previously copied the full path and created a stream for each object child. Selector state snapshots also copied the path on every visited container.

Append escaped member names and array indices directly to the existing path buffer, restoring its saved length after traversal. State snapshots now store only the path length. Regression coverage exercises escaped paths, nested filters, recursive updates, and deletes.

Local benchmark: Paired nine-batch JSON.GET measurements on an Apple M2, using matching Release builds and identical results in both versions. Median server time improved in each workload:

Workload Upstream base PR Reduction
Wide object, no matches 292 us 144 us 51%
Deep arrays, no matches 457 us 289 us 37%
Catalog, 768 matches 759 us 272 us 64%
Filtered catalog, 384 matches 2,520 us 1,960 us 22%

Timings are local microbenchmarks; actual gains may vary by workload and environment.

Copilot AI balanced review requested due to automatic review settings November 15, 2026 01:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified for the path-buffer reuse change.

No open issues 🎉

What changed in this PR

This PR reduces overhead in the JSONPath selector by reusing the path buffer during recursive traversal while preserving JSON Pointer escaping.

Changes:

  • Replace per-child path copies with append-and-restore operations.
  • Append escaped object-member names directly through a shared helper.
  • Add regression coverage for sibling restoration, escaped and empty keys, embedded NULs, and array indices.
File Description
tst/​unit/​selector_test.cc Adds recursive traversal path-restoration and escaping coverage.
src/​json/​selector.h Updates the escaping helper to accept a path buffer.
src/​json/​selector.cc Reuses the traversal path buffer and removes stream-based member escaping.

Review effort: Balanced

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Selector path construction now appends escaped object-member names directly to a string. Recursive descent restores the path after each child traversal. A unit test checks returned values and paths, including escaped characters and an embedded NUL.

Changes: Selector path construction

Layer / File(s) Summary
Append escaped member paths
src/json/selector.h, src/json/selector.cc
The escaping method now writes to a jsn::string path. Object-member evaluation and traversal append the slash and escaped member name directly to that path.
Restore paths during recursive descent
src/json/selector.cc, tst/unit/selector_test.cc
Recursive descent restores the path length after object-member and array-index traversal. The new test checks six matched values, their order, and paths containing escaped characters, empty keys, array indices, and an embedded NUL.

Suggested reviewers: roshkhatri, zackcam, mohanrajendran

Change: Refactor

Merge Risk: Minimal · up to ed0b9

This optimizes how JSONPath recursive traversal builds paths and is not expected to change results. The supplied evidence shows no remaining merge-blocking risk, and the new tests cover the main escaping cases.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: recursive JSONPath traversal is optimized. The raw summary shows the change removes per-child path copies and ostringstream use in selector.cc, which matches the title…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description states the PR changes recursive JSONPath traversal. It says escaped member names and array indices are appended directly to the existing path buffer, with the length restored after tra…
Full details: Title check

Explanation

The title names the main change: recursive JSONPath traversal is optimized. The raw summary shows the change removes per-child path copies and ostringstream use in selector.cc, which matches the title. The title is short, specific, and free of noise.

Full details: Description check

Explanation

The description states the PR changes recursive JSONPath traversal. It says escaped member names and array indices are appended directly to the existing path buffer, with the length restored after traversal, and that state snapshots store only the path length. It also lists regression coverage for escaped paths, nested filters, recursive updates, and deletes, plus benchmark results. This matches the changeset in selector.cc, selector.h, and the new selector test.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e84f31cb-9661-4afb-88cb-8b40d461cb94
Commits

Reviewing files that changed from the base of the PR and between b0b6b63 and ed0b972.

Files selected for processing: 3

  • src/json/selector.cc
  • src/json/selector.h
  • tst/unit/selector_test.cc

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Signed-off-by: Guy Korland <gkorland@gmail.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@gkorland
gkorland force-pushed the gkorland-recursive-jsonpath-performance branch from ed0b972 to a6bd244 Compare November 15, 2026 01:55
Signed-off-by: Guy Korland <gkorland@gmail.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.

2 participants