Repository navigation
perf(mocks): reduce setup allocations and fixed-return overhead - #7827
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Confidence Score: 5/5
[Medium impact] The PR appears safe to merge; no blocking issues were found. SummaryThis PR reduces allocations during mock setup and avoids boxing fixed value-type returns during calls.
No actionable issues found. Tests were not run during this review. Reviews (2) · Last reviewed commit: "perf(mocks): limit fixed return dispatch..." · Reviewed by Greptile |
Review of #7827I read the diff and the surrounding code in What I checked
Non-blocking suggestions
Nice, well-scoped performance change with good tests. Approving. |
Updated [TUnit](https://github.com/thomhurst/TUnit) from 1.87.0 to 1.87.12. <details> <summary>Release notes</summary> _Sourced from [TUnit's releases](https://github.com/thomhurst/TUnit/releases)._ ## 1.87.12 <!-- Release notes generated using configuration in .github/release.yml at v1.87.12 --> ## What's Changed ### Other Changes * ci: keep the pipeline run report and cache Playwright browsers by @thomhurst in thomhurst/TUnit#7813 * docs: mention source-generated competitors in mock benchmark descriptions by @vbreuss in thomhurst/TUnit#7821 * perf(mocks): reduce setup allocations and fixed-return overhead by @thomhurst in thomhurst/TUnit#7827 * fix(mocks): preserve nullable base-class constraints by @thomhurst in thomhurst/TUnit#7829 * fix(mocks): reduce allocations, bound benchmarks, and clear test warnings by @thomhurst in thomhurst/TUnit#7831 * perf(mocks): read the latest capture without allocating by @thomhurst in thomhurst/TUnit#7832 ### Dependencies * chore(deps): update all non-major dependencies by @thomhurst in thomhurst/TUnit#7814 * chore(deps): update dependency masstransit to 9.3.0 by @thomhurst in thomhurst/TUnit#7816 * chore(deps): update dependency microsoft.playwright to 1.65.1 by @thomhurst in thomhurst/TUnit#7817 * chore(deps): update dependency mockolate to 4.2.0 by @thomhurst in thomhurst/TUnit#7819 * chore(deps): update all non-major dependencies to 33.7.0 by @thomhurst in thomhurst/TUnit#7826 **Full Changelog**: thomhurst/TUnit@v1.87.0...v1.87.12 Commits viewable in [compare view](thomhurst/TUnit@v1.87.0...v1.87.12). </details> [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore <dependency name> major version` will close this group update PR and stop Dependabot creating any more for the specific dependency's major version (unless you unignore this specific dependency's major version or upgrade to it yourself) - `@dependabot ignore <dependency name> minor version` will close this group update PR and stop Dependabot creating any more for the specific dependency's minor version (unless you unignore this specific dependency's minor version or upgrade to it yourself) - `@dependabot ignore <dependency name>` will close this group update PR and stop Dependabot creating any more for the specific dependency (unless you unignore this specific dependency or upgrade to it yourself) - `@dependabot unignore <dependency name>` will remove all of the ignore conditions of the specified dependency - `@dependabot unignore <dependency name> <ignore condition>` will remove the ignore condition of the specified dependency and ignore conditions </details> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Updated [TUnit](https://github.com/thomhurst/TUnit) from 1.84.0 to 1.87.12. <details> <summary>Release notes</summary> _Sourced from [TUnit's releases](https://github.com/thomhurst/TUnit/releases)._ ## 1.87.12 <!-- Release notes generated using configuration in .github/release.yml at v1.87.12 --> ## What's Changed ### Other Changes * ci: keep the pipeline run report and cache Playwright browsers by @thomhurst in thomhurst/TUnit#7813 * docs: mention source-generated competitors in mock benchmark descriptions by @vbreuss in thomhurst/TUnit#7821 * perf(mocks): reduce setup allocations and fixed-return overhead by @thomhurst in thomhurst/TUnit#7827 * fix(mocks): preserve nullable base-class constraints by @thomhurst in thomhurst/TUnit#7829 * fix(mocks): reduce allocations, bound benchmarks, and clear test warnings by @thomhurst in thomhurst/TUnit#7831 * perf(mocks): read the latest capture without allocating by @thomhurst in thomhurst/TUnit#7832 ### Dependencies * chore(deps): update all non-major dependencies by @thomhurst in thomhurst/TUnit#7814 * chore(deps): update dependency masstransit to 9.3.0 by @thomhurst in thomhurst/TUnit#7816 * chore(deps): update dependency microsoft.playwright to 1.65.1 by @thomhurst in thomhurst/TUnit#7817 * chore(deps): update dependency mockolate to 4.2.0 by @thomhurst in thomhurst/TUnit#7819 * chore(deps): update all non-major dependencies to 33.7.0 by @thomhurst in thomhurst/TUnit#7826 **Full Changelog**: thomhurst/TUnit@v1.87.0...v1.87.12 ## 1.87.0 <!-- Release notes generated using configuration in .github/release.yml at v1.87.0 --> ## What's Changed ### Other Changes * docs: fix the contribution guide link in the TUnit.Core readme by @farisbasha in thomhurst/TUnit#7806 * fix(aspire): support AddDotnetProject resources in AspireFixture by @thomhurst in thomhurst/TUnit#7812 ### Dependencies * chore(deps): update all non-major dependencies by @thomhurst in thomhurst/TUnit#7807 ## New Contributors * @farisbasha made their first contribution in thomhurst/TUnit#7806 **Full Changelog**: thomhurst/TUnit@v1.86.6...v1.87.0 ## 1.86.6 <!-- Release notes generated using configuration in .github/release.yml at v1.86.6 --> ## What's Changed ### Other Changes * feat: convert string [Arguments] to CultureInfo, Uri and Version by @thomhurst in thomhurst/TUnit#7800 * fix(assertions): treat the same instance as equivalent in IsEquivalentTo by @mohsinaliwork686-rgb in thomhurst/TUnit#7799 ### Dependencies * chore(deps): update all non-major dependencies by @thomhurst in thomhurst/TUnit#7797 * chore(deps): update dependency verify.tool to v1 by @thomhurst in thomhurst/TUnit#7803 * chore(deps): update dependency mockolate to 4.1.1 by @thomhurst in thomhurst/TUnit#7802 ## New Contributors * @mohsinaliwork686-rgb made their first contribution in thomhurst/TUnit#7799 **Full Changelog**: thomhurst/TUnit@v1.86.0...v1.86.6 ## 1.86.0 <!-- Release notes generated using configuration in .github/release.yml at v1.86.0 --> ## What's Changed ### Other Changes * fix(assertions): keep the awaited value and drill-ins after Because by @JohnVerheij in thomhurst/TUnit#7788 * fix(assertions)!: collection checks no longer chain without And/Or by @thomhurst in thomhurst/TUnit#7791 * test: fix flaky owner-collection check in ExternalObjectOwnershipTests by @thomhurst in thomhurst/TUnit#7794 * fix(assertions): keep a value-type result of AssertionResult<T> as T by @JohnVerheij in thomhurst/TUnit#7793 ### Dependencies * chore(deps): update all non-major dependencies by @thomhurst in thomhurst/TUnit#7790 **Full Changelog**: thomhurst/TUnit@v1.85.7...v1.86.0 ## 1.85.7 <!-- Release notes generated using configuration in .github/release.yml at v1.85.7 --> ## What's Changed ### Other Changes * fix: read custom data source row metadata without reflection in source-generated mode by @thomhurst in thomhurst/TUnit#7784 ### Dependencies * chore(deps): update all non-major dependencies to 1.85.0 by @thomhurst in thomhurst/TUnit#7777 * chore(deps): update dependency coverlet.collector to 10.1.1 by @thomhurst in thomhurst/TUnit#7780 * chore(deps): update all non-major dependencies to 33.5.0 by @thomhurst in thomhurst/TUnit#7783 **Full Changelog**: thomhurst/TUnit@v1.85.0...v1.85.7 ## 1.85.0 <!-- Release notes generated using configuration in .github/release.yml at v1.85.0 --> ## What's Changed ### Other Changes * fix(deps): keep net8.0/net10.0 on OpenTelemetry 1.20.x by @thomhurst in thomhurst/TUnit#7758 * chore(deps): pin runtime-versioned packages to their target framework's major by @thomhurst in thomhurst/TUnit#7762 * Migrate the build pipeline to ModularPipelines v4 by @thomhurst in thomhurst/TUnit#7702 * ci: select Xcode 27.2 for the Apple smoke tests by @thomhurst in thomhurst/TUnit#7765 * chore(deps): cap Threading analyzers below 18.7.65 and FSharp.Core below 11 in Renovate by @thomhurst in thomhurst/TUnit#7766 * feat: respect system theme in HTML reports by @ahmedsamy56 in thomhurst/TUnit#7768 * feat: convert string [Arguments] through declared TypeConverters by @thomhurst in thomhurst/TUnit#7759 * ci: skip the scene manifest check for the Apple smoke app by @thomhurst in thomhurst/TUnit#7770 * fix: convert string [Arguments] to IParsable types under Native AOT by @thomhurst in thomhurst/TUnit#7761 ### Dependencies * chore(deps): update all non-major dependencies by @thomhurst in thomhurst/TUnit#7749 * chore(deps): update dependency mockolate to v4 by @thomhurst in thomhurst/TUnit#7757 * chore(deps): update dependency microsoft.templateengine.authoring.templateverifier to v11 by @thomhurst in thomhurst/TUnit#7718 * chore(deps): update all non-major dependencies by @thomhurst in thomhurst/TUnit#7751 ## New Contributors * @ahmedsamy56 made their first contribution in thomhurst/TUnit#7768 **Full Changelog**: thomhurst/TUnit@v1.84.0...v1.85.0 Commits viewable in [compare view](thomhurst/TUnit@v1.84.0...v1.87.12). </details> [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore <dependency name> major version` will close this group update PR and stop Dependabot creating any more for the specific dependency's major version (unless you unignore this specific dependency's major version or upgrade to it yourself) - `@dependabot ignore <dependency name> minor version` will close this group update PR and stop Dependabot creating any more for the specific dependency's minor version (unless you unignore this specific dependency's minor version or upgrade to it yourself) - `@dependabot ignore <dependency name>` will close this group update PR and stop Dependabot creating any more for the specific dependency (unless you unignore this specific dependency or upgrade to it yourself) - `@dependabot unignore <dependency name>` will remove all of the ignore conditions of the specified dependency - `@dependabot unignore <dependency name> <ignore condition>` will remove the ignore condition of the specified dependency and ignore conditions </details> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Description
Fixed value-type returns currently cross an object-returning behavior interface on every invocation. Ordinary mock setup also allocates duplicate call counts and rare feature state. This change keeps fixed returns typed, derives counts from the published call buffer, and allocates behavior locks separately from rare state.
Volatile.Writeand use it for verification counts. Remove the second count array, its holder, and oneInterlocked.Incrementper recorded call.RareState. Ordinary setups avoid rare-state allocation and the unnecessary out/ref lookup lock during invocation.Related Issue
Fixes #7825. Addresses the setup allocation work in #7824. Automatic capture storage (#7822) and the permanent invocation benchmark lifecycle (#7823) remain separate work.
Type of Change
Benchmark evidence
Release, .NET 11.0.0, SDK 11.0.100, BenchmarkDotNet 0.16.0-preview.2, Windows 11 x64, Intel Core i7-12700K, workstation GC. Baseline:
bddb7b706d704b74870ebb5a38eb9209c59899d6; final:ed13282b44ee6a3b8cc0a01711bde11e8663b07d.Two launches, six warmups, and ten measurement iterations per launch. Lifecycle cases target 200 ms per iteration. Invocation cases execute exactly 2,097,152 calls per iteration and recreate all mocks, including captured-argument history, outside the timed region. Timing runs are separate from builds and tests. Errors below are half-widths of 99.9% confidence intervals; time change uses the ratio of means.
Setupconfigures three fixed returns without invoking them.CallbackandVerifyinclude creation, setup, and invocation.FullWorkflowis the existing repository/logger workflow.IntegerandStringare configured invocation-only cases;UnconfiguredIntegerchecks the unmatched-call path. Creation is a control. Long-history invocation timings are sensitive to GC and batch size; these Windows results are not a direct comparison with the published Linux charts.Incremental measurements and snapshot provenance
The variants are cumulative source revisions. Each job compiles the same standalone harness against its selected saved
TUnit.Mocks.dll; the existing benchmark fixture assembly is held at the baseline revision.bddb7b706d704b74870ebb5a38eb9209c59899d681a9e3d00eb37e7a1fee0873f7c8401cf991a40a4494f4eb46b422e798df451b88790394ffe2a970ed13282b44ee6a3b8cc0a01711bde11e8663b07d(includes separate behavior lock and final dispatch guard cleanup)SHA-256 of the runtime DLLs:
1B72EA31475C097AC2841341877CA345326DA7A264C562D78723D79DD9C5E6B64ACDACACC8503F551AC8649FE238AE860A984B5293E4984983FF6D55BA33EFE1D784E8A5580FBB71352B95AF5F37B850A6D73729D9EBD7DD2CDF030365256060AEFA05E7879AD7B50667D7748562B896DE77C17F5C3E79479B39F0AA576EF236Validation
dotnet test --project tests/TUnit.Mocks.Tests --framework net10.0 -c Release: 1,538 passed.dotnet test --project tests/TUnit.Mocks.Tests --framework net11.0 -c Release: 1,538 passed.dotnet publish tests/TUnit.Mocks.Aot.Tests -c Release -f net11.0 -p:Aot=true --use-current-runtime: passed, including its trim/AOT warning guard.git diff --check: passed.The 22 added regression cases cover struct-copy semantics, nullable returns, state/out-ref/event side effects, raw-return cleanup after exceptions, concurrent buffer growth and clearing, and concurrent behavior/rare-state mutation. Relevant existing tests also cover composition, sequences, partial mocks, and generated dispatch. Full solution and cross-platform validation remain with CI;
TUnit.Engine.Testswas not run locally, per the repository guide.Checklist
Reproducing the comparison
In isolated worktrees for the revisions above, build the existing
benchmarks/TUnit.Mocks.Benchmarksproject withdotnet build -c Release -f net11.0. Copy each completebin/Release/net11.0output tobefore-bin,typed-bin,counts-bin, orafter-binbeside a temporarybenchmarksdirectory. Copy the baseline output tobaseline-binas well. FollowAGENTS.mdfor worktree ownership and locations.Place the project and source below in that temporary
benchmarksdirectory. CopyDirectory.Packages.propsandglobal.jsonfrom the baseline checkout there to reuse the exact package and SDK selections. Run from that directory:MockRuntimeComparison.csproj:Program.cs:Summary by CodeRabbit