Skip to content

v4: honour RequestBufferPool for request staging; hash requests once, on demand - #3416

Merged
mgravell merged 2 commits into
v4from
marc/v4-request-buffer-pool
Dec 3, 2026
Merged

mgravell merged 2 commits into
v4from
marc/v4-request-buffer-pool

Conversation

@mgravell

@mgravell mgravell commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Two request-path fixes raised on #3364.

RequestBufferPool is honoured for request staging. v3 buffered every request in the configured RequestBufferPool. v4's transport already used it for the socket's write buffers, but the copy an operation holds until the request is written always came from ArrayPool<byte>.Shared.

  • Executors now carry the configured pool (RespExecutorBase.RequestBufferPool, supplied by the connection manager and forwarded by the database, batch and transaction executors), and RespPayloadOperation.Attach rents from it. The owner is released through IDisposable when the last reference to the request goes, exactly once.
  • Requests that fit the operation's 512-byte inline buffer are unaffected: they never used a pool.
  • A large request that arrives owned (the blocking path, and anything detached) used to be shared zero-copy, keeping the builder's ArrayPool array alive until the write. With a pool configured it's now copied into that pool instead. That's one extra copy for large requests, only for callers who asked for a pool; everyone else keeps the zero-copy hand-off.
  • Not covered here: the builder's own scratch buffer, which on the common path is lent and returned within the call. That belongs with letting the builder use stack space for small requests, which is a separate change.

Requests are hashed once, and only when the cache needs it. RespRequest hashed its whole payload in the constructor, for every request, cached or not. Merely deferring that to GetHashCode() makes a cache miss worse: each dictionary operation sees a different copy of the key, so a miss-and-store hashed about 7 times (lookup, in-flight probe, doorkeeper, fill registration, store, in-flight removal). Copies of a readonly struct can't share a lazily computed field, so the hash lives on the one instance that is passed by reference throughout:

  • RespRequestFrame computes the hash on first use. The cache's three entry points ask the frame (AsHashedLookupKey(), and frame.Hash for the doorkeeper).
  • Every request derived from the frame afterwards carries the hash through a constructor argument: lookup views, Detach, both CopyForCacheKey overloads (including the slab copy), ToOwned and WithFlags. So every EntryKey built from them reuses it.
  • A request built any other way computes its own hash on first use, with an Unsafe.AsRef write-back. The race is benign, since every thread writes the same value.
  • 0 means "not computed": ComputeHash maps a real 0 to 1, and every path goes through it, so a carried hash and a computed one always agree.
  • Equals is now length plus a byte comparison. Dictionaries only call it once the stored hash codes match.

The result: uncached requests and every write (cached or not) hash 0 times, so a large SET never hashes its value. A hit hashes once, and so does a miss-and-store. Writes never reach the cache's lookup path (IsCacheable requires a read-only command), and their local invalidation works from the key bytes.

Found along the way, and left alone: the transport's write buffer, also rented from RequestBufferPool, isn't returned when a connection is torn down. BufferedStreamWriter deliberately recycles only on a clean exit, rather than risk returning memory a write may still touch, and the GC reclaims it. It's the right trade, but a pool that counts outstanding rentals will see it, so docs/Configuration.md now says what the option covers.

Testing:

  • New RequestBufferPoolTests, using a counting MemoryPool:
    • every large SET (sync and async) stages in the configured pool;
    • every request copy is returned;
    • nothing, request or transport block, is returned twice.
      Ignoring the pool in Attach fails both cases.
  • New RequestHashTests, using a DEBUG-only per-thread count of hash computations ([Conditional("DEBUG")], so it costs nothing in Release):
    • miss-and-store hashes once;
    • a hit hashes once;
    • re-filling after invalidation hashes once;
    • admission through the doorkeeper hashes once per miss;
    • a large SET with caching on, and a CacheOptOut read, hash 0 times.
      When the frame stops passing its hash on, the counts go to 7 and 3. Also pinned: content-based equality and hashing across arrays, offsets and flags. The counting tests skip in Release, which is what CI builds, and run in Debug. So AGENTS.md now steers local test runs to Debug, where DEBUG-only checks (asserts, RESPite's buffer leak tracking, instrumentation like this) actually run.
  • net10.0 full suite against a Redis 8.12 topology:
    • Release: 8208 total, 0 failed.
    • Debug: 2 failures, and both fail identically on v4 without this change (InterpolatedCustomArgTests.AStructImplementerDoesNotBox, ExceptionFactoryTests.MultipleEndpointsThrowConnectionException).
  • SanityChecks pass, and the library builds on all TFMs.

…mand

A request held by an operation until it is written (anything past the 512-byte inline
buffer) is now copied into the configured RequestBufferPool rather than ArrayPool.Shared,
as v3 buffered every request there. With no pool configured nothing changes.

RespRequest no longer hashes its payload at construction: GetHashCode computes it when
asked, which only the client-side cache does, so uncached sends - and every write, cache
or not - never hash the value.
The frame - the one instance the cache path passes by reference - computes the hash on first
use and every request derived from it carries it: lookup views, the detached fill key, the
stored copy. A miss-and-store hashed 7 times with on-demand hashing alone; now once, as does
a hit. 0 marks 'not computed', so a real 0 maps to 1.

A DEBUG-only per-thread counter lets tests pin the counts; they skip in Release. AGENTS.md
now steers local test runs to Debug, where DEBUG-only checks actually run.
@mgravell mgravell changed the title v4: honour RequestBufferPool for request staging; hash requests on demand v4: honour RequestBufferPool for request staging; hash requests once, on demand Dec 3, 2026
@mgravell
mgravell merged commit dbd397f into v4 Dec 3, 2026
3 checks passed
@mgravell
mgravell deleted the marc/v4-request-buffer-pool branch December 3, 2026 23:42
@mgravell mgravell mentioned this pull request Dec 4, 2026
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