Skip to content

perf: search the strings of an array or a list directly in Contains - #1822

Merged
vbreuss merged 2 commits into
mainfrom
perf/contains-direct-loop
Oct 11, 2026
Merged

vbreuss merged 2 commits into
mainfrom
perf/contains-direct-loop

Conversation

@vbreuss

@vbreuss vbreuss commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

Contains with a string advanced an enumerator, checked the cancellation and called the comparison virtually for every item, which cost about 9 ns per item, against about 3 ns in FluentAssertions and TUnit. On the 17 titles of the Contains benchmark this put aweXpect behind both.

On .NET 8 and later, an array or a List<T> is now searched with a plain loop when the strings are compared ordinally. Only a comparison that runs no code of the caller searches directly, because a list detects a modification during the search only in its enumerator. An array or a list knows all its items, so the search cannot be canceled before their end.

Measured on the 17 titles of the benchmark (ABBA, mean of two runs per side):

Method Before After Allocated before / after
List<string> Contains (first item) 254.3 ns 265.8 ns 720 B / 720 B
List<string> Contains (7th item, the benchmark) 324.3 ns 286.6 ns 720 B / 720 B
List<string> Contains (last item) 388.9 ns 302.9 ns 720 B / 720 B
string[] Contains (last item) 363.8 ns 278.6 ns 720 B / 720 B
List<string> DoesNotContain 405.9 ns 323.8 ns 720 B / 720 B

The first item is within the noise.

`Contains` with a string advanced an enumerator, checked the cancellation and called the comparison virtually for every item, which cost about 9 ns per item, against about 3 ns in FluentAssertions and TUnit. On the 17 titles of the `Contains` benchmark this put aweXpect behind both.

On .NET 8 and later, an array or a `List<T>` is now searched with a plain loop when the strings are compared ordinally. Only a comparison that runs no code of the caller searches directly, because a list detects a modification during the search only in its enumerator. An array or a list knows all its items, so the search cannot be canceled before their end.

Measured on the 17 titles of the benchmark (ABBA, mean of two runs per side):

| Method | Before | After | Allocated before / after |
|---|---:|---:|---:|
| `List<string>` `Contains` (first item) | 254.3 ns | 265.8 ns | 720 B / 720 B |
| `List<string>` `Contains` (7th item, the benchmark) | 324.3 ns | 286.6 ns | 720 B / 720 B |
| `List<string>` `Contains` (last item) | 388.9 ns | 302.9 ns | 720 B / 720 B |
| `string[]` `Contains` (last item) | 363.8 ns | 278.6 ns | 720 B / 720 B |
| `List<string>` `DoesNotContain` | 405.9 ns | 323.8 ns | 720 B / 720 B |

The first item is within the noise.
@vbreuss vbreuss self-assigned this Oct 11, 2026
@vbreuss
vbreuss added this pull request to stack #1824 October 11, 2026 18:50
@vbreuss
vbreuss merged commit 8daee84 into main Oct 11, 2026
12 checks passed
@vbreuss
vbreuss deleted the perf/contains-direct-loop branch October 11, 2026 18:55
@sonarqubecloud

