perf: zero-copy republish path; document receive-path copy rationale#86
Merged
Conversation
niemyjski
commented
Jun 24, 2026
niemyjski
commented
Jun 24, 2026
niemyjski
commented
Jun 24, 2026
Co-authored-by: Blake Niemyjski <bniemyjski@gmail.com>
Per review feedback, removed the inline PERF/rationale comments on both the republish and receive paths. Also restores the .ToArray() copy in ConvertToMessage that was inadvertently dropped alongside the comment. RabbitMQ rents BasicDeliverEventArgs.Body from an ArrayPool and reclaims it once the async consumer callback returns. Subscribers run on separate Task.Run continuations and may capture the message and read message.Data after their handler returns, so the receive path must own a stable copy. The republish path stays zero-copy because the publish is fully awaited within the consumer callback while the pooled buffer is still valid.
Reference envelope.Body directly instead of copying via ToArray(). Safe because the body is deserialized synchronously within the awaited subscriber dispatch before the handler returns, per the handler-scoped IMessage.Data validity contract.
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.
Summary
Two improvements to
RabbitMQMessageBusfor theReadOnlyMemory<byte>transport contract introduced in FoundatioFx/Foundatio#530:PublishMessageAsyncnow acceptsReadOnlyMemory<byte>instead ofbyte[];RepublishMessageWithIncrementedDeliveryCountAsyncpassesenvelope.Bodydirectly with no.ToArray()copy.ToArray()copy inConvertToMessagewas previously unexplained; comment now accurately describes why the copy existsChange 1: Zero-copy republish path
RepublishMessageWithIncrementedDeliveryCountAsyncpreviously calledenvelope.Body.ToArray()to satisfy thebyte[]parameter. Now it passesenvelope.Bodydirectly — no copy.This is safe because the publish call is fully awaited within the receive callback, which is itself called before the channel returns the buffer to the pool.
Change 2: Accurate receive-path comment
The copy itself is unchanged and correct. RabbitMQ's
BasicDeliverEventArgs.Bodyis backed by a pooled buffer that is returned to the pool after the receive callback completes. Any subscriber that storesmessage.Datafor use beyond the handler (e.g. async logging, deferred processing) would read garbage from a recycled buffer without this copy.