From bd8b1d20b5e2b8cbebf0319de7ec6c3517aa527c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sun, 11 Oct 2026 12:04:05 +0200 Subject: [PATCH] perf: evaluate the expectations of `ThatAll` and `ThatAny` without a task each The result of every combined expectation and of the combination itself was returned as a `Task`, which allocates even when the evaluation completes synchronously, because the result is a struct that cannot be cached. | Benchmark | Before | After | |---|---:|---:| | `ThatAll` of 3 met expectations (run 1) | 551.8 ns / 2.08 KB | 594.5 ns / 1.73 KB | | `ThatAll` of 3 met expectations (run 2) | 570.9 ns / 2.08 KB | 581.1 ns / 1.73 KB | | `ThatAny` of 2 expectations (run 1) | 535.6 ns / 1.48 KB | 480.0 ns / 1.23 KB | | `ThatAny` of 2 expectations (run 2) | 461.7 ns / 1.48 KB | 490.7 ns / 1.23 KB | The times differ within the noise between runs; the allocations are lower in both. --- Source/aweXpect.Core/Core/Expectation.cs | 4 ++-- Source/aweXpect.Core/Results/ExpectationResult.cs | 4 ++-- Tests/aweXpect.Core.Tests/ExpectTests.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/aweXpect.Core/Core/Expectation.cs b/Source/aweXpect.Core/Core/Expectation.cs index 7734bc930d..d6abc791e5 100644 --- a/Source/aweXpect.Core/Core/Expectation.cs +++ b/Source/aweXpect.Core/Core/Expectation.cs @@ -71,7 +71,7 @@ public override int GetHashCode() public override string? ToString() => base.ToString(); - internal abstract Task GetResult(int index); + internal abstract ValueTask GetResult(int index); /// /// Ends the evaluation, once the failure message no longer reads the collections it materialized. @@ -171,7 +171,7 @@ public TaskAwaiter GetAwaiter() protected abstract Outcome CheckOutcome(Outcome? previous, Outcome current); /// - internal override async Task GetResult(int index) + internal override async ValueTask GetResult(int index) { (Expectation Expectation, Result Result)[] results = new (Expectation, Result)[_expectations.Length]; Exception? failureCause = null; diff --git a/Source/aweXpect.Core/Results/ExpectationResult.cs b/Source/aweXpect.Core/Results/ExpectationResult.cs index 2ac90cddb9..fdd86c50c7 100644 --- a/Source/aweXpect.Core/Results/ExpectationResult.cs +++ b/Source/aweXpect.Core/Results/ExpectationResult.cs @@ -141,7 +141,7 @@ public TaskAwaiter GetAwaiter() /// /// The combination does not trace its members, so a met expectation is traced here. /// - internal override async Task GetResult(int index) + internal override async ValueTask GetResult(int index) { ConstraintResult result = await ExpectationBuilder.IsMet(false); if (result.Outcome == Outcome.Success && ExpectationBuilder.IsTracing) @@ -368,7 +368,7 @@ public TSelf WithTimeout(TimeSpan timeout) /// /// The combination does not trace its members, so a met expectation is traced here. /// - internal override async Task GetResult(int index) + internal override async ValueTask GetResult(int index) { ConstraintResult result = await ExpectationBuilder.IsMet(false); if (result.Outcome == Outcome.Success && ExpectationBuilder.IsTracing) diff --git a/Tests/aweXpect.Core.Tests/ExpectTests.cs b/Tests/aweXpect.Core.Tests/ExpectTests.cs index ebadc3c4c3..a72747a2fe 100644 --- a/Tests/aweXpect.Core.Tests/ExpectTests.cs +++ b/Tests/aweXpect.Core.Tests/ExpectTests.cs @@ -898,8 +898,8 @@ private sealed class MyHolder(MyClass inner) private sealed class MyExpectation(Expectation.Result result, params ResultContext[] contexts) : Expectation { - internal override Task GetResult(int index) - => Task.FromResult(new Result(result.Index, result.SubjectLine, + internal override ValueTask GetResult(int index) + => new(new Result(result.Index, result.SubjectLine, new WithContexts(result.ConstraintResult, contexts))); internal override Task EndEvaluation()