Skip to content

Reduce automatic argument capture allocations for ordinary mock calls #7822

Closed

Description

@thomhurst

Summary

Every non-default Arg<T> in TUnit.Mocks wraps its matcher in a CapturingMatcher<T>. Matching calls allocate and populate a separate ConcurrentQueue<T?> even when the argument is an inline Arg.Any<T>() and the caller never reads Values or Latest. This duplicates argument retention alongside the engine's call history and adds substantial cost to short mock workflows.

Reproduction

Create a mock for an interface with int Add(int a, int b), configure mock.Add(Arg.Any<int>(), Arg.Any<int>()).Returns(42), and call mock.Object.Add(1, 2). Profile repeated fresh mocks, then compare the same engine setup with bare AnyMatcher<int>.Instance matchers through the generated-code-facing APIs.

At bddb7b7, a warmed allocation probe using GC.GetAllocatedBytesForCurrentThread() measured 2,400 B for engine creation, setup and the first captured call versus 1,184 B with two bare matchers. The 1,216 B difference includes two 32 B capture decorators and their first-call queue storage.

Relevant source:

Proposal

Reduce automatic capture's allocation and synchronization costs while preserving the documented Arg<T>.Values and Latest behavior. Evaluate compact typed storage or reuse of recorded arguments with equivalent capture semantics. Removing capture outright would change behavior and is not an acceptable fix.

Measure fresh-mock callbacks and full workflows as well as long invocation histories. Retain concurrent capture, partial-match rejection and capture-order coverage.

Environment

Windows 11 x64, Intel Core i7-12700K, SDK 11.0.100, Release; allocation probe on .NET 10.0.15. The .NET 11.0.0 full-workflow benchmark allocates 6,464 B per operation. EventPipe allocation sampling identifies integer queue segments and their slot arrays among its largest allocation sources.

Please reduce automatic capture overhead without weakening existing capture semantics.

Activity

  1. added
    enhancement
    performance
    P3
    on Nov 19, 2026
  2. thomhurst commented on Nov 19, 2026

    @thomhurst
    OwnerAuthor

    A diagnostic ablation confirms capture's contribution. On .NET 11.0.0, Release, eight measured iterations (five warmups, 150 ms target), temporarily replacing only the capture enqueue bodies with no-ops changes callback from 405.65 ns / 3,200 B to 308.38 ns / 1,536 B, full workflow from 1,079.28 ns / 6,464 B to 932.30 ns / 5,312 B, and the verification workflow from 415.38 ns / 3,040 B to 323.62 ns / 1,888 B. Baseline confirmation was run after the ablation to check drift. This is causal isolation, not a proposed implementation: disabling capture changes documented behavior. Capture explains 14% of full-workflow time here, so setup and call-record overhead also need attention (#7824).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3enhancementperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions