Skip to content

Probe IObject by &str without interning the key - #27

Closed
fcostaoliveira wants to merge 1 commit into
RedisJSON:masterfrom
fcostaoliveira:pr/lookup-by-str-without-intern
Closed

fcostaoliveira wants to merge 1 commit into
RedisJSON:masterfrom
fcostaoliveira:pr/lookup-by-str-without-intern

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Nov 24, 2026 •

Copy link
Copy Markdown

Summary

ObjectIndex for &str interned the probe key before every lookup, mutable lookup and removal: a cache-guard acquisition, a hash into the global string set, a refcount bump and, on a miss, an allocation that the temporary's drop then freed, all before the object's own table was consulted. The table hashes key bytes, so a borrowed &str can probe it directly.

This PR adds a small ProbeKey trait with impls for IString (identity-first equality, as today) and str (same DefaultHasher over the bytes via str::hash, byte equality against resident.as_str()), makes find_bucket_impl generic over it, and routes index_into, index_into_mut and remove for &str through the borrowed probe. index_or_insert still interns, because an insertion needs an owned key. Interning semantics are unchanged: a lookup never created a cache entry that outlived the call.

In RedisJSON this is the path behind every JSON path step through an object (json_path's get_key) and every by-key get/get_mut in ivalue_manager.

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 are interleaved three times; the delta is the median over the three pairs, and the spread column is the base's own run-to-run spread.

x86-64, Intel Sapphire Rapids (Xeon Platinum 8488C), Linux, bench pinned to one core, host idle

Benchmark Base Candidate Δ Base spread
lookup/business-22-hit (22-key object, key present) 56.0 ns 40.9 ns −26.9% 0.1%
lookup/business-22-miss 70.7 ns 15.0 ns −78.8% 0.6%
lookup/path-chain-yelp ($.businesses[0].id) 56.7 ns 24.2 ns −57.3% 0.2%
lookup/wide-128-hit-long-key 71.2 ns 53.7 ns −24.6% 0.1%
lookup/wide-128-hit-short-key (inline key) 55.4 ns 38.7 ns −30.1% 0.1%
lookup/wide-128-miss-long-key 157.6 ns 100.9 ns −36.0% 0.1%
lookup/typed-array-slice-index (control) 21.6 ns 21.6 ns 0.0% 0.0%
mutation/num-incrby-i64 (get_mut("field")) −32.8%
mutation/num-incrby-f64 −28.0%

arm64, Apple M-series, macOS (same protocol, no pinning available): hits −30 to −46%, misses −48 to −83%, by-key mutations −10 to −39%, control +0.0%.

Memory: retained and peak bytes per fixture, mem_allocated(), clone/serialise/CBOR sizes are identical to the baseline on every fixture; lookups allocate nothing before or after.

TMA (Linux, toplev level 2, 20 s windows, CPU-pinned), lookup/path-chain-yelp:

                      base    candidate
Frontend_Bound        5.9%      1.8%
Bad_Speculation       0.4%      0.4%
Backend_Bound        34.0%     12.9%
  Memory_Bound       21.6%      0.0%
  Core_Bound         12.4%     12.9%
Retiring             59.8%     85.0%

The intern-cache round trip was the memory-bound part of a lookup; what remains is a core-bound hash and probe.

Things worth knowing

  • Two benchmarks that do not execute any changed code moved on the Linux host (+3.7% on a mem_allocated() tree walk, +4.0% on a 24 ns array insert/remove loop) across three interleaved pairs. Their TMA trees and nm placement point at code layout rather than the change; the Apple host shows the same class of movement on different benchmarks. Flagging rather than hiding it.
  • str::hash and IString's as_str().hash feed DefaultHasher the same bytes, so a &str probe lands in the bucket the stored key was hashed into.
  • Tests: cargo test --lib -- --test-threads=1 passes in both the default and thread_safe configurations.

The benchmark suite and the full experiment record (including rejected variants) live in the redis-performance/json-agent-workspace repository, branch fcostaoliveira/add-optimize-overnight-skill, under experiments/EXP-002-lookup-no-intern/.


Note

Medium Risk
Touches core object lookup/removal logic with unsafe bucket indexing; behavior must stay aligned with existing hash/equality invariants, but scope is localized and insertion semantics are unchanged.

Overview
Read-only object key operations no longer call IString::intern on the lookup key. Instead they probe the object's hash table (or small-object linear scan) directly using borrowed &str bytes.

The change introduces a ProbeKey abstraction and makes find_bucket_impl generic over it: IString keeps identity-first matching; &str uses the same DefaultHasher over the string bytes and compares with resident.as_str(). find_bucket_str exposes that path, and ObjectIndex for &str's index_into, index_into_mut, and remove use it end-to-end. index_or_insert still interns, since insertion requires an owned key.

This targets hot paths such as JSON path steps and by-key get / get_mut in RedisJSON, cutting intern-cache work on hits and especially on misses.

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

RedisJSON impact by command

RedisJSON 9ca1c88 built unchanged against pinned ijson (379dadf) and against this change (Cargo [patch] only), on a pinned redis-server (x86-64, Intel Sapphire Rapids, Linux); memtier -c 16 -t 2 15 s runs (-c 1 -t 1 for the 633 KB issue674 document; the yelp filter and $..name rows with redis-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.

Command Workload pinned ijson ops/s this PR ops/s Δ throughput p50 ms
JSON.SET $.businesses[*].reviews[*].geo <GeoJSON feature>, 28 targets 19,215 21,683 +12.8% 1.559 → 1.351
JSON.SET $.businesses[*].extra <26-key object>, 10 targets 24,800 27,515 +10.9% 1.199 → 1.071
JSON.ARRAPPEND $.businesses[*].categories <GeoJSON feature>, 10 targets 45,833 50,772 +10.8% 0.623 → 0.575
JSON.MERGE $.businesses[*] {"merged": <26-key object>}, 10 targets 17,629 18,845 +6.9% 1.695 → 1.583
JSON.NUMINCRBY $.businesses[*].review_count 1, 10 targets 94,105 100,302 +6.6% 0.319 → 0.295
JSON.GET $.businesses[?(@.avg_rating>=4.5)].name yelp 66,637 70,555 +5.9% 0.191 → 0.183
JSON.GET $.businesses[*].reviews[*].user_name, 28 leaves 83,529 88,404 +5.8% 0.359 → 0.335
JSON.GET $..uid over the 633 KB issue674 document, 4,378 objects 1,341 1,415 +5.5% 0.751 → 0.711
JSON.GET five wildcard paths over 10 businesses ($.businesses[*].{name,city,review_count,avg_rating,zip}) 52,691 54,937 +4.3% 0.559 → 0.543

Unchanged (|Δ| < 3%, 9 of 18 workloads): JSON.GET: $.message.code on the 38 KB yelp doc, $.ResultSet.totalResultsAvailable yahoo2, $.[8].zero jsonsl-1, $ whole 38 KB yelp document, $.ResultSet.totalResultsAvailable yahoo2 at pipeline 16, $..[?(@.uid==1198)].MD5ModelUID issue674, $..name yelp; JSON.SET: $.message.code 1 yelp; JSON.NUMINCRBY: num $ 1. Single-path commands at pipeline 1 spend their time in protocol handling (server TMA identical across variants); the filter over 4,378 objects and the string-heavy serialisations spend theirs outside the lookup path.

Every mover performs one key lookup per visited object (json_path get_key, follow_path get_mut), the path this PR takes off the interning cache. Memory unchanged.

🤖 Generated with Claude Code

ObjectIndex for &str used IString::intern(self) before every lookup, remove
and mutable lookup: a cache-guard acquisition, a hash into the global
string set, a refcount bump and, on a miss, an alloc/free pair, all before
the object's own table was consulted. The table hashes key bytes, so a
borrowed &str can probe it directly: a ProbeKey trait hashes the borrowed
bytes with the same hasher and compares bytes against resident keys.
Insertion (index_or_insert) still interns because it needs an owned key.
No change to interning semantics: lookups never created cache entries that
outlived the call.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@fcostaoliveira

Copy link
Copy Markdown
Author

RedisJSON end-to-end results for this change are in: RedisJSON 9ca1c88 built unchanged against pinned ijson, this PR, and this PR + #28, on a pinned redis-server (x86-64 Sapphire Rapids), memtier, three interleaved rounds per cell, with server-side TMA.

  • Single-path JSON.GET/JSON.SET/JSON.NUMINCRBY at pipeline 1: within ±1% (server TMA identical).
  • One lookup per visited object ($..uid over 4,378 objects, wildcard/multi-path GET, wildcard NUMINCRBY): +4.3…+6.6% throughput.
  • Multi-target writes that also look up per target: wildcard JSON.SET +11.0…+12.8%, JSON.MERGE +6.9%, JSON.ARRAPPEND +10.8% (this PR alone).

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

@fcostaoliveira

Copy link
Copy Markdown
Author

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.

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.

1 participant