Skip to content

Conversation

@TimothyMakkison
Copy link
Contributor

  • Make methods synchronous
  • Use ValueListBuilder in RegisterReceivers

Before

image

After

image

Fun fact: Attribute.Equals and Attribute.GetHashCode allocate a FieldInfo[] each time they are called.

@thomhurst
Copy link
Owner

Summary

Performance optimization using ValueListBuilder for allocation reduction and making methods synchronous.

Critical Issues

Missing try-finally in RegisterReceivers (line 41-77): The ValueListBuilder is only disposed if no exception occurs. This could leak array pool buffers.

Verdict

REQUEST CHANGES - Need try-finally block around VLB usage.

@thomhurst
Copy link
Owner

Detailed Fix

In EventReceiverOrchestrator.RegisterReceivers, the ValueListBuilder needs try-finally:

Before: vlb.Dispose() only runs if no exception occurs
After: Wrap the usage in try-finally to guarantee disposal

This prevents leaking array pool buffers when exceptions occur during event object enumeration or registration.

Performance Notes

The changes are excellent for performance:

  • ValueListBuilder reduces allocations vs List
  • Synchronous methods avoid async state machine overhead
  • Early exit checks prevent allocations when no receivers exist

The only minor point is the duplicated early-exit logic in InvokeTestEndEventReceiversAsync, but the performance benefit likely justifies it.

@thomhurst thomhurst merged commit adf1c65 into thomhurst:main Jan 15, 2026
9 of 10 checks passed
This was referenced Jan 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants