Fix per-resolve closure allocation in resolve pipeline (#1493)#1494
Merged
Conversation
Since 9.2, the built middleware chain wrapped each stage in an Action passed to a shared ExecuteWithDiagnostics helper. That inner lambda captured the per-invocation ResolveRequestContext, so a fresh closure was allocated for every middleware stage on every resolve, regressing allocations vs. 9.1. Inline the diagnostics/metrics logic into each build-time lambda so they close only over build-time state (next, stage, stagePhase, stageName) and are allocated once per pipeline build, restoring 9.1 allocation behavior. Both the standard and metrics-enabled chains are fixed. Add a ResolvePipelineAllocationBenchmark mirroring the reported repro, and a PipelineBuilderTests regression test asserting a built pipeline allocates zero bytes per invocation.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1494 +/- ##
===========================================
+ Coverage 77.99% 78.24% +0.24%
===========================================
Files 218 218
Lines 5945 5944 -1
Branches 1273 1273
===========================================
+ Hits 4637 4651 +14
+ Misses 768 753 -15
Partials 540 540 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Jul 9, 2026
Closed
This was referenced Jul 13, 2026
This was referenced Jul 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1493.
Problem
@BrendanLally reported that per-resolve memory allocations increased in Autofac 9.2+ and traced it to
ResolvePipelineBuilder.BuildPipeline. The diagnosis is correct.The 9.2 "Consolidated metrics handling" refactor introduced a shared
ExecuteWithDiagnostics(context, stage, Action action)helper, called like:The inner
() => { ... }lambda capturescontext— a per-invocation parameter — so a fresh closure is allocated for every middleware stage on every resolve. In 9.1 the equivalent lambda closed over only build-time state and was allocated once per pipeline build (pipelines are cached).BuildMetricsMiddlewareChainhad the same defect.Fix
Inline the diagnostics/metrics logic into each build-time lambda so they close only over build-time state (
next,stage,stagePhase,stageName) and are allocated once per pipeline build. Both the standard and metrics-enabled chains are fixed. The two builders were extracted to static methods to keepBuildPipelinewithin the cognitive-complexity analyzer limit. Diagnostics and metrics behavior is unchanged.Validation
Added
ResolvePipelineAllocationBenchmark(mirroring the reported repro) and compared current source against the 9.1.0 NuGet package via the benchmark harness's baseline feature:The 9.1.0 numbers match the reporter's exactly, and the fix fully restores them (the timing regression is gone too).
Tests
Added
PipelineBuilderTests.InvokingBuiltPipelineDoesNotAllocatePerInvocation, asserting a built pipeline allocates zero bytes per invocation. Verified it fails on the pre-fix code (32 B/stage per resolve) and passes with the fix. Full core (870) and specification (498) suites pass;dotnet formatis clean.