Skip to content

perf: record each call as a node of a lock-free linked list - #995

Merged
vbreuss merged 3 commits into
mainfrom
perf/linked-list-recording
Nov 17, 2026
Merged

vbreuss merged 3 commits into
mainfrom
perf/linked-list-recording

Conversation

@vbreuss

@vbreuss vbreuss commented Nov 17, 2026

Copy link
Copy Markdown
Member

A member called a few times paid for a 16-slot chunk plus the chunk reference array on its second call, which dominated the recording cost of a typical unit test. One node per call allocates only what is recorded.

Readers walk the list without a lock, which also removes the deadlock when a parameter matcher waits for another thread to record a call to the same member. A call that started before Clear is still dropped, also when the list was empty.

Measured locally against main with the multi-call workflow benchmark: Complete 1244 ns / 6.52 KB -> 1079 ns / 4.13 KB (1.78x -> 1.45x Imposter). Allocations drop or stay level in every Complete benchmark. A member called thousands of times allocates one node per call; the N=1000 benchmarks will show that cost.

@vbreuss vbreuss self-assigned this Nov 17, 2026
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Test Results

    47 files      47 suites   56m 33s ⏱️
 4 796 tests  4 796 ✅ 0 💤 0 ❌
48 368 runs  48 368 ✅ 0 💤 0 ❌

Results for commit a2623fc.

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

🚀 Benchmark Results

Compared to main @​ 79ca04a8: memory to the latest run, time to the median of the last 10 runs, as the runners differ in speed. A relative change marks when Mockolate gets more than 15% faster or slower relative to Imposter and TUnitMocks: 🔴 / 🟢 against both of them, 🔸 / 🔹 against only one.

CallbackBenchmarks

Memory

Benchmark main PR Δ
Callback 1,552 B 952 B 🟢 -600 B (-39%)

Relative performance (how many times as long the other library takes as Mockolate, main → PR)

Benchmark vs Imposter vs TUnitMocks
Callback 0.68× → 0.83× 🟢 -19% 1.68× → 2.19× 🟢 -23%
All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Callback 223.9 ns → 190.2 ns (-15%) 1,552 B → 952 B 0.68× → 0.83× 🟢 -19% 1.68× → 2.19× 🟢 -23%
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.19GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

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

Callback Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 190.2 ns 9.22 ns 8.63 ns 1.00 952 B 1.00
Imposter 158.8 ns 2.23 ns 2.09 ns 0.84 616 B 0.65
TUnitMocks 416.9 ns 16.81 ns 15.73 ns 2.20 2056 B 2.16
Moq 63,979.1 ns 3,486.97 ns 3,091.11 ns 337.04 9056 B 9.51
NSubstitute 3,602.7 ns 123.64 ns 115.66 ns 18.98 7864 B 8.26
FakeItEasy 3,664.4 ns 139.13 ns 130.14 ns 19.30 6930 B 7.28

CompleteEventBenchmarks

Memory

Benchmark main PR Δ
Event 1,000 B 864 B 🟢 -136 B (-14%)

Relative performance (how many times as long the other library takes as Mockolate, main → PR)

Benchmark vs Imposter vs TUnitMocks
Event 0.81× → 0.99× 🟢 -18% 0.75× → 0.89× 🟢 -16%
All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Event 207.8 ns → 154.7 ns (-26%) 1,000 B → 864 B 0.81× → 0.99× 🟢 -18% 0.75× → 0.89× 🟢 -16%
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

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

Event Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 154.7 ns 2.77 ns 2.59 ns 1.00 864 B 1.00
Imposter 153.0 ns 3.85 ns 3.60 ns 0.99 512 B 0.59
TUnitMocks 138.3 ns 2.49 ns 2.33 ns 0.89 1392 B 1.61
Moq 11,144.4 ns 24.61 ns 21.82 ns 72.08 12761 B 14.77
NSubstitute 4,492.6 ns 33.00 ns 29.25 ns 29.06 9192 B 10.64
FakeItEasy 195,094.8 ns 1,065.44 ns 996.61 ns 1,261.85 15619 B 18.08

