Skip to content

Commit 89ddc69

Browse files
#136 Insufficient performance
1 parent 51dc5da commit 89ddc69

8 files changed

Lines changed: 156 additions & 59 deletions

File tree

src/Pure.DI.Core/Core/DependencyGraphBuilder.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
3333
var setup = ctx.Setup;
3434
var nodes = ctx.Nodes;
3535
var accumulators = ctx.Accumulators;
36-
var nodesCache = ctx.NodesCache;
3736
var nodesLength = nodes.Length;
3837
var map = new Dictionary<Injection, DependencyNode>(nodesLength);
3938
var contextMap = new Dictionary<Injection, DependencyNode>(nodesLength);
@@ -120,7 +119,7 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
120119
var contextTag = GetContextTag(injection, newNode);
121120
var newInjection = injection with { Tag = contextTag ?? injection.Tag };
122121
edges.Add(new Dependency(true, newNode, newInjection, targetNode, position));
123-
queue.Enqueue(CreateNewProcessingNode(nodesCache, newInjection.Tag, newNode));
122+
queue.Enqueue(CreateNewProcessingNode(newInjection.Tag, newNode));
124123
}
125124

126125
continue;
@@ -135,7 +134,7 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
135134
{
136135
if (!marker.IsMarkerBased(setup, sourceNode!.Type))
137136
{
138-
queue.Enqueue(CreateNewProcessingNode(nodesCache, injection.Tag, sourceNode));
137+
queue.Enqueue(CreateNewProcessingNode(injection.Tag, sourceNode));
139138
continue;
140139
}
141140
}
@@ -156,7 +155,7 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
156155
{
157156
var contextTag = GetContextTag(injection, newNode);
158157
var newInjection = injection with { Tag = contextTag ?? injection.Tag };
159-
var newProcessingNode = CreateNewProcessingNode(nodesCache, newInjection.Tag, newNode);
158+
var newProcessingNode = CreateNewProcessingNode(newInjection.Tag, newNode);
160159
UpdateMap(newInjection, newNode);
161160
queue.Enqueue(newProcessingNode);
162161
}
@@ -207,7 +206,7 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
207206
if (genericNode is not null)
208207
{
209208
UpdateMap(newInjection, genericNode);
210-
queue.Enqueue(CreateNewProcessingNode(nodesCache, newInjection.Tag, genericNode));
209+
queue.Enqueue(CreateNewProcessingNode(newInjection.Tag, genericNode));
211210
foreach (var contract in genericBinding.Contracts.Where(i => i.ContractType is not null))
212211
{
213212
foreach (var tag in contract.Tags.Select(i => i.Value).DefaultIfEmpty(null))
@@ -265,7 +264,7 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
265264
var contextTag = GetContextTag(injection, newNode);
266265
var newInjection = injection with { Tag = contextTag ?? injection.Tag };
267266
UpdateMap(newInjection, newNode);
268-
var processingNode = CreateNewProcessingNode(nodesCache, newInjection.Tag, newNode);
267+
var processingNode = CreateNewProcessingNode(newInjection.Tag, newNode);
269268
queue.Enqueue(processingNode);
270269
}
271270

@@ -274,7 +273,7 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
274273
}
275274

276275
// OnCannotResolve
277-
if (TryCreateOnCannotResolve(nodesCache, setup, typeConstructor, targetNode, injection, ref maxBindingId, map, processed))
276+
if (TryCreateOnCannotResolve(setup, typeConstructor, targetNode, injection, ref maxBindingId, map, processed))
278277
{
279278
continue;
280279
}
@@ -300,7 +299,7 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
300299
var contextTag = GetContextTag(injection, newNode);
301300
var newInjection = injection with { Tag = contextTag ?? injection.Tag };
302301
UpdateMap(newInjection, newNode);
303-
queue.Enqueue(CreateNewProcessingNode(nodesCache, newInjection.Tag, newNode));
302+
queue.Enqueue(CreateNewProcessingNode(newInjection.Tag, newNode));
304303
}
305304

306305
continue;
@@ -358,7 +357,7 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
358357
var contextTag = GetContextTag(injection, newNode);
359358
var newInjection = injection with { Tag = contextTag ?? injection.Tag };
360359
UpdateMap(newInjection, newNode);
361-
queue.Enqueue(CreateNewProcessingNode(nodesCache, newInjection.Tag, newNode));
360+
queue.Enqueue(CreateNewProcessingNode(newInjection.Tag, newNode));
362361
}
363362

364363
continue;
@@ -383,6 +382,11 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
383382

