Skip to content

perf: add Which continuations without closures - #1791

Merged
vbreuss merged 1 commit into
mainfrom
perf/throws-without-closures
Oct 11, 2026
Merged

vbreuss merged 1 commit into
mainfrom
perf/throws-without-closures

Conversation

@vbreuss

@vbreuss vbreuss commented Oct 11, 2026

Copy link
Copy Markdown
Member

Every ForWhich, e.g. behind Throws<T>(), HasSingle().Which or DoesNotThrow().WhoseResult, allocated two closures and two delegates to create the which node in place of the right-most operand, and Throws<T>() added a closure over the exception type for its accessor. The which node is now created from a state with a static lambda, and Throws<T>() shares one accessor per exception type.

Benchmark Before After
DoesNotThrow().WhoseResult.IsEqualTo(42) 771.8 ns / 1.48 KB 726.0 ns / 1.27 KB

Every `ForWhich`, e.g. behind `Throws<T>()`, `HasSingle().Which` or `DoesNotThrow().WhoseResult`, allocated two closures and two delegates to create the which node in place of the right-most operand, and `Throws<T>()` added a closure over the exception type for its accessor. The which node is now created from a state with a static lambda, and `Throws<T>()` shares one accessor per exception type.

| Benchmark | Before | After |
|---|---:|---:|
| `DoesNotThrow().WhoseResult.IsEqualTo(42)` | 771.8 ns / 1.48 KB | 726.0 ns / 1.27 KB |
| `HasSingle().Which.IsEqualTo(42)` | 522.8 ns / 1.27 KB | 522.7 ns / 1.05 KB |
| `Throws<InvalidOperationException>()` | 6609.4 ns / 1.96 KB | 6564.5 ns / 1.66 KB |
@vbreuss vbreuss self-assigned this Oct 11, 2026
@vbreuss
vbreuss enabled auto-merge (squash) October 11, 2026 09:01
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

     91 files       91 suites   1h 59m 48s ⏱️
 37 523 tests  37 523 ✅ 0 💤 0 ❌
217 902 runs  217 902 ✅ 0 💤 0 ❌

Results for commit feefb46.

@vbreuss
vbreuss merged commit ad09d2c into main Oct 11, 2026
16 checks passed
@vbreuss
vbreuss deleted the perf/throws-without-closures branch October 11, 2026 10:04
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.5 LTS (Noble Numbat)
INTEL XEON PLATINUM 8573C 2.30GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.303
[Host] : .NET 8.0.31 (8.0.31, 8.0.3126.42015), X64 RyuJIT x86-64-v4

Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=1 WarmupCount=10

Method Mean vs aweXpect Error StdDev Gen0 Gen1 Allocated vs aweXpect
Bool_aweXpect 126.9 ns 3.23 ns 3.02 ns 0.0052 - 440 B
Bool_FluentAssertions 245.9 ns +94% 3.58 ns 2.99 ns 0.0110 - 952 B +116%
Bool_TUnit 160.3 ns +26% 4.93 ns 4.61 ns 0.0055 - 472 B +7%
Equivalency_aweXpect 80,627.7 ns 832.85 ns 738.30 ns 0.1221 - 16440 B
Equivalency_FluentAssertions 1,906,279.5 ns +2264% 41,786.90 ns 39,087.49 ns 56.6406 9.7656 4841611 B +29350%
Equivalency_TUnit 187,568.4 ns +133% 3,259.76 ns 3,049.18 ns 1.7090 - 155992 B +849%
EventTriggered_aweXpect 636.7 ns 9.10 ns 8.51 ns 0.0219 - 1840 B
EventTriggered_FluentAssertions 30,701.5 ns +4722% 410.14 ns 363.58 ns 0.0610 - 5256 B +186%
Int_GreaterThan_aweXpect 138.5 ns 2.04 ns 1.91 ns 0.0064 - 544 B
Int_GreaterThan_FluentAssertions 240.2 ns +73% 3.85 ns 3.60 ns 0.0143 - 1224 B +125%
Int_GreaterThan_TUnit 192.1 ns +39% 5.34 ns 4.99 ns 0.0091 - 776 B +43%
ItemsCount_AtLeast_aweXpect 274.6 ns 7.16 ns 6.70 ns 0.0072 - 632 B
ItemsCount_AtLeast_FluentAssertions 465.7 ns +70% 8.26 ns 7.72 ns 0.0238 - 2008 B +218%
ItemsCount_AtLeast_TUnit 364.5 ns +33% 5.78 ns 5.41 ns 0.0124 - 1064 B +68%
Satisfies_aweXpect 179.4 ns 3.89 ns 3.64 ns 0.0069 - 584 B
Satisfies_FluentAssertions 15,571.1 ns +8579% 243.10 ns 227.39 ns 0.0610 0.0458 5614 B +861%
Satisfies_TUnit 197.2 ns +10% 4.29 ns 3.80 ns 0.0083 - 712 B +22%
String_aweXpect 235.3 ns 4.85 ns 4.54 ns 0.0072 - 608 B
String_FluentAssertions 1,101.7 ns +368% 17.01 ns 15.92 ns 0.0458 - 3944 B +549%
String_TUnit 289.1 ns +23% 4.79 ns 4.48 ns 0.0105 - 912 B +50%
StringArray_aweXpect 567.7 ns 5.67 ns 4.73 ns 0.0105 - 896 B
StringArray_FluentAssertions 1,238.3 ns +118% 21.73 ns 19.26 ns 0.0496 - 4152 B +363%
StringArray_TUnit 555.3 ns -2% 15.80 ns 14.78 ns 0.0143 - 1264 B +41%
StringArrayInAnyOrder_aweXpect 815.5 ns 12.92 ns 12.08 ns 0.0153 - 1304 B
StringArrayInAnyOrder_FluentAssertions 15,372.1 ns +1785% 155.27 ns 145.24 ns 0.3967 - 33465 B +2466%
StringArrayInAnyOrder_TUnit 662.3 ns -19% 10.76 ns 9.54 ns 0.0162 - 1408 B +8%

github-actions Bot added a commit that referenced this pull request Oct 11, 2026
github-actions Bot added a commit that referenced this pull request Oct 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This is addressed in release v3.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state: released The issue is released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant