镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

perf: reduce allocations for property and indexer access - #602

Merged
vbreuss merged 4 commits into
mainfrom
topic/speedup-property-access
Mar 29, 2026
Merged

vbreuss merged 4 commits into
mainfrom
topic/speedup-property-access

Conversation

@vbreuss

@vbreuss vbreuss commented Mar 29, 2026 •

Copy link
Copy Markdown
Member

This PR reduces runtime allocations in Mockolate’s property and indexer access paths by removing LINQ/predicate usage and inlining property setup resolution logic into the interaction handlers.

Key Changes:

  • Replaced indexer setup lookup via FirstOrDefault(predicate) with an allocation-free loop-based matcher.
  • Inlined property setup retrieval/initialization logic into GetProperty/SetProperty to avoid delegate-related allocations.
  • Removed now-unused helper methods/usings related to the previous setup lookup approach.

@vbreuss vbreuss self-assigned this Mar 29, 2026
@github-actions

github-actions Bot commented Mar 29, 2026 •

Copy link
Copy Markdown

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 7763 2.61GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.201
[Host] : .NET 10.0.5 (10.0.5, 10.0.526.15411), X64 RyuJIT x86-64-v3

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

Method Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Indexer_Mockolate 946.7 ns 5.71 ns 4.46 ns 1.00 0.01 0.1898 0.0010 3.1 KB 1.00
Indexer_Moq 191,484.8 ns 713.99 ns 667.87 ns 202.26 1.15 0.7324 0.4883 14.57 KB 4.70
Indexer_NSubstitute 6,387.5 ns 30.09 ns 28.15 ns 6.75 0.04 0.5722 - 9.41 KB 3.04
Indexer_FakeItEasy 6,339.7 ns 32.60 ns 28.90 ns 6.70 0.04 0.4883 0.0153 8.03 KB 2.59
Indexer_Imposter 802.5 ns 13.05 ns 12.21 ns 0.85 0.01 0.2928 0.0048 4.8 KB 1.55
Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 7763 2.61GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.201
[Host] : .NET 10.0.5 (10.0.5, 10.0.526.15411), X64 RyuJIT x86-64-v3

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

Method Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Method_Mockolate 735.5 ns 6.06 ns 5.38 ns 1.00 0.01 0.1307 - 2.14 KB 1.00
Method_Moq 192,710.5 ns 461.18 ns 431.39 ns 262.01 1.93 0.7324 0.4883 14.59 KB 6.82
Method_NSubstitute 6,274.4 ns 26.81 ns 23.77 ns 8.53 0.07 0.5493 0.0076 9.09 KB 4.24
Method_FakeItEasy 6,478.5 ns 31.91 ns 28.29 ns 8.81 0.07 0.4883 0.0153 8.05 KB 3.76
Method_Imposter 588.0 ns 2.47 ns 1.93 ns 0.80 0.01 0.2537 0.0029 4.15 KB 1.94
Method_TUnitMocks 2,714.4 ns 15.36 ns 14.37 ns 3.69 0.03 0.4005 0.0076 6.55 KB 3.06
Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 7763 2.61GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.201
[Host] : .NET 10.0.5 (10.0.5, 10.0.526.15411), X64 RyuJIT x86-64-v3

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

Method Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Property_Mockolate 624.6 ns 3.86 ns 3.42 ns 1.00 0.01 0.1116 - 1.83 KB 1.00
Property_Moq 4,967.3 ns 55.21 ns 51.64 ns 7.95 0.09 0.3281 - 5.48 KB 3.00
Property_NSubstitute 5,292.7 ns 27.65 ns 24.51 ns 8.47 0.06 0.5188 0.0076 8.58 KB 4.69
Property_FakeItEasy 4,728.1 ns 62.59 ns 58.54 ns 7.57 0.10 0.3815 0.0153 6.35 KB 3.47
Property_Imposter 389.9 ns 11.53 ns 10.78 ns 0.62 0.02 0.1783 0.0019 2.91 KB 1.59
Property_TUnitMocks 1,964.6 ns 13.55 ns 11.32 ns 3.15 0.02 0.3319 0.0076 5.47 KB 2.99
Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 7763 2.61GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.201
[Host] : .NET 10.0.5 (10.0.5, 10.0.526.15411), X64 RyuJIT x86-64-v3

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

Method Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Event_Mockolate 427.9 ns 2.29 ns 1.91 ns 1.00 0.01 0.0710 - 1.16 KB 1.00
Event_Moq 13,030.3 ns 23.61 ns 20.93 ns 30.45 0.14 0.5951 - 9.76 KB 8.38
Event_NSubstitute 5,498.7 ns 73.38 ns 68.64 ns 12.85 0.16 0.5188 0.0076 8.52 KB 7.32
Event_FakeItEasy 196,123.2 ns 1,069.45 ns 948.04 ns 458.32 2.91 0.4883 0.2441 10.23 KB 8.79
Event_Imposter 1,002.8 ns 17.65 ns 16.51 ns 2.34 0.04 0.5093 0.0134 8.34 KB 7.16
Event_TUnitMocks 2,550.3 ns 160.19 ns 149.84 ns 5.96 0.34 0.2785 0.0057 4.55 KB 3.91
Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 7763 2.61GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.201
[Host] : .NET 10.0.5 (10.0.5, 10.0.526.15411), X64 RyuJIT x86-64-v3

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

Method Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Callback_Mockolate 437.9 ns 4.65 ns 4.13 ns 1.00 0.01 0.1073 - 1.76 KB 1.00
Callback_Moq 98,090.4 ns 408.68 ns 362.28 ns 224.02 2.19 0.4883 0.2441 8.88 KB 5.05
Callback_NSubstitute 4,414.5 ns 21.17 ns 19.80 ns 10.08 0.10 0.4730 0.0076 7.74 KB 4.40
Callback_FakeItEasy 4,905.0 ns 111.12 ns 103.94 ns 11.20 0.25 0.4120 0.0153 6.81 KB 3.87
Callback_Imposter 412.9 ns 12.13 ns 11.35 ns 0.94 0.03 0.1454 0.0010 2.38 KB 1.36
Callback_TUnitMocks 2,410.1 ns 66.21 ns 61.93 ns 5.50 0.15 0.3777 0.0076 6.18 KB 3.52

@github-actions

github-actions Bot commented Mar 29, 2026 •

Copy link
Copy Markdown

Test Results

    21 files  ±0      21 suites  ±0   5m 44s ⏱️ +54s
 2 886 tests ±0   2 885 ✅ ±0  1 💤 ±0  0 ❌ ±0 
19 393 runs  ±0  19 392 ✅ ±0  1 💤 ±0  0 ❌ ±0 

Results for commit 520f4b2. ± Comparison against base commit 8d8a527.

♻️ This comment has been updated with latest results.

@vbreuss
vbreuss force-pushed the topic/speedup-property-access branch from 783a76b to 9e105c0 Compare March 29, 2026 15:38
@vbreuss vbreuss changed the title perf: speed-up property access perf: reduce allocations for property and indexer access Mar 29, 2026
@vbreuss
vbreuss force-pushed the topic/speedup-property-access branch from d06c670 to 4e73e19 Compare March 29, 2026 15:44
@vbreuss
vbreuss marked this pull request as ready for review March 29, 2026 15:46
Copilot AI review requested due to automatic review settings March 29, 2026 15:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR reduces runtime allocations in Mockolate’s property and indexer access paths by removing LINQ/predicate usage and inlining property setup resolution logic into the interaction handlers.

Changes:

  • Replaced indexer setup lookup via FirstOrDefault(predicate) with an allocation-free loop-based matcher.
  • Inlined property setup retrieval/initialization logic into GetProperty/SetProperty to avoid delegate-related allocations.
  • Removed now-unused helper methods/usings related to the previous setup lookup approach.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
Source/Mockolate/Setup/MockSetups.cs Updates indexer setup lookup to a non-LINQ, interaction-based matcher.
Source/Mockolate/MockRegistry.Setup.cs Removes property/indexer helper methods and an unused using after inlining.
Source/Mockolate/MockRegistry.Interactions.cs Inlines property setup resolution and updates indexer setup lookup call sites.

Comment thread Source/Mockolate/Setup/MockSetups.cs Resolved
@vbreuss
vbreuss enabled auto-merge (squash) March 29, 2026 17:46
@sonarqubecloud

Copy link
Copy Markdown

@vbreuss
vbreuss merged commit a773d7d into main Mar 29, 2026
12 checks passed
@vbreuss
vbreuss deleted the topic/speedup-property-access branch March 29, 2026 17:51
@github-actions

Copy link
Copy Markdown

This is addressed in release v2.1.1.

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.

2 participants