Repository navigation
v4: honour RequestBufferPool for request staging; hash requests once, on demand - #3416
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two request-path fixes raised on #3364.
RequestBufferPoolis honoured for request staging. v3 buffered every request in the configuredRequestBufferPool. 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 fromArrayPool<byte>.Shared.RespExecutorBase.RequestBufferPool, supplied by the connection manager and forwarded by the database, batch and transaction executors), andRespPayloadOperation.Attachrents from it. The owner is released throughIDisposablewhen the last reference to the request goes, exactly once.ArrayPoolarray 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.Requests are hashed once, and only when the cache needs it.
RespRequesthashed its whole payload in the constructor, for every request, cached or not. Merely deferring that toGetHashCode()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:RespRequestFramecomputes the hash on first use. The cache's three entry points ask the frame (AsHashedLookupKey(), andframe.Hashfor the doorkeeper).Detach, bothCopyForCacheKeyoverloads (including the slab copy),ToOwnedandWithFlags. So everyEntryKeybuilt from them reuses it.Unsafe.AsRefwrite-back. The race is benign, since every thread writes the same value.ComputeHashmaps a real 0 to 1, and every path goes through it, so a carried hash and a computed one always agree.Equalsis 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
SETnever hashes its value. A hit hashes once, and so does a miss-and-store. Writes never reach the cache's lookup path (IsCacheablerequires 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.BufferedStreamWriterdeliberately 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, sodocs/Configuration.mdnow says what the option covers.Testing:
RequestBufferPoolTests, using a countingMemoryPool:SET(sync and async) stages in the configured pool;Ignoring the pool in
Attachfails both cases.RequestHashTests, using a DEBUG-only per-thread count of hash computations ([Conditional("DEBUG")], so it costs nothing in Release):SETwith caching on, and aCacheOptOutread, 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.mdnow steers local test runs to Debug, where DEBUG-only checks (asserts, RESPite's buffer leak tracking, instrumentation like this) actually run.v4without this change (InterpolatedCustomArgTests.AStructImplementerDoesNotBox,ExceptionFactoryTests.MultipleEndpointsThrowConnectionException).