Skip to content

Commit f940591

Browse files
committed
Skip resolve pipeline on cached objects if we can.
1 parent 901e224 commit f940591

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/Autofac/Core/Resolving/ResolveOperation.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ public object GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, i
9999
throw new ObjectDisposedException(ResolveOperationResources.TemporaryContextDisposed, innerException: null);
100100
}
101101

102+
// Fast-path: if the registration is shared and already cached, skip the entire
103+
// pipeline (context allocation, middleware chain, etc.). Only safe when:
104+
// - no event handlers or diagnostics are observing,
105+
// - the service pipeline is the default (no decorators or custom service middleware).
106+
if (request.Registration.Sharing == InstanceSharing.Shared &&
107+
request.ResolvePipeline == ServicePipelines.DefaultServicePipeline &&
108+
ResolveRequestBeginning is null &&
109+
!DiagnosticSource.IsEnabled() &&
110+
currentOperationScope.TryGetSharedInstance(request.Registration.Id, request.DecoratorTarget?.Id, out var cached))
111+
{
112+
return cached;
113+
}
114+
102115
// Create a new request context.
103116
var requestContext = new DefaultResolveRequestContext(this, request, currentOperationScope, DiagnosticSource);
104117

0 commit comments

Comments
 (0)