Skip to content

perf: keep an Action or Func<Task> subject without a wrapping closure - #1785

Merged
vbreuss merged 1 commit into
mainfrom
perf/delegate-subjects-without-wrappers
Oct 11, 2026
Merged

vbreuss merged 1 commit into
mainfrom
perf/delegate-subjects-without-wrappers

Conversation

@vbreuss

@vbreuss vbreuss commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

Wrapping the delegate in a lambda that ignores the cancellation token allocated a closure and a delegate for every Expect.That(() => ...), which is how most Throws and DoesNotThrow expectations start.

Benchmark Before After
DoesNotThrow on an Action 222.1 ns / 584 B 209.8 ns / 504 B
DoesNotThrow on a Func<Task> 223.2 ns / 592 B 205.9 ns / 512 B
Throws<InvalidOperationException> on an Action 5589.0 ns / 1792 B 5511.3 ns / 1712 B

@vbreuss vbreuss self-assigned this Oct 11, 2026
…sure

Wrapping the delegate in a lambda that ignores the cancellation token allocated a closure and a delegate for every `Expect.That(() => ...)`, which is how most `Throws` and `DoesNotThrow` expectations start.

| Benchmark | Before | After |
|---|---:|---:|
| `DoesNotThrow` on an `Action` | 222.1 ns / 584 B | 209.8 ns / 504 B |
| `DoesNotThrow` on a `Func<Task>` | 223.2 ns / 592 B | 205.9 ns / 512 B |
| `Throws<InvalidOperationException>` on an `Action` | 5589.0 ns / 1792 B | 5511.3 ns / 1712 B |
@vbreuss
vbreuss force-pushed the perf/delegate-subjects-without-wrappers branch from 8b43c47 to caab307 Compare October 11, 2026 08:28
@vbreuss
vbreuss enabled auto-merge (squash) October 11, 2026 08:42
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

     91 files  ±0       91 suites  ±0   1h 46m 19s ⏱️ - 3m 14s
 37 526 tests  - 5   37 526 ✅  - 5  0 💤 ±0  0 ❌ ±0 
217 902 runs  ±0  217 902 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit caab307. ± Comparison against base commit 7790486.

This pull request removes 4962 and adds 4885 tests. Note that renamed tests count towards both.

	
	    get;
	"a": 1
	await
	{
	}, 1)


, 2)

b, 2, False)
…
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(355f66b0-48a0-4abf-994b-ebd70cb4bac8)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(685234b0-68d8-43a1-a4e0-6526cf294925)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(a469a719-ad2d-4a33-ad32-7bbf7600b00d)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(c63ae8d2-5ac1-4435-9ecf-7ee6ea192693)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(d34961e1-74bf-43e5-af6b-9d6bbddea198)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(ebf27f69-a75b-49e6-a517-35645e94e01f)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(eeba5d4e-be5b-436e-8eb6-24ace399dfc8)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(23040a86-fafa-4af7-be19-1b021a7e4cec)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(35f78c84-0c73-468f-8384-26c5aec627a8)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(5b7bd49a-a0bd-4721-b03b-0a950c44f316)
…

@vbreuss
vbreuss merged commit d79dbb7 into main Oct 11, 2026
16 checks passed
@vbreuss
vbreuss deleted the perf/delegate-subjects-without-wrappers branch October 11, 2026 09:53
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.5 LTS (Noble Numbat)
AMD EPYC 9V45 2.60GHz, 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 68.32 ns 1.442 ns 1.279 ns 0.0262 - 440 B
Bool_FluentAssertions 132.16 ns +93% 1.932 ns 1.713 ns 0.0567 - 952 B +116%
Bool_TUnit 105.21 ns +54% 1.880 ns 1.667 ns 0.0281 - 472 B +7%
Equivalency_aweXpect 59,618.43 ns 194.556 ns 172.469 ns 0.9766 - 16440 B
Equivalency_FluentAssertions 1,241,659.54 ns +1983% 12,343.424 ns 10,942.124 ns 289.0625 42.9688 4841647 B +29350%
Equivalency_TUnit 105,374.78 ns +77% 803.592 ns 671.036 ns 9.2773 0.6104 155992 B +849%
EventTriggered_aweXpect 361.70 ns 4.536 ns 3.788 ns 0.1097 - 1840 B
EventTriggered_FluentAssertions 28,730.17 ns +7843% 479.724 ns 400.591 ns 0.3052 0.2441 5256 B +186%
Int_GreaterThan_aweXpect 76.78 ns 1.716 ns 1.606 ns 0.0324 - 544 B
Int_GreaterThan_FluentAssertions 123.34 ns +61% 1.683 ns 1.492 ns 0.0730 - 1224 B +125%
Int_GreaterThan_TUnit 125.93 ns +64% 5.120 ns 4.789 ns 0.0463 - 776 B +43%
ItemsCount_AtLeast_aweXpect 139.18 ns 3.499 ns 3.273 ns 0.0377 - 632 B
ItemsCount_AtLeast_FluentAssertions 273.39 ns +96% 4.139 ns 3.669 ns 0.1197 - 2008 B +218%
ItemsCount_AtLeast_TUnit 201.96 ns +45% 1.766 ns 1.652 ns 0.0634 - 1064 B +68%
Satisfies_aweXpect 99.76 ns 1.988 ns 1.859 ns 0.0348 - 584 B
Satisfies_FluentAssertions 9,466.67 ns +9390% 143.280 ns 119.645 ns 0.3357 0.2899 5615 B +861%
Satisfies_TUnit 123.40 ns +24% 0.565 ns 0.441 ns 0.0424 - 712 B +22%
String_aweXpect 135.35 ns 0.984 ns 0.872 ns 0.0362 - 608 B
String_FluentAssertions 634.42 ns +369% 9.566 ns 8.948 ns 0.2356 0.0010 3944 B +549%
String_TUnit 170.33 ns +26% 0.449 ns 0.398 ns 0.0544 - 912 B +50%
StringArray_aweXpect 327.33 ns 3.156 ns 2.797 ns 0.0534 - 896 B
StringArray_FluentAssertions 650.23 ns +99% 2.174 ns 2.034 ns 0.2480 - 4152 B +363%
StringArray_TUnit 337.50 ns +3% 5.058 ns 4.731 ns 0.0753 - 1264 B +41%
StringArrayInAnyOrder_aweXpect 539.57 ns 6.195 ns 5.173 ns 0.0772 - 1304 B
StringArrayInAnyOrder_FluentAssertions 10,387.94 ns +1825% 83.204 ns 64.960 ns 1.9989 0.0610 33471 B +2467%
StringArrayInAnyOrder_TUnit 456.05 ns -15% 4.795 ns 4.004 ns 0.0839 - 1408 B +8%

github-actions Bot added a commit that referenced this pull request Oct 11, 2026
…subject without a wrapping closure (#1785) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Oct 11, 2026
…subject without a wrapping closure (#1785) by Valentin Breuß
@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