You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The CreateExecutableTestFactory property getter in TestMetadata<T> creates new lambda closures every time it is accessed, causing unnecessary heap allocations and GC pressure.
Evidence
From dotnet-trace profiling of the TUnit.PerformanceBenchmarks project (3700+ tests):
<get_CreateExecutableTestFactory>b__2 shows 5.41% inclusive CPU time
<set_InstanceFactory>b__0 shows 2.2% inclusive CPU time
Root Cause
In TUnit.Core/TestMetadata\1.cs` lines 62-116, the property getter creates new lambdas on every access:
publicoverrideFunc<ExecutableTestCreationContext,TestMetadata,AbstractExecutableTest>CreateExecutableTestFactory{get{if(InstanceFactory!=null&&InvokeTypedTest!=null){return(context,metadata)=>// NEW LAMBDA EVERY ACCESS!{vartypedMetadata=(TestMetadata<T>)metadata;Func<TestContext,Task<object>>createInstance=async testContext =>{ ...};Func<object,object?[],TestContext,CancellationToken,Task>invokeTest=async(...)=>{ ...};returnnewExecutableTest(createInstance,invokeTest){ ...};};}thrownewInvalidOperationException(...);}}
Summary
The
CreateExecutableTestFactoryproperty getter inTestMetadata<T>creates new lambda closures every time it is accessed, causing unnecessary heap allocations and GC pressure.Evidence
From dotnet-trace profiling of the TUnit.PerformanceBenchmarks project (3700+ tests):
<get_CreateExecutableTestFactory>b__2shows 5.41% inclusive CPU time<set_InstanceFactory>b__0shows 2.2% inclusive CPU timeRoot Cause
In
TUnit.Core/TestMetadata\1.cs` lines 62-116, the property getter creates new lambdas on every access:Suggested Fix
Cache the delegate after first creation:
Impact
Related Files
TUnit.Core/TestMetadata\1.cs`