Repository navigation
perf: stop searching a string in Contains once the outcome is decided - #1818
Merged
Merged
Conversation
`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
enabled auto-merge (squash)
October 11, 2026 16:46
|
Contributor
Test Results 91 files 91 suites 2h 3m 3s ⏱️ Results for commit 9cf73b8. |
Contributor
🚀 Benchmark ResultsDetails
|
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ß
Contributor
|
This is addressed in release v3.1.0. |
This was referenced Oct 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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 withAsRegex()allocated aMatchfor 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
Containsalready does.Measured on a 100,000 character log whose first line contains the expected text (ABBA, mean of two runs per side):
Contains("ERROR")on the logThe
DoesNotContainruns varied between 3.0 and 5.7 µs on both sides and are noise.