Repository navigation
Conversation
Replace pairwise ancestor checks in dom_merge_value with a sorted-prefix scan over matched JSON pointers, ordering '/' before token bytes so each ancestor precedes its entire subtree. Add unit tests covering prefix boundaries, escaped keys, and O(P log P) comparison scaling across disjoint targets and overlapping subtrees. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Signed-off-by: Guy Korland <gkorland@gmail.com>
There was a problem hiding this comment.
🟢 Approval recommended
The JSON.MERGE performance optimization has no active review issues.
No open issues 🎉
What changed in this PR
Improves JSON.MERGE performance when paths match many targets, while preserving ancestor-wins behavior for overlapping selections.
Changes:
- Replaces quadratic ancestor comparisons with separator-aware sorting and a prefix scan, reducing filtering complexity to
O(P * L * log P). - Adds regression tests for pointer boundaries, including escaped characters, empty keys, and embedded NULs.
- Adds comparison-count scaling checks for disjoint targets and overlapping subtrees.
| File | Description |
|---|---|
tst/unit/dom_test.cc |
Adds boundary-case, merge-correctness, and scaling tests. |
src/json/dom.cc |
Introduces topmost-target filtering, integrates it into merge processing, and updates complexity documentation. |
Review effort: Balanced
|
No actionable comments were generated in the recent review. Walkthrough
Change: Refactor Merge Risk:
|
JSON.MERGEpreviously performed pairwisepath_is_ancestorcomparisons across all matched JSON pointers (O(P^2 * L)), causing quadratic scaling when wildcard or recursive-descent paths matched many targets.Replace the nested loop with
filter_topmost_merge_targets, which sorts matched pointers with'/'ordered before all token bytes and filters descendants in a single linear prefix scan (O(P * L * log P)). Because literal slashes inside JSON Pointer tokens are escaped as~1per RFC 6901, ordering'/'first keeps every ancestor contiguous with its entire subtree even when sibling keys share a prefix (for example"a!"or"a-"alongside"a").Regression and scaling tests in
dom_test.cccover tricky prefix boundaries (escaped slashes/tildes, empty keys, embedded NULs) and deterministic comparison-count bounds across both disjoint targets and multi-level overlapping subtrees.Local benchmark: End-to-end
dom_merge_valuemedian timings (5 runs after warmup, matching-O3 -DNDEBUGRelease builds on arm64):P)unstable$.users[*].profile)$.users[*].profile)$.users[*].profile)$..a)$..a)$..a)Timings are local microbenchmarks; actual gains may vary by workload and environment.