Skip to content

Commit

Permalink
Memory optimization from #919: Autofac.Builder.RegistrationBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
tillig committed Aug 16, 2018
1 parent 26e9606 commit 0a3a081
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/Autofac/Builder/RegistrationBuilder.cs
Expand Up @@ -133,7 +133,7 @@ public static class RegistrationBuilder
builder.RegistrationStyle.Id,
builder.RegistrationData,
builder.ActivatorData.Activator,
builder.RegistrationData.Services,
builder.RegistrationData.Services.ToArray(),
builder.RegistrationStyle.Target);
}

Expand All @@ -149,7 +149,7 @@ public static class RegistrationBuilder
Guid id,
RegistrationData data,
IInstanceActivator activator,
IEnumerable<Service> services)
Service[] services)
{
return CreateRegistration(id, data, activator, services, null);
}
Expand All @@ -170,7 +170,7 @@ public static class RegistrationBuilder
Guid id,
RegistrationData data,
IInstanceActivator activator,
IEnumerable<Service> services,
Service[] services,
IComponentRegistration target)
{
if (activator == null) throw new ArgumentNullException(nameof(activator));
Expand All @@ -179,9 +179,15 @@ public static class RegistrationBuilder
var limitType = activator.LimitType;
if (limitType != typeof(object))
{
foreach (var ts in services.OfType<IServiceWithType>())
foreach (var ts in services)
{
if (!ts.ServiceType.GetTypeInfo().IsAssignableFrom(limitType.GetTypeInfo()))
var asServiceWithType = ts as IServiceWithType;
if (asServiceWithType == null)
{
continue;
}

if (!asServiceWithType.ServiceType.GetTypeInfo().IsAssignableFrom(limitType.GetTypeInfo()))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, RegistrationBuilderResources.ComponentDoesNotSupportService, limitType, ts));
}
Expand Down
Expand Up @@ -60,7 +60,7 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Fun
if (registrationAccessor == null) throw new ArgumentNullException(nameof(registrationAccessor));

Type constructedImplementationType;
IEnumerable<Service> services;
Service[] services;
if (OpenGenericServiceBinder.TryBindServiceType(service, _registrationData.Services, _activatorData.ImplementationType, out constructedImplementationType, out services))
{
var swt = (IServiceWithType)service;
Expand Down
Expand Up @@ -60,7 +60,7 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Fun
if (registrationAccessor == null) throw new ArgumentNullException(nameof(registrationAccessor));

Type constructedImplementationType;
IEnumerable<Service> services;
Service[] services;
if (OpenGenericServiceBinder.TryBindServiceType(service, _registrationData.Services, _activatorData.ImplementationType, out constructedImplementationType, out services))
{
yield return RegistrationBuilder.CreateRegistration(
Expand Down
Expand Up @@ -41,7 +41,7 @@ internal static class OpenGenericServiceBinder
IEnumerable<Service> configuredOpenGenericServices,
Type openGenericImplementationType,
out Type constructedImplementationType,
out IEnumerable<Service> constructedServices)
out Service[] constructedServices)
{
var swt = service as IServiceWithType;
if (swt != null && swt.ServiceType.GetTypeInfo().IsGenericType)
Expand Down

0 comments on commit 0a3a081

Please sign in to comment.