perf(consumer): build processor chain once per consumer#50
Merged
Conversation
The processor decorator chain (8 objects + the deserializer) was rebuilt on every delivery inside the consume callback. Every processor and the deserializer are stateless across messages — the only per-message input is the RabbitMQMessage passed to consume() — so the chain can be constructed once per consumer and reused. Hoisting it out of the hot path removes 8 allocations per consumed message, leaving only the unavoidable RabbitMQMessage allocation. Behavior is unchanged; the last-resort try/catch around consume() is preserved.
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.
What
The processor decorator chain (the 8-deep
RunMQExceptionLoggerProcessor → … → RunMQBaseProcessorstack plus aDefaultDeserializer) was being constructed inside the consume callback, on every delivery. Every processor and the deserializer are stateless across messages — the only per-message input is theRabbitMQMessagehanded toconsume()— so the chain can be built once per consumer and reused.This moves the construction above the
consumerChannel.consume(...)call. The per-message hot path now allocates only the unavoidableRabbitMQMessage.Why
Removes 8 object allocations per consumed message. Behavior is identical:
try/catcharoundconsume()is preservedPerformance
Benchmarked before/after with the consumer-side scenarios (
consume,concurrent,reliability, mean of 3 runs each). Throughput is unchanged within run-to-run noise — at 16–20k msg/s the bottleneck is broker round-trips / simulated work, and the removed objects are cheap short-lived young-gen allocations.So this is a cleanliness / GC-pressure reduction, not a throughput win — it shouldn't be sold as the latter. The value is fewer allocations per message under sustained high-throughput or memory-constrained loads.
Tests
Full suite green: 149 unit + 85 e2e passing.