Skip to content

Conversation

@TimothyMakkison
Copy link
Contributor

Small change avoid Action creation by using a static lambda and manually boxing state.]

Before

image

After

image

@TimothyMakkison TimothyMakkison changed the title perf: elimiate Func creation in ExecuteAsync perf: eliminate Func creation in ExecuteAsync Jan 17, 2026
@thomhurst
Copy link
Owner

Summary

Eliminates delegate allocation in DedicatedThreadExecutor.ExecuteAsync by using a static lambda with manually boxed state.

Critical Issues

None found ✅

Suggestions

None - this is a clean performance optimization that aligns with TUnit's "Performance First" principle.

Analysis

The change converts a capturing lambda (which allocates a closure object) to a static lambda with explicit state passing:

  • All captured variables (this, action, tcs) are correctly passed through the tuple
  • Thread-safety is maintained - the tuple is created before thread.Start(state)
  • The optimization is meaningful since DedicatedThreadExecutor is used for STAThread tests and dedicated thread execution, making this a hot path
  • AOT-compatible and uses modern C# patterns
  • Bonus: Fixes indentation on line 412

The benchmark screenshots show measurable improvement in allocations, which directly supports TUnit's performance-first philosophy.

Verdict

APPROVE - No critical issues

@thomhurst thomhurst merged commit 3c6ccfc into thomhurst:main Jan 17, 2026
9 of 10 checks passed
This was referenced Jan 18, 2026
This was referenced Jan 19, 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