Skip to content

Commit 3c6ccfc

Browse files
perf: elimiate Func creation in ExecuteAsync (#4460)
1 parent b597aa9 commit 3c6ccfc

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

TUnit.Core/Executors/DedicatedThreadExecutor.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ protected sealed override async ValueTask ExecuteAsync(Func<ValueTask> action)
1818

1919
var tcs = new TaskCompletionSource<object?>();
2020

21-
var thread = new Thread(() =>
21+
var thread = new Thread(static state =>
2222
{
23+
var (threadExecutor, action, tcs) = (ValueTuple<DedicatedThreadExecutor, Func<ValueTask>, TaskCompletionSource<object?>>)state!;
2324
Exception? capturedException = null;
2425

2526
try
2627
{
27-
Initialize();
28+
threadExecutor.Initialize();
2829

2930
try
3031
{
31-
ExecuteAsyncActionWithMessagePump(action, tcs);
32+
threadExecutor.ExecuteAsyncActionWithMessagePump(action, tcs);
3233
}
3334
catch (Exception e)
3435
{
@@ -41,7 +42,7 @@ protected sealed override async ValueTask ExecuteAsync(Func<ValueTask> action)
4142
}
4243
finally
4344
{
44-
CleanUp();
45+
threadExecutor.CleanUp();
4546

4647
if (capturedException != null && !tcs.Task.IsCompleted)
4748
{
@@ -50,8 +51,10 @@ protected sealed override async ValueTask ExecuteAsync(Func<ValueTask> action)
5051
}
5152
});
5253

54+
var state = (this, action, tcs);
55+
5356
ConfigureThread(thread);
54-
thread.Start();
57+
thread.Start(state);
5558

5659
await tcs.Task;
5760
}
@@ -409,7 +412,7 @@ public override SynchronizationContext CreateCopy()
409412
}
410413
}
411414

412-
public ValueTask OnTestRegistered(TestRegisteredContext context)
415+
public ValueTask OnTestRegistered(TestRegisteredContext context)
413416
{
414417
context.SetParallelLimiter(new ProcessorCountParallelLimit());
415418
return default(ValueTask);

0 commit comments

Comments
 (0)