Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create parallel data structure for types to fasten up lookups later. #618

Merged
merged 10 commits into from
Aug 14, 2022
8 changes: 7 additions & 1 deletion src/Castle.Windsor/Core/ComponentModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public sealed class ComponentModel : GraphNode

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly List<Type> services = new List<Type>(4);
private HashSet<Type> servicesFast = new HashSet<Type>();
sqeezy marked this conversation as resolved.
Show resolved Hide resolved

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private ComponentName componentName;
Expand Down Expand Up @@ -331,6 +332,11 @@ public IEnumerable<Type> Services
{
get { return services; }
}

internal HashSet<Type> FastServices
{
get { return servicesFast; }
}
sqeezy marked this conversation as resolved.
Show resolved Hide resolved

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal ParameterModelCollection ParametersInternal
Expand Down Expand Up @@ -378,7 +384,7 @@ public void AddService(Type type)
type));
}

ComponentServicesUtil.AddService(services, type);
ComponentServicesUtil.AddService(services,servicesFast, type);
sqeezy marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down
8 changes: 6 additions & 2 deletions src/Castle.Windsor/Core/Internal/ComponentServicesUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ public class ComponentServicesUtil
{
private static readonly TypeByInheritanceDepthMostSpecificFirstComparer comparer = new TypeByInheritanceDepthMostSpecificFirstComparer();

public static void AddService(IList<Type> existingServices, Type newService)
public static void AddService(IList<Type> existingServices,HashSet<Type> lookup, Type newService)
sqeezy marked this conversation as resolved.
Show resolved Hide resolved
{
if (existingServices.Contains(newService))
if (lookup.Contains(newService))
{
return;
}
if (newService.GetTypeInfo().IsInterface)
{
existingServices.Add(newService);
lookup.Add(newService);
return;
}
if (newService.GetTypeInfo().IsClass == false)
Expand All @@ -44,18 +45,21 @@ public static void AddService(IList<Type> existingServices, Type newService)
if (existingServices[i].GetTypeInfo().IsInterface)
{
existingServices.Insert(i, newService);
lookup.Add(newService);
}
var result = comparer.Compare(newService, existingServices[i]);
if (result < 0)
{
existingServices.Insert(i, newService);
lookup.Add(newService);
return;
}
if (result == 0)
{
return;
}
}
lookup.Add(newService);
existingServices.Add(newService);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Castle.Windsor/MicroKernel/Handlers/AbstractHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public object Resolve(CreationContext context)

public virtual bool Supports(Type service)
{
return ComponentModel.Services.Contains(service);
return ComponentModel.FastServices.Contains(service);
sqeezy marked this conversation as resolved.
Show resolved Hide resolved
}

public virtual bool SupportsAssignable(Type service)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class ComponentRegistration<TService> : IRegistration
{
private readonly List<IComponentModelDescriptor> descriptors = new List<IComponentModelDescriptor>();
private readonly List<Type> potentialServices = new List<Type>();
private readonly HashSet<Type> potentialServicesFast = new HashSet<Type>();
sqeezy marked this conversation as resolved.
Show resolved Hide resolved

private bool ifComponentRegisteredIgnore;
private Type implementation;
Expand Down Expand Up @@ -473,7 +474,7 @@ public ComponentRegistration<TService> Forward(IEnumerable<Type> types)
{
foreach (var type in types)
{
ComponentServicesUtil.AddService(potentialServices, type);
ComponentServicesUtil.AddService(potentialServices,potentialServicesFast , type);
sqeezy marked this conversation as resolved.
Show resolved Hide resolved
}
return this;
}
Expand Down