Skip to content

perf: share parsed regexes between expectations - #1788

Merged
vbreuss merged 1 commit into
mainfrom
perf/cache-parsed-regexes
Oct 11, 2026
Merged

vbreuss merged 1 commit into
mainfrom
perf/cache-parsed-regexes

Conversation

@vbreuss

@vbreuss vbreuss commented Oct 11, 2026

Copy link
Copy Markdown
Member

Every expectation has string options of its own, so a regex pattern was parsed again for every evaluation, and AsRegex(RegexOptions) parsed an empty pattern just to validate the options. With RegexOptions.Compiled this emitted IL twice per assertion. The parsed regexes are now kept in a bounded cache keyed by pattern, options and culture.

Benchmark Before After
IsEqualTo("order-[0-9]+").AsRegex() 1.285 µs / 3 KB 310.5 ns / 656 B

Every expectation has string options of its own, so a regex pattern was parsed again for every evaluation, and `AsRegex(RegexOptions)` parsed an empty pattern just to validate the options. With `RegexOptions.Compiled` this emitted IL twice per assertion. The parsed regexes are now kept in a bounded cache keyed by pattern, options and culture.

| Benchmark | Before | After |
|---|---:|---:|
| `IsEqualTo("order-[0-9]+").AsRegex()` | 1.285 µs / 3 KB | 310.5 ns / 656 B |
| `IsEqualTo("order-[0-9]+").AsRegex(RegexOptions.Compiled)` | 1039.4 µs / 16.96 KB | 265.1 ns / 680 B |
| `IsEqualTo("ORDER-[0-9]+").AsRegex().IgnoringCase()` | 2.152 µs / 3.87 KB | 373.9 ns / 656 B |
@vbreuss
vbreuss enabled auto-merge (squash) October 11, 2026 08:43
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

     91 files  ± 0       91 suites  ±0   1h 47m 18s ⏱️ - 2m 15s
 37 514 tests  - 17   37 514 ✅  - 17  0 💤 ±0  0 ❌ ±0 
217 902 runs  ± 0  217 902 ✅ ± 0  0 💤 ±0  0 ❌ ±0 

Results for commit d15bf64. ± Comparison against base commit 7790486.

This pull request removes 4958 and adds 4869 tests. Note that renamed tests count towards both.

	
	    get;
	"a": 1
	await
	{
	}, 1)


, 2)

b, 2, False)
…
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(255aa8ef-7178-4fe7-ab4c-5865d0a321e0)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(5f1ef555-f371-4a06-bbd8-3d1379c61684)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(71ae37ec-2a52-4c7b-b57b-55f5f08e27e4)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(8185997e-ac62-47ad-952d-a2728b7ecae9)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(9f8f2d87-552a-4f7d-9bf3-b4752f6e665a)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(a87ec885-bef9-44f7-8808-b573df7f90af)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(c3e7796a-94f2-4cbb-adaf-a8641f0148fe)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(6c57921f-4eb6-46e0-9e59-f75e347a694a)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(7718b99a-f3d2-4798-862f-7a2516626f76)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(c0c311a6-2261-495a-bc6e-0428e153cbb3)
…

@vbreuss
vbreuss merged commit d62d702 into main Oct 11, 2026
16 checks passed
@vbreuss
vbreuss deleted the perf/cache-parsed-regexes branch October 11, 2026 09:52
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.5 LTS (Noble Numbat)
AMD EPYC 7763 2.45GHz, 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-v3

Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=1 WarmupCount=10

Method Mean vs aweXpect Error StdDev Gen0 Gen1 Allocated vs aweXpect
Bool_aweXpect 149.9 ns 5.20 ns 4.61 ns 0.0262 - 440 B
Bool_FluentAssertions 268.4 ns +79% 14.54 ns 13.60 ns 0.0567 - 952 B +116%
Bool_TUnit 214.9 ns +43% 1.52 ns 1.42 ns 0.0281 - 472 B +7%
Equivalency_aweXpect 108,517.4 ns 176.38 ns 156.35 ns 0.9766 - 16440 B
Equivalency_FluentAssertions 2,758,208.3 ns +2442% 23,450.62 ns 18,308.70 ns 289.0625 42.9688 4841651 B +29350%
Equivalency_TUnit 205,584.2 ns +89% 535.74 ns 447.36 ns 9.2773 0.4883 155992 B +849%
EventTriggered_aweXpect 793.5 ns 8.06 ns 7.54 ns 0.1097 - 1840 B
EventTriggered_FluentAssertions 84,852.5 ns +10594% 1,213.25 ns 1,075.51 ns 0.2441 0.1221 5252 B +185%
Int_GreaterThan_aweXpect 159.6 ns 4.39 ns 4.11 ns 0.0324 - 544 B
Int_GreaterThan_FluentAssertions 272.6 ns +71% 7.58 ns 7.09 ns 0.0730 - 1224 B +125%
Int_GreaterThan_TUnit 259.3 ns +63% 4.83 ns 4.28 ns 0.0463 - 776 B +43%
ItemsCount_AtLeast_aweXpect 394.8 ns 3.57 ns 3.34 ns 0.0377 - 632 B
ItemsCount_AtLeast_FluentAssertions 480.4 ns +22% 8.23 ns 7.70 ns 0.1192 - 2008 B +218%
ItemsCount_AtLeast_TUnit 406.5 ns +3% 18.84 ns 17.62 ns 0.0634 - 1064 B +68%
Satisfies_aweXpect 205.7 ns 3.10 ns 2.90 ns 0.0348 - 584 B
Satisfies_FluentAssertions 31,649.8 ns +15289% 186.70 ns 165.51 ns 0.3052 0.2441 5614 B +861%
Satisfies_TUnit 254.4 ns +24% 3.02 ns 2.68 ns 0.0424 - 712 B +22%
String_aweXpect 276.6 ns 2.46 ns 2.18 ns 0.0362 - 608 B
String_FluentAssertions 1,291.0 ns +367% 26.42 ns 24.72 ns 0.2346 - 3944 B +549%
String_TUnit 340.6 ns +23% 3.77 ns 3.53 ns 0.0544 - 912 B +50%
StringArray_aweXpect 695.3 ns 4.02 ns 3.76 ns 0.0534 - 896 B
StringArray_FluentAssertions 1,415.5 ns +104% 25.71 ns 24.05 ns 0.2480 - 4152 B +363%
StringArray_TUnit 677.2 ns -3% 16.09 ns 15.05 ns 0.0753 - 1264 B +41%
StringArrayInAnyOrder_aweXpect 1,082.6 ns 15.67 ns 14.66 ns 0.0763 - 1304 B
StringArrayInAnyOrder_FluentAssertions 27,905.1 ns +2478% 136.94 ns 128.09 ns 1.9836 0.0610 33471 B +2467%
StringArrayInAnyOrder_TUnit 838.4 ns -23% 5.07 ns 4.74 ns 0.0839 - 1408 B +8%

github-actions Bot added a commit that referenced this pull request Oct 11, 2026
github-actions Bot added a commit that referenced this pull request 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