perf: defer allocations in event receivers and hook executors#4306
Merged
Conversation
- Defer List<object> allocation in RegisterReceivers until needed - Use single TryAdd operation instead of Contains + Add pattern - Defer List<Exception> allocations in after-hook methods until exception occurs - Return empty collection literal [] instead of pre-allocated list when no items These changes reduce GC pressure by avoiding unnecessary allocations in the common case where no exceptions occur and objects are already registered. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Owner
Author
SummaryPerformance optimization that defers allocations in event receivers and hook executors until actually needed. Critical IssuesNone found ✅ SuggestionsNone - the changes are well-implemented and follow TUnit's performance-first principles. AnalysisThis PR optimizes hot paths in the TUnit engine by applying lazy allocation patterns: EventReceiverOrchestrator.cs (TUnit.Engine/Services/EventReceiverOrchestrator.cs:41-82)
Both files - Exception collection pattern (TUnit.Engine/Services/EventReceiverOrchestrator.cs:148-196, TUnit.Engine/Services/HookExecutor.cs:65-330)
TUnit Rules Compliance
The changes are well-justified by CPU profiling data (0.36% exclusive time in RegisterReceivers) and target the common case where hooks execute successfully without exceptions. Verdict✅ APPROVE - No critical issues. Solid performance optimization following TUnit's engineering principles. |
This was referenced Jan 12, 2026
This was referenced Feb 13, 2026
This was referenced Feb 23, 2026
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
List<object>allocation inRegisterReceiversuntil actually neededTryAddoperation instead ofContains + Addpattern (reduces dictionary lookups)List<Exception>allocations in after-hook methods until an exception actually occurs[]instead of pre-allocated list when no itemsRationale
CPU profiling with
dotnet traceidentifiedEventReceiverOrchestrator.RegisterReceiversas taking 0.36% exclusive time. The changes reduce GC pressure by avoiding unnecessary allocations in the common case where:Test plan
🤖 Generated with Claude Code