Skip to content

Commit 054355b

Browse files
TimothyMakkisonthomhurst
authored andcommitted
perf: eliminate closure and Func creation
1 parent c882eff commit 054355b

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

TUnit.Core/ContextProvider.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,20 @@ public AssemblyHookContext GetOrCreateAssemblyContext(Assembly assembly)
6161
/// <summary>
6262
/// Gets or creates a class context
6363
/// </summary>
64+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2111",
65+
Justification = "Type parameter is annotated at the method boundary.")]
6466
public ClassHookContext GetOrCreateClassContext(
6567
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods)]
6668
Type classType)
6769
{
68-
return _classContexts.GetOrAdd(classType, type =>
70+
return _classContexts.GetOrAdd(classType, static ([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods)]
71+
type, state) =>
6972
{
70-
return new ClassHookContext(GetOrCreateAssemblyContext(classType.Assembly))
73+
return new ClassHookContext(state.GetOrCreateAssemblyContext(type.Assembly))
7174
{
72-
ClassType = classType
75+
ClassType = type
7376
};
74-
});
77+
}, this);
7578
}
7679

7780
/// <summary>

TUnit.Engine/Discovery/ReflectionTestDataCollector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ public async IAsyncEnumerable<TestMetadata> CollectTestsStreamingAsync(
170170
// Stream tests from this assembly
171171
await foreach (var test in DiscoverTestsInAssemblyStreamingAsync(assembly, cancellationToken))
172172
{
173-
ImmutableInterlocked.Update(ref _discoveredTests, list => list.Add(test));
173+
ImmutableInterlocked.Update(ref _discoveredTests, static (list, test) => list.Add(test), test);
174174
yield return test;
175175
}
176176
}
177177

178178
// Stream dynamic tests
179179
await foreach (var dynamicTest in DiscoverDynamicTestsStreamingAsync(testSessionId, cancellationToken))
180180
{
181-
ImmutableInterlocked.Update(ref _discoveredTests, list => list.Add(dynamicTest));
181+
ImmutableInterlocked.Update(ref _discoveredTests, static (list, dynamicTest) => list.Add(dynamicTest), dynamicTest);
182182
yield return dynamicTest;
183183
}
184184
}

0 commit comments

Comments
 (0)