Skip to content

perf(miss-tracking): alloc-free per-position tracking for ArrayPerElementNulls (HMGET) [RED-200840] - #470

Closed
fcostaoliveira wants to merge 1 commit into
fix/red-200840-emptycollection-misstracking-perffrom
fix/red-200840-hmget-allocfree-misstracking
Closed

fcostaoliveira wants to merge 1 commit into
fix/red-200840-emptycollection-misstracking-perffrom
fix/red-200840-hmget-allocfree-misstracking

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Jun 16, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #469 (stacked on it). Fixes the HMGET half of the RED-200840 regression.

ArrayPerElementNulls replies (HMGET / MGET / ZMSCORE / GEOPOS / ...) forced set_keep_value(true), materializing the whole reply tree — a malloc+memcpy per element plus an mbulk/bulk_el node per element — purely to attribute hits/misses per position. For small values the per-element node allocation dominates, so #469 (which only addressed the EmptyCollection/HGETALL path) left HMGET regressed.

This PR replaces the materialized walk with a per-top-level-element hit bitmap recorded directly by the parser (set_track_elem_misses): one uint8_t per element into protocol_response::m_elem_hits — no value copy, no node tree. Hit/miss is known at parse time:

  • bulk leaf → hit iff $N, N>0 (null $-1 and empty $0 are misses);
  • single-type leaf → hit unless RESP3 null _;
  • nested sub-array element → hit iff non-empty (*0/*-1 are misses).

Nesting uses a single counter (m_nested_remaining) tracking outstanding leaf slots in the current top-level element's subtree, so only direct children of the top array are recorded — no frame stack, any depth.

Lab results (Granite Rapids, single-core client, pipeline=64, 40-field hashes, N=5 interleaved)

2.2.1 2.4.1 #469 this PR
HMGET ops/sec 341,542 215,741 (−37%) 214,412 (−37%) 335,293 (−1.8%)
HMGET CPU µs/op 2.58 4.64 (+80%) 4.67 (+81%) 2.92 (+13%)
HGETALL ops/sec 136,020 71,602 (−47%) 132,986 (−2%) 132,313 (−3%)

HMGET throughput restored to within ~2% of 2.2.1; HGETALL stays fixed.

Correctness

Verified deterministically (--key-maximum=1, fixed keys) that per-position hit/miss is identical to the old materialized walk for HMGET (mixed present/absent fields), MGET (multi-key spec), and nested GEOPOS — under both RESP2 and RESP3 (the _ null path). The existing tests/test_arbitrary_command_misses.py suite (flat + GEOPOS-nested + RESP3-nil cases) covers these shapes.

Notes

  • Residual +13% CPU/op on HMGET is the per-element bitmap push + the client-side per_key_hit vector; throughput is effectively at baseline. A further micro-opt (reuse the per_key_hit scratch / skip per-position when no JSON out-file) is possible but diminishing returns.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.8 [email protected]


Note

Medium Risk
Changes the hot-path RESP parser and arbitrary-command miss accounting for multi-element replies; behavior is intended to match the prior materialized walk but any parsing edge case could skew per-position stats.

Overview
ArrayPerElementNulls miss tracking (HMGET, MGET, ZMSCORE, GEOPOS, etc.) no longer turns on set_keep_value(true) to build and walk a full reply tree. Connections that need per-position hits now use set_track_elem_misses(true), and the Redis parser records a compact m_elem_hits bitmap while parsing.

The parser adds track_leaf and a m_nested_remaining counter so only top-level array slots get a hit/miss flag (bulks, RESP3 _, nested non-empty sub-arrays), matching the old walk without malloc/memcpy or per-element nodes. client::handle_response for ReplyShape::ArrayPerElementNulls copies that bitmap into per_key_hit instead of inspecting get_mbulk_value().

SCAN and other paths that still need full replies keep set_keep_value only where required; EmptyCollection and other shapes are unchanged.

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

@jit-ci

jit-ci Bot commented Jun 16, 2026 •

Copy link
Copy Markdown

🛡️ Jit Security Scan Results

CRITICAL HIGH MEDIUM

✅ No security findings were detected in this PR


Security scan by Jit

@fcostaoliveira
fcostaoliveira force-pushed the fix/red-200840-emptycollection-misstracking-perf branch from b87d08f to 1432068 Compare June 16, 2026 12:31
…mentNulls

The arbitrary-command miss-tracking for ArrayPerElementNulls replies
(HMGET/MGET/ZMSCORE/GEOPOS/...) forced set_keep_value(true), materializing the
entire reply tree -- a malloc+memcpy per element plus an mbulk/bulk_el node per
element -- purely to attribute hits/misses per position. For small values the
per-element node allocation, not the value copy, dominates, so this cost ~37%
throughput / ~80% client CPU per op on HMGET (RED-200840, lab-confirmed).

Replace the materialized walk with a per-top-level-element hit bitmap recorded
directly by the parser (set_track_elem_misses): one uint8_t per element into
m_last_response.m_elem_hits, no value copy, no node tree. Hit/miss is known at
parse time:
  - bulk leaf: hit iff $N with N>0 (null $-1 and empty $0 are misses);
  - single-type leaf: hit unless RESP3 null '_';
  - nested sub-array element: hit iff non-empty (*0/*-1 are misses).

Nesting is handled with a single counter (m_nested_remaining) that tracks
outstanding leaf slots in the current top-level element's subtree, so only
direct children of the top-level array are recorded -- no frame stack, any
depth. The client reads the bitmap instead of walking get_mbulk_value(); a
non-array reply (top_array_len<0) still yields one miss bucket, exactly as the
old get_mbulk_value()==NULL guard did.

Verified deterministically (key-maximum=1) that per-position hit/miss is
identical to the materialized walk for HMGET, MGET, and nested GEOPOS, under
both RESP2 and RESP3.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/red-200840-hmget-allocfree-misstracking branch from fc00cfc to 1bf5d3b Compare June 16, 2026 12:31
@fcostaoliveira
fcostaoliveira marked this pull request as ready for review June 16, 2026 12:57
@fcostaoliveira
fcostaoliveira deleted the branch fix/red-200840-emptycollection-misstracking-perf June 16, 2026 14:36
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