Skip to content

Commit

Permalink
Memory optimization from #919: Autofac.Core.Registration.ServiceRegis…
Browse files Browse the repository at this point in the history
…trationInfo.
  • Loading branch information
tillig committed Aug 16, 2018
1 parent 1ad26e0 commit 06a0bbf
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/Autofac/Core/Registration/ServiceRegistrationInfo.cs
Expand Up @@ -48,13 +48,13 @@ internal class ServiceRegistrationInfo
/// List of service implementations coming from sources. Sources have priority over preserve-default implementations.
/// Implementations from sources are enumerated in preserve-default order, so the most default implementation comes first.
/// </summary>
private readonly List<IComponentRegistration> _sourceImplementations = new List<IComponentRegistration>();
private List<IComponentRegistration> _sourceImplementations = null;

/// <summary>
/// List of explicit service implementations specified with the PreserveExistingDefaults option.
/// Enumerated in preserve-defaults order, so the most default implementation comes first.
/// </summary>
private readonly List<IComponentRegistration> _preserveDefaultImplementations = new List<IComponentRegistration>();
private List<IComponentRegistration> _preserveDefaultImplementations = null;

[SuppressMessage("Microsoft.ApiDesignGuidelines", "CA2213", Justification = "The creator of the compponent registration is responsible for disposal.")]
private IComponentRegistration _defaultImplementation;
Expand Down Expand Up @@ -88,10 +88,18 @@ public IEnumerable<IComponentRegistration> Implementations
get
{
RequiresInitialization();
return Enumerable
.Reverse(_defaultImplementations)
.Concat(_sourceImplementations)
.Concat(_preserveDefaultImplementations);
var resultingCollection = Enumerable.Reverse(_defaultImplementations);
if (_sourceImplementations != null)
{
resultingCollection = resultingCollection.Concat(_sourceImplementations);
}

if (_preserveDefaultImplementations != null)
{
resultingCollection = resultingCollection.Concat(_preserveDefaultImplementations);
}

return resultingCollection;
}
}

Expand All @@ -114,18 +122,32 @@ public bool IsRegistered
}

private bool Any =>
_defaultImplementations.Any() ||
_sourceImplementations.Any() ||
_preserveDefaultImplementations.Any();
_defaultImplementations.Count > 0 ||
_sourceImplementations != null ||
_preserveDefaultImplementations != null;

public void AddImplementation(IComponentRegistration registration, bool preserveDefaults, bool originatedFromSource)
{
if (preserveDefaults)
{
if (originatedFromSource)
{
if (_sourceImplementations == null)
{
_sourceImplementations = new List<IComponentRegistration>();
}

_sourceImplementations.Add(registration);
}
else
{
if (_preserveDefaultImplementations == null)
{
_preserveDefaultImplementations = new List<IComponentRegistration>();
}

_preserveDefaultImplementations.Add(registration);
}
}
else
{
Expand All @@ -144,8 +166,8 @@ public bool TryGetRegistration(out IComponentRegistration registration)

registration = _defaultImplementation ?? (_defaultImplementation =
_defaultImplementations.LastOrDefault() ??
_sourceImplementations.FirstOrDefault() ??
_preserveDefaultImplementations.FirstOrDefault());
_sourceImplementations?.First() ??
_preserveDefaultImplementations?.First());

return registration != null;
}
Expand Down Expand Up @@ -214,7 +236,7 @@ public bool ShouldRecalculateAdaptersOn(IComponentRegistration registration)
// - Have already been initialized
// - Were created via a registration source (because we might be adding an equivalent explicit registration such as Func<T>)
// - Don't contain any registrations (because a registration source was added when no adaptee was present)
return IsInitialized && (_sourceImplementations.Any() || !Any);
return IsInitialized && (_sourceImplementations != null || !Any);
}
}
}

0 comments on commit 06a0bbf

Please sign in to comment.