Skip to content

Commit fc4dec5

Browse files
thomhurstclaude
andauthored
perf: cache CreateExecutableTestFactory delegate (#4292)
* chore: add .worktrees/ to gitignore Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * perf: cache CreateExecutableTestFactory delegate Cache the factory delegate instead of creating new lambdas on every property access. This avoids repeated allocations in the hot path during test execution. Fixes #4281 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 8875401 commit fc4dec5

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,7 @@ doc/plans/
435435
*speedscope*.json
436436

437437
# Dotnet trace files
438-
*.nettrace
438+
*.nettrace
439+
440+
# Git worktrees
441+
.worktrees/

TUnit.Core/TestMetadata`1.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class TestMetadata<
1414
{
1515
private Func<Type[], object?[], T>? _instanceFactory;
1616
private Func<T, object?[], Task>? _testInvoker;
17+
private Func<ExecutableTestCreationContext, TestMetadata, AbstractExecutableTest>? _cachedExecutableTestFactory;
1718

1819
/// <summary>
1920
/// Strongly typed instance factory
@@ -63,10 +64,16 @@ public override Func<ExecutableTestCreationContext, TestMetadata, AbstractExecut
6364
{
6465
get
6566
{
67+
// Return cached factory if available
68+
if (_cachedExecutableTestFactory != null)
69+
{
70+
return _cachedExecutableTestFactory;
71+
}
72+
6673
// For AOT mode, create delegates from the strongly-typed ones
6774
if (InstanceFactory != null && InvokeTypedTest != null)
6875
{
69-
return (context, metadata) =>
76+
_cachedExecutableTestFactory = (context, metadata) =>
7077
{
7178
var typedMetadata = (TestMetadata<T>)metadata;
7279

@@ -109,6 +116,8 @@ public override Func<ExecutableTestCreationContext, TestMetadata, AbstractExecut
109116
Context = context.Context
110117
};
111118
};
119+
120+
return _cachedExecutableTestFactory;
112121
}
113122

114123
throw new InvalidOperationException($"InstanceFactory and InvokeTypedTest must be set for {typeof(T).Name}");

0 commit comments

Comments
 (0)