Skip to content

Commit

Permalink
Optimize by using value type IntPtr as BindingKeys instead of Type re…
Browse files Browse the repository at this point in the history
…ference
  • Loading branch information
PereViader committed Sep 24, 2024
1 parent 47331a9 commit d7965d1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions ManualDi.Main/ManualDi.Main/Building/DiContainerBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ManualDi.Main

public sealed class DiContainerBindings
{
private readonly Dictionary<Type, List<TypeBinding>> typeBindings;
private readonly Dictionary<IntPtr, List<TypeBinding>> typeBindings;
private readonly List<ContainerDelegate> injectDelegates;
private readonly List<ContainerDelegate> initializationDelegates;
private readonly List<ContainerDelegate> startupDelegates;
Expand Down Expand Up @@ -41,7 +41,7 @@ public DiContainerBindings(

public void AddBinding<TApparent, TConcrete>(TypeBinding<TApparent, TConcrete> typeBinding)
{
var apparentType = typeof(TApparent);
var apparentType = typeof(TApparent).TypeHandle.Value;
if (!typeBindings.TryGetValue(apparentType, out var bindings))
{
bindings = new List<TypeBinding>(1);
Expand All @@ -53,7 +53,7 @@ public void AddBinding<TApparent, TConcrete>(TypeBinding<TApparent, TConcrete> t

public void AddUnsafeBinding(UnsafeTypeBinding typeBinding)
{
var apparentType = typeBinding.ApparentType;
var apparentType = typeBinding.ApparentType.TypeHandle.Value;
if (!typeBindings.TryGetValue(apparentType, out var bindings))
{
bindings = new List<TypeBinding>(1);
Expand Down
8 changes: 4 additions & 4 deletions ManualDi.Main/ManualDi.Main/Container/DiContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ManualDi.Main
{
public sealed class DiContainer : IDiContainer
{
private readonly Dictionary<Type, List<TypeBinding>> allTypeBindings;
private readonly Dictionary<IntPtr, List<TypeBinding>> allTypeBindings;
private readonly IDiContainer? parentDiContainer;
private readonly BindingContext bindingContext = new();

Expand All @@ -16,7 +16,7 @@ public sealed class DiContainer : IDiContainer
private TypeBinding? injectedTypeBinding;

public DiContainer(
Dictionary<Type, List<TypeBinding>> allTypeBindings,
Dictionary<IntPtr, List<TypeBinding>> allTypeBindings,
IDiContainer? parentDiContainer,
int? initializationsCount = null,
int? initializationsOnDepthCount = null,
Expand Down Expand Up @@ -94,7 +94,7 @@ private object ResolveBinding(TypeBinding typeBinding)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private TypeBinding? GetTypeForConstraint(Type type, FilterBindingDelegate? filterBindingDelegate)
{
if (!allTypeBindings.TryGetValue(type, out var typeBindings))
if (!allTypeBindings.TryGetValue(type.TypeHandle.Value, out var typeBindings))
{
return null;
}
Expand Down Expand Up @@ -136,7 +136,7 @@ private object ResolveBinding(TypeBinding typeBinding)

public void ResolveAllContainer(Type type, FilterBindingDelegate? filterBindingDelegate, IList resolutions)
{
if (allTypeBindings.TryGetValue(type, out var typeBindings))
if (allTypeBindings.TryGetValue(type.TypeHandle.Value, out var typeBindings))
{
bindingContext.InjectedIntoTypeBinding = injectedTypeBinding;

Expand Down

0 comments on commit d7965d1

Please sign in to comment.