perf(session): optimise ProducerBuffer — swap field roles#1674
Open
Tehsmash wants to merge 3 commits into
Open
perf(session): optimise ProducerBuffer — swap field roles#1674Tehsmash wants to merge 3 commits into
Tehsmash wants to merge 3 commits into
Conversation
Contributor
|
The latest Buf updates on your PR. Results from workflow ci-buf / buf (pull_request).
|
57de4ca to
c08522f
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
c08522f to
2e71ff9
Compare
micpapal
requested changes
May 29, 2026
| self.ring[self.next] = id; | ||
| self.next = (self.next + 1) % self.capacity; | ||
| true | ||
| } |
Member
There was a problem hiding this comment.
Here we can drop the return value as we always return true
90c7ad0 to
389cb3b
Compare
Replace the Vec<Option<Message>> ring + HashMap<usize,usize> index layout with HashMap<usize,Message> (messages) + Vec<usize> ring (ring). The key hot-path gain is in push(): evicting the oldest entry no longer requires touching the large Message struct — we read a single usize from the compact ring Vec (512 × 8 B = 4 KB, L1-resident) instead of dereferencing a Vec<Option<Message>> slot to call .get_id(). Additional wins: - get(): one fewer indirection (map lookup → clone, not map → vec → clone) - clear(): HashMap::clear() only; no vec![None; capacity] reallocation - iter(): ring-ordered walk preserving insertion order; cold path only Signed-off-by: Sam Betts <1769706+Tehsmash@users.noreply.github.com>
`push()` always returned `true`; no caller used the value. Change the return type to `()` as suggested in PR review. Signed-off-by: Sam Betts <1769706+Tehsmash@users.noreply.github.com>
Replace assert!(messages.push(...)) with plain push() calls now that push() returns () instead of bool. Signed-off-by: Sam Betts <1769706+Tehsmash@users.noreply.github.com>
389cb3b to
6f8a6ce
Compare
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.
Description
ProducerBufferis the fixed-capacity ring buffer used in the reliable-mode hot path to store messages for retransmission. The previous layout kept fullMessageobjects in aVec<Option<Message>>ring and message IDs in aHashMap<usize, usize>index.The bottleneck in
push()was the eviction step: to evict the oldest slot we had to readbuffer[self.next]— a large protobuf struct unlikely to be cache-warm — just to extract its.get_id().This PR swaps the field roles:
The
ringVec is 512 × 8 B = 4 KB — fits entirely in L1. Eviction now reads a single integer instead of dereferencing a large struct.Per-operation impact:
pushevictionVec<Option<Message>>[next]→.get_id()Vec<usize>[next]— integer, L1-hotgetclearvec![None; capacity]HashMap::clear()only, no allocationiterOptionunwrapiter()is only called on the cold flush-on-connect path so its slightly more complex traversal has no hot-path impact.All call sites in
session_sender.rsuse the same public API (push,get,iter,clear) — no changes needed outsideproducer_buffer.rs.Type of Change
Performance optimisation — no behaviour change.
Checklist