Repository navigation
Clone an exact-sized IObject by copying its bucket table - #28
fcostaoliveira wants to merge 2 commits into
Conversation
IObject::clone_impl rebuilt the table of the clone by re-inserting every key, hashing each one again although the keys are the same interned strings. When the source is exact-sized (capacity == len, which is what compact parsing produces) the clone has the same capacity and therefore the same hash_capacity, so the items are cloned index-for-index and the bucket table is copied verbatim. Sources with spare capacity keep the rebuild. Capacity, item order, table contents and memory of the clone are unchanged; only the hashing is gone.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit bccd2cb. Configure here.
… leak them Review finding on the table-copy clone: set_len was called once after the loop, so a panic inside a nested key/value clone left len at 0 and drop_impl would deallocate the written slots without dropping them. Set len after each item instead.
|
RedisJSON end-to-end results for this change (stacked on #27, as in the Where RedisJSON clones the argument once per target, versus #27 alone:
GET workloads and single-path commands: unchanged (no clone on those paths). Memory: clone capacity and retained bytes unchanged. Full record, raw memtier output and TMA trees: https://github.com/redis-performance/json-agent-workspace/blob/main/experiments/MODULE-001-redisjson-end-to-end/README.md 🤖 Generated with Claude Code |
|
Closing. A re-measurement on this exact head still shows a benchmark or end-to-end row more than 3% worse than base, which is our bar for proposing a change. I will reopen or resubmit once that row is resolved. |

Summary
IObject::clone_implbuilt the clone by re-inserting every key into a fresh table, hashing each key again although the keys are the same interned strings and the clone has the same capacity as the source would need. When the source is exact-sized (capacity() == len, which is what compact parsing produces for every object RedisJSON loads), the clone has the same capacity and therefore the samehash_capacity, so this PR clones the items index-for-index and copies the bucket table verbatim. Sources with spare capacity (after mutations) keep the existing rebuild path.Nothing observable about the clone changes: same capacity (
with_capacity(len), as before), same item order, same table contents, same bytes. Only the hashing is gone. Pre-checks done in the source:DefaultHasher::new()has fixed keys (no per-table seed), the table has no tombstones (remove_bucketshifts the Robin Hood run back), and the clone's capacity was alreadylen.In RedisJSON this is the
.clone()behind JSON.SET overwrite paths, JSON.GET of subtrees and JSON.MSET.Measurements
Criterion microbenchmarks mirroring RedisJSON's call paths (
default-features = false, string cache initialised as OSS RedisJSON does, system allocator, release profile), base and candidate interleaved three times, median over the pairs, spread = the base's own run-to-run spread.x86-64, Intel Sapphire Rapids (Xeon Platinum 8488C), Linux, bench pinned to one core, host idle
arm64, Apple M-series, macOS (same protocol, no pinning): clone/yelp −58%, memory identical.
Memory: retained and peak bytes,
mem_allocated(), clone/serialise/CBOR sizes identical to the baseline on every fixture.TMA (Linux, toplev level 2, CPU-pinned),
clone/yelp:The removed work (SipHash and probing, ~46% of the baseline clone's samples) was retiring-heavy; what remains per item (a refcount increment and two dependent stores) is a core-bound chain, so the tree shifts toward Backend/Core while the clone gets 2.2× faster.
Things worth knowing
get_mut+ number replace loop) across three interleaved pairs on the Linux host;nmplacement and TMA (Frontend_Bound 14.6% → 18.4% on the sibling f64 variant) point at code layout. Flagging rather than hiding it.iter_batchedsetup cloned an object; those were the setup's cache state, confirmed byperf record(the timed routine was under 2% of the process samples) and gone once the benchmarks mutate in place.cargo test --lib -- --test-threads=1passes in both the default andthread_safeconfigurations.Benchmark suite and the full experiment record (including the superseded verdicts) live in
redis-performance/json-agent-workspace, branchfcostaoliveira/add-optimize-overnight-skill, underexperiments/EXP-003-clone-copy-table/.Note
Medium Risk
Unsafe pointer/table copying on a core data structure; correctness depends on exact-capacity invariants and valid source tables, though behavior is meant to be unchanged.
Overview
Optimizes
IObject::clone_implwhen the source is exact-sized (capacity() == len, typical after compact parsing): instead of re-inserting every entry and re-hashing keys, the clone writes key/value pairs index-for-index, bumpslenafter each item (so a panicking nested clone does not leak partial state), and memcopies the Robin Hood bucket table when one exists.Objects that still have spare capacity after mutations keep the previous rebuild-via-
insertpath. Observable clone semantics (capacity, order, lookup behavior) are intended to stay the same; the win is avoiding SipHash and probing on the hot RedisJSON clone paths.Reviewed by Cursor Bugbot for commit 505321b. Bugbot is set up for automated code reviews on this repo. Configure here.
RedisJSON impact by command
RedisJSON
9ca1c88built unchanged against pinnedijson(379dadf) and against this change (Cargo[patch]only), on a pinnedredis-server(x86-64, Intel Sapphire Rapids, Linux); memtier-c 16 -t 215 s runs (-c 1 -t 1for the 633 KB issue674 document; the yelp filter and$..namerows withredis-benchmark -c 16 --threads 2 -n 300000), three interleaved rounds per cell, base run-to-run spreads 0.3–5.1%. Only commands that moved by 3% or more are listed; everything else is within noise. Full record, raw memtier output and server TMA: MODULE-001 in json-agent-workspace.$.businesses[*].extra <26-key object>, 10 targets$.businesses[*] {"merged": <26-key object>}, 10 targets$.businesses[*].reviews[*].geo <GeoJSON feature>, 28 targets$.businesses[*].categories <GeoJSON feature>, 10 targetsUnchanged (|Δ| < 3%, 14 of 18 workloads): JSON.GET:
$.message.codeon the 38 KB yelp doc,$.ResultSet.totalResultsAvailableyahoo2,$.[8].zerojsonsl-1,$whole 38 KB yelp document,$.ResultSet.totalResultsAvailableyahoo2 at pipeline 16, five wildcard paths over 10 businesses,$.businesses[*].reviews[*].user_name,$..uidover the 633 KB issue674 document,$..[?(@.uid==1198)].MD5ModelUIDissue674,$.businesses[?(@.avg_rating>=4.5)].nameyelp,$..nameyelp; JSON.SET:$.message.code 1yelp; JSON.NUMINCRBY:num $ 1,$.businesses[*].review_count 1. No clone runs on those paths.Every mover clones its argument once per matched target (
value.clone()/val.clone()/args.clone()incommands.rs): a 26-key object with a bucket table gains the most (table copied instead of 26 re-inserts); the small nested GeoJSON objects gain the index-for-index copy alone. Memory: same capacity and retained bytes as before.🤖 Generated with Claude Code