Skip to content

Cache path depths for mutation ordering - #133

Open
gkorland wants to merge 2 commits into
valkey-io:unstablefrom
gkorland:gkorland-mutation-ordering-performance
Open

gkorland wants to merge 2 commits into
valkey-io:unstablefrom
gkorland:gkorland-mutation-ordering-performance

Conversation

@gkorland

Copy link
Copy Markdown

Mutation ordering in order_result_set_deepest_first previously rescanned each JSON Pointer path with std::count(..., '/') on every std::stable_sort comparison, doing repeated O(path length) work across O(N log N) comparisons.

Precompute each path's '/' depth once before sorting while preserving stable deepest-first ordering for equal depths, and return early when rs.size() <= 1 to avoid allocating a depth vector or scanning the path on empty and singleton result sets. Unit tests cover deepest-first ordering, equal-depth tie stability, escaped path tokens, empty results, and root/nested singletons.

Local benchmark: Paired nine-round Release (-O3 -DNDEBUG) measurements on arm64 comparing unstable against this change:

Workload Base (median) PR (median) Speedup
order_result_set (N=1) 12.60 ns 11.05 ns 1.14x
order_result_set (N=16) 2.02 us 0.28 us 7.32x
order_result_set (N=256) 133.84 us 7.23 us 18.51x
order_result_set (N=2048) 2,175.58 us 88.50 us 24.58x
End-to-end dom_clear("$..items") (341 matches) 426.44 us 342.93 us 1.24x

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

gkorland and others added 2 commits November 14, 2026 23:13
Signed-off-by: Guy Korland <gkorland@gmail.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Guy Korland <gkorland@gmail.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 15, 2026 07:17
@coderabbitai

coderabbitai Bot commented Nov 15, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 40 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

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: 7ed7378b-3e70-41b0-8af3-cfdfead5d5ae
Commits

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

Files selected for processing: 2

  • src/json/dom.cc
  • tst/unit/dom_test.cc
  • 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

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 remain for the mutation-ordering optimization.

No open issues 🎉

What changed in this PR

Optimizes mutation ordering by computing each JSON Pointer path’s depth once, preserving deepest-first ordering and stable ordering for equal-depth paths.

Changes:

  • Replaces repeated path scans during sorting with cached depths.
  • Returns early for empty and singleton result sets, avoiding depth allocation and scanning.
  • Adds unit tests for ordering, tie stability, escaped tokens, and empty and singleton results.
File Description
tst/​unit/​dom_test.cc Adds focused tests for mutation ordering and edge cases.
src/​json/​dom.cc Caches path depths before sorting and adds an empty/singleton fast path.

Review effort: Balanced

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