384383
if (!disableAutoBinding)
385384
{
385+
/*if (!Debugger.IsAttached)
386+
{
387+
Debugger.Launch();
388+
}*/
389+
386390
var autoBinding = bindingsFactory.CreateAutoBinding(setup, targetNode, injection, typeConstructor, ++maxBindingId);
387391
var autoNodes = dependencyNodePrioritizer.SortByPriority(nodesFactory.CreateNodes(setup, typeConstructor, autoBinding)).ToList();
388392
foreach (var autoNode in autoNodes)
@@ -399,7 +403,7 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
399403
{
400404
var contextTag = GetContextTag(injection, newNode);
401405
var newInjection = injection with { Tag = contextTag ?? injection.Tag };
402-
var newProcessingNode = CreateNewProcessingNode(nodesCache, newInjection.Tag, newNode);
406+
var newProcessingNode = CreateNewProcessingNode(newInjection.Tag, newNode);
403407
UpdateMap(newInjection, newNode);
404408
queue.Enqueue(newProcessingNode);
405409
}
@@ -409,7 +413,7 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
409413
}
410414

411415
// OnCannotResolve
412-
if (TryCreateOnCannotResolve(nodesCache, setup, typeConstructor, targetNode, injection, ref maxBindingId, map, processed))
416+
if (TryCreateOnCannotResolve(setup, typeConstructor, targetNode, injection, ref maxBindingId, map, processed))
413417
{
414418
continue;
415419
}
@@ -513,9 +517,7 @@ private MdConstructKind GetConstructKind(INamedTypeSymbol geneticType)
513517
});
514518
}
515519

