Skip to content

perf: stop searching a string in Contains once the outcome is decided - #1818

Merged
vbreuss merged 1 commit into
mainfrom
perf/contains-string-stops-at-the-decisive-match
Oct 11, 2026
Merged

vbreuss merged 1 commit into
mainfrom
perf/contains-string-stops-at-the-decisive-match

Conversation

@vbreuss

@vbreuss vbreuss commented Oct 11, 2026

Copy link
Copy Markdown
Member

Contains(substring) counted every occurrence in the subject, although its quantifier is decided after a few: the default "at least once" by the first match. On a long subject this scanned the whole string, and with AsRegex() allocated a Match for every occurrence.

It now counts only up to the amount that decides the quantifier. When counting stopped there, the failure message reports a lower bound ("contained "x" at least once"), as collection Contains already does.

Measured on a 100,000 character log whose first line contains the expected text (ABBA, mean of two runs per side):

Method Before After Allocated before / after
Contains("ERROR") on the log 22,540.0 ns 158.0 ns 568 B / 568 B

The DoesNotContain runs varied between 3.0 and 5.7 µs on both sides and are noise.

`Contains(substring)` counted every occurrence in the subject, although its quantifier is decided after a few: the default "at least once" by the first match. On a long subject this scanned the whole string, and with `AsRegex()` allocated a `Match` for every occurrence.

It now counts only up to the amount that decides the quantifier. When counting stopped there, the failure message reports a lower bound ("contained "x" at least once"), as collection `Contains` already does.

Measured on a 100,000 character log whose first line contains the expected text (ABBA, mean of two runs per side):

| Method | Before | After | Allocated before / after |
|---|---:|---:|---:|
| `Contains("ERROR")` on the log | 22,540.0 ns | 158.0 ns | 568 B / 568 B |
| `Contains("ERR[O]R").AsRegex()` on the log | 179,722.9 ns | 378.8 ns | 473,608 B / 1,032 B |
| `DoesNotContain("WARN")` on the log (full scan either way) | 3,665.5 ns | 4,374.3 ns | 568 B / 568 B |
| `Contains("some")` on a short string | 172.8 ns | 152.7 ns | 568 B / 568 B |

The `DoesNotContain` runs varied between 3.0 and 5.7 µs on both sides and are noise.
@vbreuss vbreuss self-assigned this Oct 11, 2026
@vbreuss
vbreuss enabled auto-merge (squash) October 11, 2026 16:46
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

     91 files       91 suites   2h 3m 3s ⏱️
 37 544 tests  37 544 ✅ 0 💤 0 ❌
218 070 runs  218 070 ✅ 0 💤 0 ❌

Results for commit 9cf73b8.

@vbreuss
vbreuss merged commit 579ea79 into main Oct 11, 2026
16 checks passed
@vbreuss
vbreuss deleted the perf/contains-string-stops-at-the-decisive-match branch October 11, 2026 17:14
@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 8370C CPU 2.80GHz (Max: 2.79GHz), 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 443.8 ns 2.98 ns 2.64 ns 0.0286 - 720 B
Contains_FluentAssertions 425.7 ns -4% 11.41 ns 10.67 ns 0.0639 - 1608 B +123%
Contains_TUnit 403.2 ns -9% 5.07 ns 4.74 ns 0.0334 - 848 B +18%
Equivalency_aweXpect 11,463.9 ns 29.39 ns 27.50 ns 0.1068 - 3040 B
Equivalency_FluentAssertions 335,053.0 ns +2823% 1,967.49 ns 1,840.40 ns 23.4375 - 590266 B +19317%
Equivalency_TUnit 19,875.7 ns +73% 108.02 ns 101.04 ns 0.6104 - 15680 B +416%
EventTriggered_aweXpect 742.3 ns 7.79 ns 7.28 ns 0.0734 - 1848 B
EventTriggered_FluentAssertions 61,606.4 ns +8199% 880.31 ns 823.44 ns 0.1831 0.1221 5254 B +184%
Int_GreaterThan_aweXpect 162.1 ns 1.86 ns 1.74 ns 0.0203 - 512 B
Int_GreaterThan_FluentAssertions 297.2 ns +83% 6.33 ns 5.61 ns 0.0486 - 1224 B +139%
Int_GreaterThan_TUnit 237.5 ns +46% 3.10 ns 2.90 ns 0.0305 - 776 B +52%
ItemsCount_AtLeast_aweXpect 289.2 ns 2.31 ns 2.16 ns 0.0238 - 600 B
ItemsCount_AtLeast_FluentAssertions 526.8 ns +82% 6.37 ns 5.96 ns 0.0792 - 2008 B +235%
ItemsCount_AtLeast_TUnit 406.4 ns +41% 2.71 ns 2.12 ns 0.0420 - 1064 B +77%
Multiple_aweXpect 784.8 ns 6.30 ns 5.58 ns 0.0668 - 1688 B
Multiple_FluentAssertions 1,983.3 ns +153% 32.52 ns 30.42 ns 0.2670 - 6744 B +300%
Multiple_TUnit 923.1 ns +18% 8.40 ns 7.45 ns 0.0887 - 2240 B +33%
Satisfies_aweXpect 189.6 ns 1.66 ns 1.55 ns 0.0219 - 552 B
Satisfies_FluentAssertions 24,685.4 ns +12916% 291.71 ns 272.86 ns 0.2136 0.1831 5615 B +917%
Satisfies_TUnit 239.1 ns +26% 4.33 ns 4.05 ns 0.0281 - 712 B +29%
String_aweXpect 271.7 ns 2.00 ns 1.87 ns 0.0229 - 576 B
String_FluentAssertions 592.7 ns +118% 8.69 ns 8.13 ns 0.0916 - 2304 B +300%
String_TUnit 351.7 ns +29% 2.17 ns 1.92 ns 0.0362 - 912 B +58%
StringArray_aweXpect 629.5 ns 4.06 ns 3.79 ns 0.0343 - 864 B
StringArray_FluentAssertions 1,466.1 ns +133% 16.97 ns 15.04 ns 0.1640 - 4152 B +381%
StringArray_TUnit 655.7 ns +4% 3.24 ns 3.03 ns 0.0496 - 1264 B +46%
StringArrayInAnyOrder_aweXpect 637.5 ns 2.78 ns 2.60 ns 0.0362 - 912 B
StringArrayInAnyOrder_FluentAssertions 24,596.4 ns +3758% 50.15 ns 46.91 ns 1.3123 0.0305 33468 B +3570%
StringArrayInAnyOrder_TUnit 808.3 ns +27% 6.81 ns 6.37 ns 0.0553 - 1408 B +54%
Throws_aweXpect 11,109.4 ns 21.29 ns 18.87 ns 0.0610 - 1720 B
Throws_FluentAssertions 36,449.3 ns +228% 52.47 ns 46.51 ns 0.7324 - 18609 B +982%
Throws_TUnit 10,356.4 ns -7% 16.69 ns 13.94 ns 0.0610 - 1824 B +6%
ThrowsAsync_aweXpect 10,976.7 ns 16.48 ns 14.61 ns 0.0763 - 2208 B
ThrowsAsync_FluentAssertions 52,174.8 ns +375% 68.45 ns 60.68 ns 0.9155 - 23553 B +967%
ThrowsAsync_TUnit 20,637.1 ns +88% 25.82 ns 21.56 ns 0.0916 - 2920 B +32%

github-actions Bot added a commit that referenced this pull request Oct 11, 2026
…ins` once the outcome is decided (#1818) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Oct 11, 2026
…ins` once the outcome is decided (#1818) 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