CompleteIndexerBenchmarks

Memory

Benchmark main PR Δ
Indexer (N=1) 2,576 B 2,320 B 🟢 -256 B (-10%)
Indexer (N=10) 4,248 B 4,048 B 🟢 -200 B (-5%)
Indexer (N=1000) 138,329 B 194,129 B 🔴 +55,800 B (+40%)
All benchmarks compared to main
Benchmark Time Memory vs Imposter
Indexer (N=1) 619.6 ns → 617.9 ns (±0%) 2,576 B → 2,320 B 0.40× → 0.46×
Indexer (N=10) 1.98 μs → 2.08 μs (+5%) 4,248 B → 4,048 B 0.31× → 0.31×
Indexer (N=1000) 167.67 μs → 147.38 μs (-12%) 138,329 B → 194,129 B 0.28× → 0.28×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon 6975P-C 3.30GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

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

Indexer N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 617.9 ns 10.90 ns 10.19 ns 1.00 2.27 KB 1.00
Imposter 1 284.5 ns 3.12 ns 2.76 ns 0.46 1.07 KB 0.47
Moq 1 85,597.6 ns 608.51 ns 569.20 ns 138.57 20.32 KB 8.97
NSubstitute 1 7,835.0 ns 42.57 ns 37.74 ns 12.68 12.78 KB 5.64
FakeItEasy 1 6,411.0 ns 42.49 ns 39.74 ns 10.38 13.27 KB 5.86
Mockolate 10 2,083.7 ns 8.73 ns 8.16 ns 1.00 3.95 KB 1.00
Imposter 10 649.8 ns 5.71 ns 5.34 ns 0.31 2.13 KB 0.54
Moq 10 90,583.8 ns 401.46 ns 375.52 ns 43.47 29.67 KB 7.51
NSubstitute 10 18,149.0 ns 63.54 ns 56.32 ns 8.71 25.58 KB 6.47
FakeItEasy 10 15,531.8 ns 81.86 ns 76.57 ns 7.45 32.47 KB 8.21
Mockolate 1000 147,383.6 ns 681.20 ns 603.87 ns 1.00 189.58 KB 1.00
Imposter 1000 40,571.9 ns 239.34 ns 212.17 ns 0.28 118.14 KB 0.62
Moq 1000 654,792.2 ns 2,517.02 ns 2,231.28 ns 4.44 1006.36 KB 5.31
NSubstitute 1000 1,172,802.7 ns 4,648.18 ns 4,120.49 ns 7.96 1465.53 KB 7.73
FakeItEasy 1000 999,599.3 ns 5,509.19 ns 5,153.30 ns 6.78 2153 KB 11.36

CompleteMethodBenchmarks

Memory

Benchmark main PR Δ
Method (N=1) 1,168 B 1,040 B 🟢 -128 B (-11%)
Method (N=10) 1,744 B 1,616 B 🟢 -128 B (-7%)
Method (N=1000) 37,112 B 64,976 B 🔴 +27,864 B (+75%)

Relative performance (how many times as long the other library takes as Mockolate, main → PR)

Benchmark vs Imposter vs TUnitMocks
Method (N=1) 0.68× → 0.79× 1.74× → 2.05× 🔹 -15%
Method (N=1000) 0.63× → 0.47× 🔸 +32% 2.88× → 2.74×
All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Method (N=1) 234.5 ns → 133.4 ns (-43%) 1,168 B → 1,040 B 0.68× → 0.79× 1.74× → 2.05× 🔹 -15%
Method (N=10) 524.9 ns → 398.9 ns (-24%) 1,744 B → 1,616 B 0.62× → 0.63× 2.31× → 2.56×
Method (N=1000) 32.85 μs → 28.89 μs (-12%) 37,112 B → 64,976 B 0.63× → 0.47× 🔸 +32% 2.88× → 2.74×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.47GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

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