sonarqubecloud Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
1 New issue

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@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
Contains_aweXpect 169.40 ns 3.736 ns 3.495 ns 0.0429 - 720 B
Contains_FluentAssertions 203.23 ns +20% 6.013 ns 5.625 ns 0.0961 0.0002 1608 B +123%
Contains_TUnit 198.62 ns +17% 3.434 ns 2.867 ns 0.0505 - 848 B +18%
Equivalency_aweXpect 6,214.65 ns 113.248 ns 100.391 ns 0.1755 - 3040 B
Equivalency_FluentAssertions 166,187.28 ns +2574% 2,912.086 ns 2,723.967 ns 35.1563 1.9531 590288 B +19317%
Equivalency_TUnit 9,809.25 ns +58% 160.275 ns 125.132 ns 0.9308 - 15680 B +416%
EventTriggered_aweXpect 342.05 ns 4.696 ns 3.921 ns 0.1101 0.0005 1848 B
EventTriggered_FluentAssertions 29,951.78 ns +8656% 914.639 ns 855.554 ns 0.3052 0.2747 5256 B +184%
Int_GreaterThan_aweXpect 74.89 ns 2.309 ns 2.047 ns 0.0305 - 512 B
Int_GreaterThan_FluentAssertions 120.99 ns +62% 1.092 ns 0.912 ns 0.0730 - 1224 B +139%
Int_GreaterThan_TUnit 123.87 ns +65% 6.188 ns 5.486 ns 0.0463 - 776 B +52%
ItemsCount_AtLeast_aweXpect 128.11 ns 2.981 ns 2.642 ns 0.0358 - 600 B
ItemsCount_AtLeast_FluentAssertions 244.88 ns +91% 1.577 ns 1.475 ns 0.1197 - 2008 B +235%
ItemsCount_AtLeast_TUnit 195.12 ns +52% 4.093 ns 3.628 ns 0.0634 - 1064 B +77%
Multiple_aweXpect 386.87 ns 9.501 ns 8.888 ns 0.1006 0.0005 1688 B
Multiple_FluentAssertions 1,018.97 ns +163% 36.055 ns 33.726 ns 0.4025 - 6744 B +300%
Multiple_TUnit 478.39 ns +24% 24.168 ns 22.607 ns 0.1335 - 2240 B +33%
Satisfies_aweXpect 93.15 ns 2.853 ns 2.529 ns 0.0329 - 552 B
Satisfies_FluentAssertions 9,320.52 ns +9906% 389.538 ns 364.374 ns 0.3357 0.2899 5615 B +917%
Satisfies_TUnit 120.11 ns +29% 2.485 ns 1.940 ns 0.0424 - 712 B +29%
String_aweXpect 136.28 ns 3.081 ns 2.882 ns 0.0343 - 576 B
String_FluentAssertions 257.99 ns +89% 5.687 ns 5.320 ns 0.1373 - 2304 B +300%
String_TUnit 169.94 ns +25% 3.345 ns 3.129 ns 0.0544 - 912 B +58%
StringArray_aweXpect 324.25 ns 2.340 ns 1.954 ns 0.0515 - 864 B
StringArray_FluentAssertions 643.22 ns +98% 22.648 ns 18.912 ns 0.2480 - 4152 B +381%
StringArray_TUnit 358.87 ns +11% 12.170 ns 11.384 ns 0.0753 - 1264 B +46%
StringArrayInAnyOrder_aweXpect 323.22 ns 11.772 ns 11.012 ns 0.0544 - 912 B
StringArrayInAnyOrder_FluentAssertions 9,878.67 ns +2956% 231.633 ns 180.844 ns 1.9989 0.0610 33471 B +3570%
StringArrayInAnyOrder_TUnit 422.67 ns +31% 9.458 ns 8.384 ns 0.0839 - 1408 B +54%
Throws_aweXpect 6,657.48 ns 102.741 ns 91.077 ns 0.0992 - 1720 B
Throws_FluentAssertions 19,160.39 ns +188% 437.683 ns 409.409 ns 1.0986 - 18609 B +982%
Throws_TUnit 6,580.09 ns -1% 220.417 ns 195.394 ns 0.1068 - 1824 B +6%
ThrowsAsync_aweXpect 6,342.18 ns 193.413 ns 180.919 ns 0.1297 - 2208 B
ThrowsAsync_FluentAssertions 28,173.69 ns +344% 716.204 ns 634.896 ns 1.4038 - 23553 B +967%
ThrowsAsync_TUnit 12,786.23 ns +102% 412.719 ns 386.058 ns 0.1678 - 2920 B +32%

@github-actions

Copy link
Copy Markdown
Contributor

This is addressed in release v3.1.0.

@github-actions github-actions Bot added the state: released The issue is released label Oct 11, 2026
github-actions Bot added a commit that referenced this pull request Oct 11, 2026
… a list directly in `Contains` (#1822) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Oct 11, 2026
… a list directly in `Contains` (#1822) by Valentin Breuß
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