Skip to content

perf: bind the value accessor of ContainsKey only for WhoseValue - #1820

Merged
vbreuss merged 1 commit into
mainfrom
perf/dictionary-key-without-closure
Oct 11, 2026
Merged

vbreuss merged 1 commit into
mainfrom
perf/dictionary-key-without-closure

Conversation

@vbreuss

@vbreuss vbreuss commented Oct 11, 2026

Copy link
Copy Markdown
Member

ContainsKey and ContainsKeys created a closure over the expected keys for every expectation, although only WhoseValue and WhoseValues use it. The accessor now gets the keys as an argument and is bound to them when WhoseValue(s) is used.

ABBA, mean of two runs per side:

Method Before After Allocated before / after
ContainsKey("b") 157.5 ns 145.0 ns 496 B / 408 B

The time difference of the last two is within the noise; the allocation is stable.

`ContainsKey` and `ContainsKeys` created a closure over the expected keys for every expectation, although only `WhoseValue` and `WhoseValues` use it. The accessor now gets the keys as an argument and is bound to them when `WhoseValue(s)` is used.

ABBA, mean of two runs per side:

| Method | Before | After | Allocated before / after |
|---|---:|---:|---:|
| `ContainsKey("b")` | 157.5 ns | 145.0 ns | 496 B / 408 B |
| `ContainsKey("b").WhoseValue.IsEqualTo(2)` | 588.2 ns | 594.7 ns | 1,136 B / 1,112 B |
| `ContainsKeys("a", "b")` | 951.4 ns | 927.7 ns | 1,768 B / 1,680 B |

The time difference of the last two is within the noise; the allocation is stable.
@vbreuss vbreuss self-assigned this Oct 11, 2026
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

     91 files       91 suites   2h 14m 9s ⏱️
 37 541 tests  37 541 ✅ 0 💤 0 ❌
218 070 runs  218 070 ✅ 0 💤 0 ❌

Results for commit 00b1573.

@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 4.47GHz, 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
Contains_aweXpect 242.06 ns 12.206 ns 11.417 ns 0.0429 - 720 B
Contains_FluentAssertions 248.29 ns +3% 5.866 ns 5.200 ns 0.0958 - 1608 B +123%
Contains_TUnit 227.43 ns -6% 4.782 ns 4.473 ns 0.0505 - 848 B +18%
Equivalency_aweXpect 6,437.58 ns 123.806 ns 103.383 ns 0.1755 - 3040 B
Equivalency_FluentAssertions 179,898.89 ns +2695% 1,178.019 ns 919.720 ns 35.1563 1.9531 590288 B +19317%
Equivalency_TUnit 10,463.05 ns +63% 157.729 ns 131.711 ns 0.9308 - 15680 B +416%
EventTriggered_aweXpect 381.77 ns 6.775 ns 5.290 ns 0.1101 0.0005 1848 B
EventTriggered_FluentAssertions 30,300.18 ns +7837% 939.140 ns 832.523 ns 0.3052 0.2441 5256 B +184%
Int_GreaterThan_aweXpect 82.08 ns 2.150 ns 1.906 ns 0.0305 - 512 B
Int_GreaterThan_FluentAssertions 148.56 ns +81% 7.750 ns 7.249 ns 0.0730 - 1224 B +139%
Int_GreaterThan_TUnit 140.89 ns +72% 3.513 ns 3.286 ns 0.0463 - 776 B +52%
ItemsCount_AtLeast_aweXpect 150.27 ns 4.141 ns 3.458 ns 0.0358 - 600 B
ItemsCount_AtLeast_FluentAssertions 280.17 ns +86% 6.806 ns 5.684 ns 0.1197 - 2008 B +235%
ItemsCount_AtLeast_TUnit 224.37 ns +49% 8.547 ns 7.995 ns 0.0634 - 1064 B +77%
Multiple_aweXpect 416.88 ns 9.322 ns 8.264 ns 0.1006 0.0005 1688 B
Multiple_FluentAssertions 1,034.34 ns +148% 24.608 ns 23.018 ns 0.4025 - 6744 B +300%
Multiple_TUnit 493.06 ns +18% 9.349 ns 8.745 ns 0.1335 - 2240 B +33%
Satisfies_aweXpect 100.51 ns 2.281 ns 2.022 ns 0.0329 - 552 B
Satisfies_FluentAssertions 10,117.04 ns +9965% 280.732 ns 234.424 ns 0.3357 0.2899 5615 B +917%
Satisfies_TUnit 131.51 ns +31% 2.469 ns 2.309 ns 0.0424 - 712 B +29%
String_aweXpect 143.53 ns 3.370 ns 2.988 ns 0.0343 - 576 B
String_FluentAssertions 270.85 ns +89% 4.672 ns 4.370 ns 0.1373 - 2304 B +300%
String_TUnit 182.78 ns +27% 3.600 ns 3.006 ns 0.0544 - 912 B +58%
StringArray_aweXpect 320.71 ns 7.676 ns 6.410 ns 0.0515 - 864 B
StringArray_FluentAssertions 662.08 ns +106% 8.325 ns 6.952 ns 0.2480 - 4152 B +381%
StringArray_TUnit 340.86 ns +6% 5.362 ns 4.753 ns 0.0753 - 1264 B +46%
StringArrayInAnyOrder_aweXpect 308.03 ns 2.180 ns 1.821 ns 0.0544 - 912 B
StringArrayInAnyOrder_FluentAssertions 10,077.75 ns +3172% 77.268 ns 72.277 ns 1.9989 0.0610 33471 B +3570%
StringArrayInAnyOrder_TUnit 414.27 ns +34% 8.042 ns 7.522 ns 0.0839 - 1408 B +54%
Throws_aweXpect 6,207.25 ns 59.838 ns 53.045 ns 0.0992 - 1720 B
Throws_FluentAssertions 18,390.41 ns +196% 150.819 ns 125.941 ns 1.0986 - 18609 B +982%
Throws_TUnit 6,314.61 ns +2% 269.213 ns 251.822 ns 0.1068 - 1824 B +6%
ThrowsAsync_aweXpect 5,823.14 ns 78.987 ns 65.958 ns 0.1297 - 2208 B
ThrowsAsync_FluentAssertions 26,860.92 ns +361% 280.393 ns 234.141 ns 1.4038 - 23553 B +967%
ThrowsAsync_TUnit 12,014.51 ns +106% 74.780 ns 62.445 ns 0.1678 - 2920 B +32%

@vbreuss
vbreuss merged commit b13f28b into main Oct 11, 2026
16 checks passed
@vbreuss
vbreuss deleted the perf/dictionary-key-without-closure branch October 11, 2026 17:44
@github-actions github-actions Bot added the state: released The issue is released label 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