perf: improve Assertion.ExecuteCore state machine generation#4328
perf: improve Assertion.ExecuteCore state machine generation#4328thomhurst merged 1 commit intothomhurst:mainfrom
Assertion.ExecuteCore state machine generation#4328Conversation
SummaryPerformance optimization that reduces state machine size for Critical IssuesSyntax typo: Line 141 has a double semicolon: var result = await CreateMetadataAndCheckAsync(contextResult.Value, contextResult.Exception);;Should be a single semicolon. SuggestionsNone - the refactoring approach is sound. The stack trace concern mentioned in the PR description is a non-issue since TUnit Rules Check
Verdict |
Fixed |
551f62b to
c341217
Compare
SummaryRefactors Critical IssuesSyntax Error - TUnit.Assertions/Core/Assertion.cs:141 var result = await CreateMetadataAndCheckAsync(contextResult.Value, contextResult.Exception);;Double semicolon will cause compilation failure. Remove one semicolon. SuggestionsStacktrace Impact - TUnit.Assertions/Core/Assertion.cs:149
Consider verifying with a test that exception messages/stacks remain useful for debugging. Method Naming - TUnit.Assertions/Core/Assertion.cs:153 Previous Review StatusUnable to fetch previous comments due to API permissions. Verdict |
c341217 to
1c7e0c4
Compare
Forgot to force push mb, renamed to Before I forget this optimisation could be applied to the following in a future PR if (Context.PendingPreWork != null)
{
var preWork = Context.PendingPreWork;
Context.PendingPreWork = null; // Clear BEFORE execution to prevent re-entry
await preWork();
}
// If this is an And/OrAssertion (composite), delegate to AssertAsync which has custom logic
if (this is Chaining.AndAssertion<TValue> or Chaining.OrAssertion<TValue>)
{
return await AssertAsync();
} |
SummaryOptimizes async state machine generation for Critical IssuesNone found ✅ Suggestions1. Minor Inaccuracy in PR Description 2. Consider Adding a Comment // Extracted to separate method to reduce async state machine size
// by avoiding DateTimeOffset fields in the main state machine
private Task<AssertionResult> CreateMetadataAndCheckAsync(...)TUnit Rules Compliance
Performance AnalysisThe refactoring extracts metadata creation and exception handling into separate scopes to prevent the C# compiler from hoisting fields into the main async state machine. This is a valid micro-optimization for a hot execution path (every assertion invocation). The tuple non-destructuring optimization is also valid - the compiler can sometimes generate more efficient code when avoiding tuple pattern matching. Verdict✅ APPROVE - Valid performance optimization with no critical issues. The code is functionally equivalent and correctly optimizes state machine generation for this hot path. |
Improve
Assertion.ExecuteCoreAsyncstate machine generation by moving logic into external methods to reduce the number of fields in the state machine.ValueTuple<TValue, Exception>) fields despite my best attemptsExecuteCoreHandleExceptionwill change the thrown exception stacktrace, although I don't think this affects the user.Before
After