Method N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 133.4 ns 3.09 ns 2.73 ns 1.00 1040 B 1.00
Imposter 1 105.5 ns 1.83 ns 1.71 ns 0.79 728 B 0.70
TUnitMocks 1 273.8 ns 5.72 ns 5.35 ns 2.05 2088 B 2.01
Moq 1 59,379.0 ns 1,037.39 ns 919.62 ns 445.45 14920 B 14.35
NSubstitute 1 3,074.9 ns 156.95 ns 146.81 ns 23.07 9320 B 8.96
FakeItEasy 1 2,929.5 ns 78.87 ns 73.77 ns 21.98 8160 B 7.85
Mockolate 10 398.9 ns 5.47 ns 4.57 ns 1.00 1616 B 1.00
Imposter 10 252.0 ns 6.92 ns 6.14 ns 0.63 1088 B 0.67
TUnitMocks 10 1,021.9 ns 28.10 ns 21.94 ns 2.56 3840 B 2.38
Moq 10 62,974.9 ns 776.64 ns 688.47 ns 157.89 18906 B 11.70
NSubstitute 10 4,478.9 ns 53.83 ns 44.95 ns 11.23 12344 B 7.64
FakeItEasy 10 4,460.4 ns 67.57 ns 59.90 ns 11.18 16394 B 10.14
Mockolate 1000 28,892.5 ns 811.27 ns 758.86 ns 1.00 64976 B 1.00
Imposter 1000 13,665.0 ns 152.95 ns 143.07 ns 0.47 40688 B 0.63
TUnitMocks 1000 79,171.8 ns 1,461.72 ns 1,295.78 ns 2.74 203585 B 3.13
Moq 1000 183,846.9 ns 8,162.24 ns 7,634.97 ns 6.37 431937 B 6.65
NSubstitute 1000 155,963.7 ns 4,869.12 ns 4,554.58 ns 5.40 378037 B 5.82
FakeItEasy 1000 217,098.0 ns 4,757.62 ns 3,972.82 ns 7.52 944381 B 14.53

CompletePropertyBenchmarks

Memory

Benchmark main PR Δ
Property (N=1) 1,456 B 1,208 B 🟢 -248 B (-17%)
Property (N=10) 2,224 B 2,216 B 🟢 -8 B (±0%)
Property (N=1000) 49,152 B 113,096 B 🔴 +63,944 B (+130%)

Relative performance (how many times as long the other library takes as Mockolate, main → PR)

Benchmark vs Imposter vs TUnitMocks
Property (N=1) 0.43× → 0.54× 🟢 -21% 1.09× → 1.37× 🟢 -21%
Property (N=1000) 0.34× → 0.26× 🔴 +33% 2.21× → 1.79× 🔴 +24%
All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Property (N=1) 335.3 ns → 245.9 ns (-27%) 1,456 B → 1,208 B 0.43× → 0.54× 🟢 -21% 1.09× → 1.37× 🟢 -21%
Property (N=10) 851.0 ns → 676.3 ns (-21%) 2,224 B → 2,216 B 0.33× → 0.37× 1.63× → 1.78×
Property (N=1000) 54.44 μs → 50.34 μs (-8%) 49,152 B → 113,096 B 0.34× → 0.26× 🔴 +33% 2.21× → 1.79× 🔴 +24%
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

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

