Replace string creation with static strings for Regex options conversion to string #1504
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.
The
BsonRegularExpression(Regex)
constructor uses string concatenation to create the options string.If there's none only one option, it will result on a static interned string. Otherwise, it will create new strings. One less than the number of options.
Using a buffer to create the string is only better when there are 4 options.
However, using precomputed strings (because they aren't that much) beat every option in time and memory consumption.
Benchmarks
BenchmarkDotNet v0.13.8, Windows 11 (10.0.26100.2152)
13th Gen Intel Core i9-13900K, 1 CPU, 32 logical and 24 physical cores
.NET SDK 9.0.100-rc.2.24474.11
[Host] : .NET 8.0.10 (8.0.1024.46610), X64 RyuJIT AVX2