516-
private bool TryCreateOnCannotResolve(
517-
ICache<ProcessingNodeKey, IProcessingNode> nodesCache,
518-
MdSetup setup,
520+
private bool TryCreateOnCannotResolve(MdSetup setup,
519521
ITypeConstructor typeConstructor,
520522
DependencyNode ownerNode,
521523
Injection unresolvedInjection,
@@ -549,7 +551,7 @@ private bool TryCreateOnCannotResolve(
549551
foreach (var onCannotResolveNode in onCannotResolveNodes)
550552
{
551553
map[unresolvedInjection] = onCannotResolveNode;
552-
processed.Add(CreateNewProcessingNode(nodesCache, unresolvedInjection.Tag, onCannotResolveNode));
554+
processed.Add(CreateNewProcessingNode(unresolvedInjection.Tag, onCannotResolveNode));
553555
return true;
554556
}
555557
}
@@ -558,8 +560,8 @@ private bool TryCreateOnCannotResolve(
558560
return false;
559561
}
560562

561-
private IProcessingNode CreateNewProcessingNode(ICache<ProcessingNodeKey, IProcessingNode> nodesCache, object? contextTag, DependencyNode dependencyNode) =>
562-
processingNodeBuilder.Build(new ProcessingNodeContext(nodesCache, dependencyNode, contextTag));
563+
private IProcessingNode CreateNewProcessingNode(object? contextTag, DependencyNode dependencyNode) =>
564+
processingNodeBuilder.Build(new ProcessingNodeContext(dependencyNode, contextTag));
563565

564566
private static object? GetContextTag(Injection injection, DependencyNode node) =>
565567
node.Factory is { Source.HasContextTag: true } ? injection.Tag : null;

src/Pure.DI.Core/Core/LocalCache.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/Pure.DI.Core/Core/Models/GraphBuildContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
record GraphBuildContext(
44
MdSetup Setup,
55
in ImmutableArray<IProcessingNode> Nodes,
6-
in ImmutableDictionary<ISymbol, ImmutableArray<MdAccumulator>> Accumulators,
7-
ICache<ProcessingNodeKey, IProcessingNode> NodesCache)
6+
in ImmutableDictionary<ISymbol, ImmutableArray<MdAccumulator>> Accumulators)
87
{
98
public IGraph<DependencyNode, Dependency>? Graph { get; set; }
109
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace Pure.DI.Core.Models;
22

33
readonly record struct ProcessingNodeContext(
4-
ICache<ProcessingNodeKey, IProcessingNode> Cache,
54
DependencyNode Node,
65
object? ContextTag,
76
ISet<Injection>? Contracts = null);

src/Pure.DI.Core/Core/ProcessingNodeBuilder.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ class ProcessingNodeBuilder(
55
Func<IInjectionsWalker> injectionsWalkerFactory)
66
: IFastBuilder<ProcessingNodeContext, IProcessingNode>
77
{
8-
public IProcessingNode Build(in ProcessingNodeContext ctx) =>
9-
ctx.Cache.Get(new ProcessingNodeKey(ctx.Node.Variation, ctx.Node, ctx.ContextTag) { Contracts = ctx.Contracts }, Build);
10-
11-
private IProcessingNode Build(ProcessingNodeKey key)
8+
public IProcessingNode Build(in ProcessingNodeContext ctx)
129
{
13-
var contracts = key.Contracts ?? contractsBuilder.Build(new ContractsBuildContext(key.Node.Binding, key.ContextTag, key.ContextTag));
10+
var node = ctx.Node;
11+
var tag = ctx.ContextTag;
12+
var contracts = ctx.Contracts ?? contractsBuilder.Build(new ContractsBuildContext(node.Binding, tag, tag));
1413
var injectionsWalker = injectionsWalkerFactory();
15-
injectionsWalker.VisitDependencyNode(Unit.Shared, key.Node);
14+
injectionsWalker.VisitDependencyNode(Unit.Shared, node);
1615
var injections = injectionsWalker.GetResult();
17-
return new ProcessingNode(key.Node, contracts, injections);
16+
return new ProcessingNode(node, contracts, injections);
1817
}
1918
}

src/Pure.DI.Core/Core/VariationalDependencyGraphBuilder.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ sealed class VariationalDependencyGraphBuilder(
2020
IFastBuilder<ProcessingNodeContext, IProcessingNode> processingNodeBuilder,
2121
IRegistryManager<int> bindingsRegistryManager,
2222
ILocationProvider locationProvider,
23-
[Tag(Tag.LocalCache)] Func<ICache<ProcessingNodeKey, IProcessingNode>> nodesCacheFactory,
2423
IDependencyNodePrioritizer dependencyNodePrioritizer,
2524
CancellationToken cancellationToken)
2625
: IBuilder<MdSetup, DependencyGraph?>
@@ -32,7 +31,6 @@ sealed class VariationalDependencyGraphBuilder(
3231
var allNodes = new List<IProcessingNode>();
3332
var injections = new Dictionary<Injection, DependencyNode>();
3433
var allOverriddenInjections = new HashSet<Injection>();
35-
var nodesCache = nodesCacheFactory();
3634
foreach (var node in rawNodes)
3735
{
3836
var contracts = contractsBuilder.Build(new ContractsBuildContext(node.Binding, MdTag.ContextTag, MdTag.AnyTag));
@@ -80,7 +78,7 @@ sealed class VariationalDependencyGraphBuilder(
8078

8179
if (isRoot || contracts.Count > 0)
8280
{
83-
allNodes.Add(processingNodeBuilder.Build(new ProcessingNodeContext(nodesCache, node, MdTag.ContextTag, contracts)));
81+
allNodes.Add(processingNodeBuilder.Build(new ProcessingNodeContext(node, MdTag.ContextTag, contracts)));
8482
}
8583
}
8684

@@ -89,14 +87,16 @@ sealed class VariationalDependencyGraphBuilder(
8987
var maxIterations = globalProperties.MaxIterations;
9088
var maxAttempts = 0x2000;
9189
DependencyGraph? dependencyGraph = null;
90+
9291
var accumulators = setup.Accumulators
9392
.GroupBy(acc => acc.AccumulatorType, SymbolEqualityComparer.Default)
9493
.ToImmutableDictionary(i => i.Key!, i => i.ToImmutableArray(), SymbolEqualityComparer.Default);
94+
9595
var buildCtx = new GraphBuildContext(
9696
setup,
9797
ImmutableArray<IProcessingNode>.Empty,
98-
accumulators,
99-
nodesCache);
98+
accumulators);
99+
100100
while (nodeVariator.TryGetNext(setsOfOptions, out var nodes))
101101
{
102102
if (maxAttempts-- <= 0)
@@ -167,14 +167,15 @@ sealed class VariationalDependencyGraphBuilder(
167167

168168
IProcessingNode CreateProcessingNode(DependencyNode dependencyNode)
169169
{
170-
var processingNode = processingNodeBuilder.Build(new ProcessingNodeContext(nodesCache, dependencyNode, MdTag.ContextTag, contractsBuilder.Build(new ContractsBuildContext(dependencyNode.Binding, MdTag.ContextTag, MdTag.AnyTag))));
170+
var processingNode = processingNodeBuilder.Build(new ProcessingNodeContext(dependencyNode, MdTag.ContextTag, contractsBuilder.Build(new ContractsBuildContext(dependencyNode.Binding, MdTag.ContextTag, MdTag.AnyTag))));
171171
allNodes.Add(processingNode);
172172
return processingNode;
173173
}
174174
}
175175

176176
return dependencyGraph;
177177
}
178+
178179
private void RegisterNode(MdSetup setup, DependencyNode node)
179180
{
180181
var binding = node.Binding;

src/Pure.DI.Core/Generator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ private void Setup() => DI.Setup()
5959
.Transient(_ => Compiled | CultureInvariant | Singleline | IgnoreCase)
6060
.Transient((RegexOptions options) => new Func<string, Regex>(p => new Regex(p, options)))
6161
.Transient<ApiInvocationProcessor, DependencyGraphBuilder, TypeConstructor, BindingBuilder, SetupContextRewriter, SetupContextMembersCollector>()
62-
.Transient<LocalCache<TT1, TT2>>(LocalCache)
6362

6463
// Walkers
6564
.SpecialType<CSharpSyntaxRewriter>()

0 commit comments

Comments
 (0)