Property N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 245.9 ns 3.87 ns 3.62 ns 1.00 1208 B 1.00
Imposter 1 132.1 ns 1.55 ns 1.29 ns 0.54 672 B 0.56
TUnitMocks 1 337.1 ns 8.08 ns 7.56 ns 1.37 1672 B 1.38
Moq 1 7,492.7 ns 100.91 ns 94.39 ns 30.47 10457 B 8.66
NSubstitute 1 5,159.3 ns 46.86 ns 39.13 ns 20.98 11664 B 9.66
FakeItEasy 1 5,105.3 ns 52.71 ns 46.72 ns 20.76 11196 B 9.27
Mockolate 10 676.3 ns 2.87 ns 2.39 ns 1.00 2216 B 1.00
Imposter 10 248.6 ns 2.75 ns 2.57 ns 0.37 960 B 0.43
TUnitMocks 10 1,204.6 ns 7.61 ns 7.12 ns 1.78 4024 B 1.82
Moq 10 11,922.3 ns 48.24 ns 45.12 ns 17.63 17385 B 7.85
NSubstitute 10 11,891.8 ns 79.13 ns 74.02 ns 17.58 21529 B 9.72
FakeItEasy 10 12,491.4 ns 86.33 ns 76.53 ns 18.47 30785 B 13.89
Mockolate 1000 50,335.2 ns 110.83 ns 103.67 ns 1.00 113096 B 1.00
Imposter 1000 13,044.6 ns 134.66 ns 125.96 ns 0.26 32641 B 0.29
TUnitMocks 1000 89,860.8 ns 476.39 ns 422.30 ns 1.79 242489 B 2.14
Moq 1000 481,585.8 ns 3,328.40 ns 3,113.39 ns 9.57 724506 B 6.41
NSubstitute 1000 770,329.1 ns 7,372.75 ns 6,896.47 ns 15.30 1139701 B 10.08
FakeItEasy 1000 922,903.4 ns 8,711.62 ns 8,148.85 ns 18.34 2194325 B 19.40

CompleteWorkflowBenchmarks

Memory

Benchmark main PR Δ
Complete 6,680 B 4,232 B 🟢 -2,448 B (-37%)
All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Complete 1.21 μs → 1.30 μs (+7%) 6,680 B → 4,232 B 0.72× → 0.73× 1.98× → 2.18×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon 6975P-C 3.70GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

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

Complete Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1,297.2 ns 24.42 ns 22.84 ns 1.00 4.13 KB 1.00
Imposter 946.4 ns 18.92 ns 17.69 ns 0.73 3.17 KB 0.77
TUnitMocks 2,823.8 ns 38.56 ns 36.07 ns 2.18 10.3 KB 2.49
Moq 202,281.9 ns 1,025.74 ns 959.48 ns 155.99 46.12 KB 11.16
NSubstitute 19,469.6 ns 52.86 ns 49.45 ns 15.01 37.13 KB 8.98
FakeItEasy 16,879.7 ns 97.75 ns 91.44 ns 13.02 34.51 KB 8.35

MockCreationBenchmarks

✅ No changes in memory or relative performance (1 benchmarks)

All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
CreateMock 16.4 ns → 16.8 ns (+3%) 160 B → 160 B 5.63× → 5.75× 1.68× → 1.70×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 2.80GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

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

CreateMock Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 16.84 ns 0.320 ns 0.300 ns 1.00 160 B 1.00
Imposter 96.92 ns 1.629 ns 1.523 ns 5.76 504 B 3.15
TUnitMocks 28.71 ns 0.884 ns 0.827 ns 1.71 216 B 1.35
Moq 1,249.82 ns 18.952 ns 17.728 ns 74.23 2088 B 13.05
NSubstitute 1,769.90 ns 31.947 ns 29.883 ns 105.12 5032 B 31.45
FakeItEasy 1,571.76 ns 37.043 ns 34.650 ns 93.35 2755 B 17.22

A member called a few times paid for a 16-slot chunk on its second call, which dominated the cost of typical unit tests; one node per call allocates only what is recorded, and lock-free readers remove the deadlock when a matcher waits for a call to the same member.

A call that started before Clear is dropped, as before.
Public mutable fields let any caller corrupt the list links and are flagged by Sonar; generated code only needs to read the links and set the sequence, verified flag and boxed interaction.
@vbreuss
vbreuss force-pushed the perf/linked-list-recording branch from 8ac2b51 to b212674 Compare November 17, 2026 17:19
It does not access instance data, which Sonar reports.
@sonarqubecloud

Copy link
Copy Markdown

@vbreuss
vbreuss enabled auto-merge (squash) November 17, 2026 18:15
@vbreuss
vbreuss merged commit fffab2b into main Nov 17, 2026
23 checks passed
@vbreuss
vbreuss deleted the perf/linked-list-recording branch November 17, 2026 18:20
github-actions Bot added a commit that referenced this pull request Nov 17, 2026
github-actions Bot added a commit that referenced this pull request Nov 17, 2026
@github-actions

Copy link
Copy Markdown

This is addressed in release v4.2.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