Skip to content

Commit

Permalink
Optimizing equality comparisons
Browse files Browse the repository at this point in the history
Fixed unitycontainer/abstractions#178
  • Loading branch information
ENikS committed Jun 14, 2020
1 parent 95746db commit 4c52862
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
9 changes: 5 additions & 4 deletions src/Builder/Context/BuilderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Runtime.CompilerServices;
using System.Security;
using Unity.Exceptions;
using Unity.Injection;
using Unity.Lifetime;
using Unity.Policy;
using Unity.Registration;
Expand Down Expand Up @@ -55,7 +56,7 @@ public object Resolve(Type type, string name)
var resolverOverride = Overrides[index];
// If matches with current parameter
if (resolverOverride is IResolve resolverPolicy &&
resolverOverride is IEquatable<NamedType> comparer && comparer.Equals(namedType))
resolverOverride is IMatch<NamedType> comparer && comparer.Match(namedType))
{
var context = this;

Expand Down Expand Up @@ -191,7 +192,7 @@ public object Resolve(ParameterInfo parameter, object value)
var resolverOverride = Overrides[index];

// If matches with current parameter
if (resolverOverride is IEquatable<ParameterInfo> comparer && comparer.Equals(parameter))
if (resolverOverride is IMatch<ParameterInfo> comparer && comparer.Match(parameter))
{
// Check if itself is a value
if (resolverOverride is IResolve resolverPolicy)
Expand Down Expand Up @@ -236,7 +237,7 @@ public object Resolve(PropertyInfo property, object value)
var resolverOverride = Overrides[index];

// Check if this parameter is overridden
if (resolverOverride is IEquatable<PropertyInfo> comparer && comparer.Equals(property))
if (resolverOverride is IMatch<PropertyInfo> comparer && comparer.Match(property))
{
// Check if itself is a value
if (resolverOverride is IResolve resolverPolicy)
Expand Down Expand Up @@ -291,7 +292,7 @@ public object Resolve(FieldInfo field, object value)
var resolverOverride = Overrides[index];

// Check if this parameter is overridden
if (resolverOverride is IEquatable<FieldInfo> comparer && comparer.Equals(field))
if (resolverOverride is IMatch<FieldInfo> comparer && comparer.Match(field))
{
// Check if itself is a value
if (resolverOverride is IResolve resolverPolicy)
Expand Down
25 changes: 13 additions & 12 deletions src/Legacy/TypeBasedOverride.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Reflection;
using Unity.Injection;

namespace Unity.Resolution
{
Expand All @@ -11,9 +12,9 @@ namespace Unity.Resolution
/// </summary>
[Obsolete("This type has been deprecated as degrading performance. Use ResolverOverride.OnType() instead.", false)]
public class TypeBasedOverride : ResolverOverride,
IEquatable<ParameterInfo>,
IEquatable<PropertyInfo>,
IEquatable<FieldInfo>
IMatch<ParameterInfo>,
IMatch<PropertyInfo>,
IMatch<FieldInfo>
{
#region Constructors

Expand Down Expand Up @@ -43,22 +44,22 @@ public override bool Equals(object obj)
return Value.Equals(obj);
}

public bool Equals(FieldInfo other)
public bool Match(FieldInfo other)
{
return Value is IEquatable<FieldInfo> info &&
info.Equals(other);
return Value is IMatch<FieldInfo> info &&
info.Match(other);
}

public bool Equals(PropertyInfo other)
public bool Match(PropertyInfo other)
{
return Value is IEquatable<PropertyInfo> info &&
info.Equals(other);
return Value is IMatch<PropertyInfo> info &&
info.Match(other);
}

public bool Equals(ParameterInfo other)
public bool Match(ParameterInfo other)
{
return Value is IEquatable<ParameterInfo> info &&
info.Equals(other);
return Value is IMatch<ParameterInfo> info &&
info.Match(other);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class GenericResolvedArrayParameterFixture
[TestMethod]
public void MatchesArrayOfGenericTypeOnly()
{
var parameterValue = (IEquatable<Type>)new GenericResolvedArrayParameter("T");
var parameterValue = (IMatch<Type>)new GenericResolvedArrayParameter("T");

Type genericTypeT
= this.GetType().GetTypeInfo().GetDeclaredMethod("GetT")
Expand All @@ -20,13 +20,13 @@ Type genericTypeU
= this.GetType().GetTypeInfo().GetDeclaredMethod("GetU")
.GetGenericArguments()[0];

Assert.IsFalse(parameterValue.Equals(genericTypeT));
Assert.IsFalse(parameterValue.Equals(genericTypeU));
Assert.IsFalse(parameterValue.Equals(typeof(object)));
Assert.IsTrue( parameterValue.Equals(genericTypeT.MakeArrayType(1)));
Assert.IsFalse(parameterValue.Equals(genericTypeT.MakeArrayType(2)));
Assert.IsFalse(parameterValue.Equals(genericTypeU.MakeArrayType(1)));
Assert.IsFalse(parameterValue.Equals(typeof(object[])));
Assert.IsFalse(parameterValue.Match(genericTypeT));
Assert.IsFalse(parameterValue.Match(genericTypeU));
Assert.IsFalse(parameterValue.Match(typeof(object)));
Assert.IsTrue( parameterValue.Match(genericTypeT.MakeArrayType(1)));
Assert.IsFalse(parameterValue.Match(genericTypeT.MakeArrayType(2)));
Assert.IsFalse(parameterValue.Match(genericTypeU.MakeArrayType(1)));
Assert.IsFalse(parameterValue.Match(typeof(object[])));
}

[TestMethod]
Expand Down

0 comments on commit 4c52862

Please sign in to comment.