Skip to content

Commit

Permalink
Reducing memory consumption during code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed May 15, 2024
1 parent a427806 commit 1103885
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
20 changes: 7 additions & 13 deletions src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@ public void Build(BuildContext ctx, in Block block)
}

var info = block.Current.Info;
if (info.HasCode)
{
ctx.Code.AppendLines(info.Code.Lines);
return;
}

var code = new LinesBuilder();
try
{
var code = info.Code;
var level = ctx.Level;
var isThreadSafe = ctx.DependencyGraph.Source.Hints.IsThreadSafeEnabled;
var lockIsRequired = ctx.LockIsRequired ?? isThreadSafe;
Expand Down Expand Up @@ -118,9 +112,9 @@ variable.Node.Lifetime is Lifetime.Singleton or Lifetime.Scoped
}
finally
{
info.HasCode = true;
if (block.Parent is not null
&& info is { PerBlockRefCount: > 1, Code.Lines.Count: > 11 })
&& info is { PerBlockRefCount: > 1 }
&& code.Count > 11)
{
var localFunctionsCode = ctx.LocalFunctionsCode;
var localMethodName = $"{Names.EnsureExistsMethodNamePrefix}_{variable.VariableDeclarationName}".Replace("__", "_");
Expand All @@ -133,15 +127,15 @@ variable.Node.Lifetime is Lifetime.Singleton or Lifetime.Scoped
localFunctionsCode.AppendLine("{");
using (localFunctionsCode.Indent())
{
localFunctionsCode.AppendLines(info.Code.Lines);
localFunctionsCode.AppendLines(code.Lines);
}

localFunctionsCode.AppendLine("}");
info.Code = new LinesBuilder();
info.Code.AppendLine($"{localMethodName}();");
code = new LinesBuilder();
code.AppendLine($"{localMethodName}();");
}

ctx.Code.AppendLines(info.Code.Lines);
ctx.Code.AppendLines(code.Lines);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/BuildTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public string OnInjected(BuildContext ctx, Variable variable)
var variableCode = variable.VariableCode;
if (variableCode == variable.VariableName)
{
var skipNotNullCheck = (variable.HasCycle || variable.Info.HasCode)
var skipNotNullCheck = (variable.HasCycle || variable.IsDeclared)
&& variable.InstanceType.IsReferenceType
&& ctx.DependencyGraph.Source.SemanticModel.Compilation.Options.NullableContextOptions != NullableContextOptions.Disable;

Expand Down
4 changes: 0 additions & 4 deletions src/Pure.DI.Core/Core/Code/VariableInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ internal class VariableInfo
private readonly HashSet<int> _perBlockRefCounts = [];
public readonly HashSet<Block> Owners = [];
public bool IsCreated;
public bool HasCode;
public LinesBuilder Code = new();

public int RefCount { get; private set; } = 1;

Expand All @@ -24,7 +22,5 @@ public void Reset()
Owners.Clear();
RefCount = 1;
IsCreated = false;
HasCode = false;
Code = new LinesBuilder();
}
}

0 comments on commit 1103885

Please sign in to comment.