Skip to content

Commit 5c11cfb

Browse files
committed
Minor optimizations
1 parent b75ba78 commit 5c11cfb

File tree

6 files changed

+65
-7
lines changed

6 files changed

+65
-7
lines changed

package.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<Version>5.8.4</Version>
4+
<Version>5.8.5</Version>
55
<PackageReleaseNotes>This package is distributed as .NET Standard 1.0, .NET 4.0, 4.5, 4.6, 4.7 package.</PackageReleaseNotes>
66
</PropertyGroup>
77

src/Builder/BuilderContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public BuilderContext(IBuilderContext original, IEnumerable<BuilderStrategy> cha
6262
_ownsOverrides = true;
6363
}
6464

65-
protected BuilderContext(IBuilderContext original, InternalRegistration registration)
65+
internal BuilderContext(IBuilderContext original, InternalRegistration registration)
6666
{
6767
var parent = (BuilderContext)original;
6868

src/UnityContainer.Implementation.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public partial class UnityContainer
7373
internal GetPolicyDelegate GetPolicy;
7474
internal SetPolicyDelegate SetPolicy;
7575
internal ClearPolicyDelegate ClearPolicy;
76-
internal Func<Type, string, bool> RegistrationExists;
7776

7877
private Func<Type, string, IPolicySet> _get;
7978
private Func<Type, string, Type, IPolicySet> _getGenericRegistration;
@@ -115,7 +114,6 @@ public UnityContainer()
115114
GetPolicy = Get;
116115
SetPolicy = Set;
117116
ClearPolicy = Clear;
118-
RegistrationExists = (type, name) => null != _get(type, name);
119117

120118
// TODO: Initialize disposables
121119
_lifetimeContainer.Add(_strategies);
@@ -180,7 +178,6 @@ private UnityContainer(UnityContainer parent)
180178
GetPolicy = parent.GetPolicy;
181179
SetPolicy = CreateAndSetPolicy;
182180
ClearPolicy = delegate { };
183-
RegistrationExists = (type, name) => null != _get(type, name);
184181

185182
// Strategies
186183
_strategies = _parent._strategies;

src/UnityContainer.Registration.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,67 @@ private bool IsTypeTypeExplicitlyRegisteredLocally(Type type)
217217
return _parent?._isTypeExplicitlyRegistered(type) ?? false;
218218
}
219219

220+
internal bool RegistrationExists(Type type, string name)
221+
{
222+
IPolicySet defaultRegistration = null;
223+
IPolicySet noNameRegistration = null;
224+
225+
var hashCode = (type?.GetHashCode() ?? 0) & 0x7FFFFFFF;
226+
for (var container = this; null != container; container = container._parent)
227+
{
228+
if (null == _registrations) continue;
229+
230+
var targetBucket = hashCode % container._registrations.Buckets.Length;
231+
for (var i = container._registrations.Buckets[targetBucket]; i >= 0; i = container._registrations.Entries[i].Next)
232+
{
233+
if (container._registrations.Entries[i].HashCode != hashCode ||
234+
container._registrations.Entries[i].Key != type)
235+
{
236+
continue;
237+
}
238+
239+
var registry = container._registrations.Entries[i].Value;
240+
241+
if (null != registry[name]) return true;
242+
if (null == defaultRegistration) defaultRegistration = registry[string.Empty];
243+
if (null != name && null == noNameRegistration) noNameRegistration = registry[null];
244+
}
245+
}
246+
247+
if (null != defaultRegistration) return true;
248+
if (null != noNameRegistration) return true;
249+
250+
var info = type.GetTypeInfo();
251+
if (!info.IsGenericType) return false;
252+
253+
type = info.GetGenericTypeDefinition();
254+
hashCode = (type?.GetHashCode() ?? 0) & 0x7FFFFFFF;
255+
for (var container = this; null != container; container = container._parent)
256+
{
257+
if (null == _registrations) continue;
258+
259+
var targetBucket = hashCode % container._registrations.Buckets.Length;
260+
for (var i = container._registrations.Buckets[targetBucket]; i >= 0; i = container._registrations.Entries[i].Next)
261+
{
262+
if (container._registrations.Entries[i].HashCode != hashCode ||
263+
container._registrations.Entries[i].Key != type)
264+
{
265+
continue;
266+
}
267+
268+
var registry = container._registrations.Entries[i].Value;
269+
270+
if (null != registry[name]) return true;
271+
if (null == defaultRegistration) defaultRegistration = registry[string.Empty];
272+
if (null != name && null == noNameRegistration) noNameRegistration = registry[null];
273+
}
274+
}
275+
276+
if (null != defaultRegistration) return true;
277+
return null != noNameRegistration;
278+
}
279+
280+
220281
#endregion
221282

222283

tests/Unity.Specification/Unity.Specification.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0-preview-20180307-01" />
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
1010
<PackageReference Include="MSTest.TestAdapter" Version="1.3.0-beta2" />
1111
<PackageReference Include="MSTest.TestFramework" Version="1.3.0-beta2" />
1212
</ItemGroup>

tests/Unity.Tests/Unity.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0-preview-20180307-01" />
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
1010
<PackageReference Include="MSTest.TestAdapter" Version="1.3.0-beta2" />
1111
<PackageReference Include="MSTest.TestFramework" Version="1.3.0-beta2" />
1212
</ItemGroup>

0 commit comments

Comments
 (0)