Skip to content

Commit

Permalink
Add child container benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed Oct 12, 2022
1 parent acd8289 commit fcc1d02
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 21 deletions.
58 changes: 58 additions & 0 deletions sandbox/stashbox.benchmarks/ChildContainerBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
extern alias from_nuget;
extern alias from_project;
using BenchmarkDotNet.Attributes;

namespace Stashbox.Benchmarks
{
[MemoryDiagnoser]
public class ChildContainerBenchmarks
{
private readonly from_nuget::Stashbox.IStashboxContainer oldContainer =
new from_nuget::Stashbox.StashboxContainer();

private readonly from_project::Stashbox.IStashboxContainer newContainer =
new from_project::Stashbox.StashboxContainer();

[GlobalSetup]
public void Setup()
{
this.oldContainer.Register<A>()
.Register<B>()
.Register<C>();

this.newContainer.Register<A>()
.Register<B>()
.Register<C>();
}

[Benchmark(Baseline = true)]
public object Old()
{
var child = this.oldContainer.CreateChildContainer();
child.Register<B>();
return this.oldContainer.Resolve(typeof(A));
}

[Benchmark]
public object New()
{
var child = this.newContainer.CreateChildContainer();
child.Register<B>();
return this.newContainer.Resolve(typeof(A));
}

class A
{
public A(B b, C c)
{ }
}

class B
{
public B(C c)
{ }
}

class C { }
}
}
1 change: 1 addition & 0 deletions sandbox/stashbox.benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static void Main()
BenchmarkConverter.TypeToBenchmarks( typeof(BeginScopeBenchmarks), config),
BenchmarkConverter.TypeToBenchmarks( typeof(SingletonBenchmarks), config),
BenchmarkConverter.TypeToBenchmarks( typeof(DecoratorBenchmarks), config),
BenchmarkConverter.TypeToBenchmarks( typeof(ChildContainerBenchmarks), config),
});

Console.ReadKey();
Expand Down
2 changes: 1 addition & 1 deletion sandbox/stashbox.benchmarks/Stashbox.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="Stashbox" Version="5.4.0" />
<PackageReference Include="Stashbox" Version="5.4.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
Expand Down
4 changes: 2 additions & 2 deletions src/Lifetime/LifetimeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected LifetimeDescriptor()
if (!this.StoreResultInLocalVariable)
return this.BuildLifetimeAppliedExpression(serviceRegistration, resolutionContext, requestedType);

var variable = resolutionContext.GetKnownVariableOrDefault(serviceRegistration.RegistrationId);
var variable = resolutionContext.DefinedVariables.GetOrDefault(serviceRegistration.RegistrationId);
if (variable != null)
return variable;

Expand All @@ -76,7 +76,7 @@ protected LifetimeDescriptor()
if (!IsRegistrationOutputCacheable(serviceRegistration, resolutionContext))
return ExpressionBuilder.BuildExpressionForRegistration(serviceRegistration, resolutionContext, requestedType);

var expression = resolutionContext.GetCachedExpression(serviceRegistration.RegistrationId);
var expression = resolutionContext.ExpressionCache.GetOrDefault(serviceRegistration.RegistrationId);
if (expression != null)
return expression;

Expand Down
22 changes: 5 additions & 17 deletions src/Resolution/ResolutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ internal class PerRequestConfiguration
public bool FactoryDelegateCacheEnabled { get; set; }
}

private readonly Tree<Expression> expressionCache;

internal readonly Tree<Expression> ExpressionCache;
internal readonly Utils.Data.Stack<object> ScopeNames;
internal readonly PerRequestConfiguration RequestConfiguration;
internal readonly IContainerContext RequestInitiatorContainerContext;
Expand Down Expand Up @@ -84,7 +83,7 @@ private ResolutionContext(IEnumerable<object> initialScopeNames,
this.RemainingDecorators = new ExpandableArray<Type, Utils.Data.Stack<ServiceRegistration>>();
this.CurrentDecorators = new ExpandableArray<ServiceRegistration>();
this.CircularDependencyBarrier = new Utils.Data.Stack<int>();
this.expressionCache = new Tree<Expression>();
this.ExpressionCache = new Tree<Expression>();
this.FactoryCache = new Tree<Func<IResolutionScope, IRequestContext, object>>();
this.NullResultAllowed = nullResultAllowed;
this.IsRequestedFromRoot = isRequestedFromRoot;
Expand Down Expand Up @@ -139,7 +138,7 @@ private ResolutionContext(PerRequestConfiguration perRequestConfiguration,
this.RemainingDecorators = remainingDecorators;
this.CurrentDecorators = currentDecorators;
this.CircularDependencyBarrier = circularDependencyBarrier;
this.expressionCache = cachedExpressions;
this.ExpressionCache = cachedExpressions;
this.FactoryCache = factoryCache;
this.NullResultAllowed = nullResultAllowed;
this.IsRequestedFromRoot = isRequestedFromRoot;
Expand Down Expand Up @@ -182,19 +181,8 @@ public void AddDefinedVariable(int key, ParameterExpression parameter) =>
public void AddDefinedVariable(ParameterExpression parameter) =>
this.DefinedVariables.Add(RuntimeHelpers.GetHashCode(parameter), parameter);

/// <summary>
/// Gets an already defined global variable.
/// </summary>
/// <param name="key">The key of the variable.</param>
/// <returns>The variable.</returns>
public ParameterExpression? GetKnownVariableOrDefault(int key) =>
this.DefinedVariables.GetOrDefault(key);

internal void CacheExpression(int key, Expression expression) =>
this.expressionCache.Add(key, expression);

internal Expression? GetCachedExpression(int key) =>
this.expressionCache.GetOrDefault(key);
this.ExpressionCache.Add(key, expression);

internal static ResolutionContext BeginTopLevelContext(
IEnumerable<object> initialScopeNames,
Expand Down Expand Up @@ -321,7 +309,7 @@ private ResolutionContext Clone(
remainingDecorators ?? this.RemainingDecorators,
currentDecorators ?? this.CurrentDecorators,
this.CircularDependencyBarrier,
cachedExpressions ?? this.expressionCache,
cachedExpressions ?? this.ExpressionCache,
this.FactoryCache,
scopeNames ?? this.ScopeNames,
currentScopeParameter ?? this.CurrentScopeParameter,
Expand Down
5 changes: 4 additions & 1 deletion src/Resolution/ServiceContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public ServiceContext(Expression serviceExpression, ServiceRegistration? service
this.ServiceRegistration = serviceRegistration;
}

internal bool IsEmpty() => Equals(this, Empty);
internal bool IsEmpty() => this.Equals(Empty);

private bool Equals(ServiceContext other) =>
ReferenceEquals(ServiceExpression, other.ServiceExpression) && ReferenceEquals(ServiceRegistration, other.ServiceRegistration);

internal static readonly ServiceContext Empty = default;
}
Expand Down

0 comments on commit fcc1d02

Please sign in to comment.