Skip to content

perf: trim the subject expression only when it is read - #1787

Merged
vbreuss merged 1 commit into
mainfrom
perf/trim-subject-expression-lazily
Oct 11, 2026
Merged

vbreuss merged 1 commit into
mainfrom
perf/trim-subject-expression-lazily

Conversation

@vbreuss

@vbreuss vbreuss commented Oct 11, 2026

Copy link
Copy Markdown
Member

The subject expression was trimmed in the constructor of every expectation, although it is only read for failure messages and tracing. For a multi-line expression, e.g. a lambda passed to Throws or DoesNotThrow, trimming split the lines and rebuilt the string on every call. The tracing calls on the delegate path read the expression even without a trace writer, so they now check whether tracing is enabled first.

Benchmark Before After
DoesNotThrow on a multi-line delegate 851.5 ns / 1768 B 357.1 ns / 752 B

The subject expression was trimmed in the constructor of every expectation, although it is only read for failure messages and tracing. For a multi-line expression, e.g. a lambda passed to `Throws` or `DoesNotThrow`, trimming split the lines and rebuilt the string on every call. The tracing calls on the delegate path read the expression even without a trace writer, so they now check whether tracing is enabled first.

| Benchmark | Before | After |
|---|---:|---:|
| `DoesNotThrow` on a multi-line delegate | 851.5 ns / 1768 B | 357.1 ns / 752 B |
| `DoesNotThrow` on a single-line delegate | 230.1 ns / 592 B | 192.8 ns / 600 B |
@vbreuss vbreuss self-assigned this Oct 11, 2026
@vbreuss
vbreuss enabled auto-merge (squash) October 11, 2026 08:42
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

     91 files  ±0       91 suites  ±0   2h 13m 50s ⏱️ + 24m 17s
 37 524 tests  - 7   37 524 ✅  - 7  0 💤 ±0  0 ❌ ±0 
217 902 runs  ±0  217 902 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 9e9d733. ± Comparison against base commit 7790486.

This pull request removes 4957 and adds 4878 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(4df2d06e-17bb-49fa-8ce2-5d609cf5013c)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(930d7098-f8da-48d9-ac67-26d4ac101788)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(ae72d8f3-c894-4997-b39e-b54c4cefd706)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(c726d693-51f8-46f9-b4a6-6ca7cfc06ae6)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(dca3197a-44f2-4cd5-9d25-6497c9dbb4ed)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(e4c8089f-28a2-4f10-98af-765fb3c4a60d)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(fc5820f9-fbaa-4224-81dc-219aef42299f)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(0cc5a02b-10cf-44b0-9604-3a98543ee96b)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(0f454166-267e-4106-9ede-308852e7c948)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(966e6fa5-a32e-4541-af2c-9474e883310a)
…

@vbreuss
vbreuss merged commit f81fa44 into main Oct 11, 2026
16 checks passed
@vbreuss
vbreuss deleted the perf/trim-subject-expression-lazily branch October 11, 2026 09:34
@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 8573C 3.01GHz, 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
Bool_aweXpect 142.3 ns 2.61 ns 2.44 ns 0.0052 - 440 B
Bool_FluentAssertions 273.5 ns +92% 2.90 ns 2.42 ns 0.0110 - 952 B +116%
Bool_TUnit 170.9 ns +20% 0.75 ns 0.66 ns 0.0055 - 472 B +7%
Equivalency_aweXpect 83,594.3 ns 157.28 ns 139.42 ns 0.1221 - 16440 B
Equivalency_FluentAssertions 1,988,663.6 ns +2279% 8,471.64 ns 7,509.89 ns 54.6875 7.8125 4841613 B +29350%
Equivalency_TUnit 181,600.9 ns +117% 706.24 ns 660.61 ns 1.7090 - 155992 B +849%
EventTriggered_aweXpect 660.6 ns 2.06 ns 1.93 ns 0.0219 - 1840 B
EventTriggered_FluentAssertions 31,924.1 ns +4733% 232.78 ns 217.75 ns 0.0610 - 5256 B +186%
Int_GreaterThan_aweXpect 151.5 ns 0.56 ns 0.49 ns 0.0064 - 544 B
Int_GreaterThan_FluentAssertions 267.2 ns +76% 0.86 ns 0.76 ns 0.0143 - 1224 B +125%
Int_GreaterThan_TUnit 205.3 ns +36% 0.66 ns 0.58 ns 0.0091 - 776 B +43%
ItemsCount_AtLeast_aweXpect 286.2 ns 1.52 ns 1.42 ns 0.0072 - 632 B
ItemsCount_AtLeast_FluentAssertions 502.1 ns +75% 2.10 ns 1.97 ns 0.0238 - 2008 B +218%
ItemsCount_AtLeast_TUnit 377.2 ns +32% 1.10 ns 1.03 ns 0.0124 - 1064 B +68%
Satisfies_aweXpect 190.3 ns 0.60 ns 0.53 ns 0.0069 - 584 B
Satisfies_FluentAssertions 15,436.6 ns +8014% 121.29 ns 113.46 ns 0.0610 0.0305 5614 B +861%
Satisfies_TUnit 209.1 ns +10% 1.02 ns 0.90 ns 0.0083 - 712 B +22%
String_aweXpect 242.9 ns 0.85 ns 0.80 ns 0.0072 - 608 B
String_FluentAssertions 1,168.5 ns +381% 11.45 ns 10.71 ns 0.0458 - 3944 B +549%
String_TUnit 304.5 ns +25% 0.86 ns 0.76 ns 0.0105 - 912 B +50%
StringArray_aweXpect 547.1 ns 3.14 ns 2.78 ns 0.0105 - 896 B
StringArray_FluentAssertions 1,324.3 ns +142% 6.52 ns 6.10 ns 0.0496 - 4152 B +363%
StringArray_TUnit 580.1 ns +6% 1.89 ns 1.77 ns 0.0143 - 1264 B +41%
StringArrayInAnyOrder_aweXpect 841.6 ns 2.52 ns 2.23 ns 0.0153 - 1304 B
StringArrayInAnyOrder_FluentAssertions 16,261.8 ns +1832% 51.46 ns 45.62 ns 0.3967 - 33465 B +2466%
StringArrayInAnyOrder_TUnit 688.9 ns -18% 1.72 ns 1.61 ns 0.0162 - 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