Skip to content

Conversation

@TimothyMakkison
Copy link
Contributor

@TimothyMakkison TimothyMakkison commented Jan 21, 2026

C# compiler like to eagerly initialise closures at the start of the method, so even on fast paths the display class for valueFactory was being created. Extracting the slow code to a new method solves this issue.

I have no idea why the .NET compiler does this, I assume it's a correctness thing (sometimes due if else usage). I really wish it didn't do this.

Before

image

After

image

@thomhurst
Copy link
Owner

Summary

Extracts slow path logic in ThreadSafeDictionary.GetOrAdd to a separate method to prevent eager closure allocation on the fast path.

Critical Issues

None found ✅

Suggestions

None - this is a clean micro-optimization that avoids unnecessary allocations on the hot path (key exists case) without changing behavior.

Verdict

APPROVE - No critical issues


Review Notes:

  • Performance optimization aligns with TUnit's "Performance First" principle
  • No public API changes, so no snapshot updates needed
  • Change is localized to a single method with clear intent
  • The extracted method preserves the original slow-path logic exactly

@thomhurst thomhurst enabled auto-merge (squash) January 23, 2026 07:19
@thomhurst thomhurst merged commit 0a40259 into thomhurst:main Jan 23, 2026
8 of 10 checks passed
This was referenced Jan 26, 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