Skip to content

Commit cd5418d

Browse files
perf: eliminate closure and Func creation
1 parent 543e703 commit cd5418d

6 files changed

+13
-6
lines changed

TUnit.Core/ContextProvider.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,20 @@ public AssemblyHookContext GetOrCreateAssemblyContext(Assembly assembly)
6363
/// <summary>
6464
/// Gets or creates a class context
6565
/// </summary>
66+
[UnconditionalSuppressMessage("Trimming", "IL2111",
67+
Justification = "Type parameter is annotated at the method boundary.")]
6668
public ClassHookContext GetOrCreateClassContext(
6769
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods)]
6870
Type classType)
6971
{
70-
return _classContexts.GetOrAdd(classType, type =>
72+
return _classContexts.GetOrAdd(classType, static ([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods)]
73+
type, state) =>
7174
{
72-
return new ClassHookContext(GetOrCreateAssemblyContext(classType.Assembly))
75+
return new ClassHookContext(state.GetOrCreateAssemblyContext(type.Assembly))
7376
{
74-
ClassType = classType
77+
ClassType = type
7578
};
76-
});
79+
}, this);
7780
}
7881

7982
/// <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
}

TUnit.PublicAPI/Tests.Core_Library_Has_No_API_Changes.DotNet10_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ namespace
419419
public .TestSessionContext TestSessionContext { get; }
420420
public .TestContext CreateTestContext(string testName, [.(..None | ..PublicParameterlessConstructor | ..PublicConstructors | ..PublicMethods | ..PublicProperties)] classType, .TestBuilderContext testBuilderContext, .CancellationToken cancellationToken) { }
421421
public .AssemblyHookContext GetOrCreateAssemblyContext(.Assembly assembly) { }
422+
[.("Trimming", "IL2111", Justification="Type parameter is annotated at the method boundary.")]
422423
public .ClassHookContext GetOrCreateClassContext([.(..None | ..PublicParameterlessConstructor | ..PublicConstructors | ..PublicMethods | ..PublicProperties)] classType) { }
423424
}
424425
public class CultureExecutor : .DedicatedThreadExecutor

TUnit.PublicAPI/Tests.Core_Library_Has_No_API_Changes.DotNet8_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ namespace
419419
public .TestSessionContext TestSessionContext { get; }
420420
public .TestContext CreateTestContext(string testName, [.(..None | ..PublicParameterlessConstructor | ..PublicConstructors | ..PublicMethods | ..PublicProperties)] classType, .TestBuilderContext testBuilderContext, .CancellationToken cancellationToken) { }
421421
public .AssemblyHookContext GetOrCreateAssemblyContext(.Assembly assembly) { }
422+
[.("Trimming", "IL2111", Justification="Type parameter is annotated at the method boundary.")]
422423
public .ClassHookContext GetOrCreateClassContext([.(..None | ..PublicParameterlessConstructor | ..PublicConstructors | ..PublicMethods | ..PublicProperties)] classType) { }
423424
}
424425
public class CultureExecutor : .DedicatedThreadExecutor

TUnit.PublicAPI/Tests.Core_Library_Has_No_API_Changes.DotNet9_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ namespace
419419
public .TestSessionContext TestSessionContext { get; }
420420
public .TestContext CreateTestContext(string testName, [.(..None | ..PublicParameterlessConstructor | ..PublicConstructors | ..PublicMethods | ..PublicProperties)] classType, .TestBuilderContext testBuilderContext, .CancellationToken cancellationToken) { }
421421
public .AssemblyHookContext GetOrCreateAssemblyContext(.Assembly assembly) { }
422+
[.("Trimming", "IL2111", Justification="Type parameter is annotated at the method boundary.")]
422423
public .ClassHookContext GetOrCreateClassContext([.(..None | ..PublicParameterlessConstructor | ..PublicConstructors | ..PublicMethods | ..PublicProperties)] classType) { }
423424
}
424425
public class CultureExecutor : .DedicatedThreadExecutor

TUnit.PublicAPI/Tests.Core_Library_Has_No_API_Changes.Net4_7.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ namespace
399399
public .TestSessionContext TestSessionContext { get; }
400400
public .TestContext CreateTestContext(string testName, classType, .TestBuilderContext testBuilderContext, .CancellationToken cancellationToken) { }
401401
public .AssemblyHookContext GetOrCreateAssemblyContext(.Assembly assembly) { }
402+
[.("Trimming", "IL2111", Justification="Type parameter is annotated at the method boundary.")]
402403
public .ClassHookContext GetOrCreateClassContext( classType) { }
403404
}
404405
public class CultureExecutor : .DedicatedThreadExecutor

0 commit comments

Comments
 (0)