Skip to content

Commit

Permalink
Improved performance
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Apr 27, 2024
1 parent 68c2c07 commit ef67690
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/VariablesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Block Build(
cancellationToken.ThrowIfCancellationRequested();
if (counter++ > Const.MaxIterationsCount)
{
throw new CompileErrorException("Cyclic dependency has been found.", rootNode.Binding.Source.GetLocation(), LogId.ErrorCyclicDependency);
throw new CompileErrorException($"The composition is too large. Stopped on the #{counter} instance.", rootNode.Binding.Source.GetLocation(), LogId.ErrorInvalidMetadata);
}

switch (currentStatement)
Expand Down
10 changes: 3 additions & 7 deletions src/Pure.DI.Core/Core/CodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,10 @@ public Unit Build(MdSetup setup)
composition = classBuilder.Build(composition);

cancellationToken.ThrowIfCancellationRequested();
var code = new StringBuilder(composition.Code.Sum(i => i.Length + 2));
foreach (var line in composition.Code)
{
code.AppendLine(line);
}

var size = composition.Code.SaveToArray(Encoding.UTF8, out var buffer);

cancellationToken.ThrowIfCancellationRequested();
sources.AddSource($"{setup.Name.FullName}.g.cs", SourceText.From(code.ToString(), Encoding.UTF8));
sources.AddSource($"{setup.Name.FullName}.g.cs", SourceText.From(buffer, size, Encoding.UTF8, SourceHashAlgorithm.Sha1, false, true));
return Unit.Shared;
}
}
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Const.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public static class Const
{
public const int MaxIterationsCount = 0xffff;
public const int MaxIterationsCount = 0x2FFFF;
}
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/DependencyGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public IEnumerable<DependencyNode> TryBuild(
cancellationToken.ThrowIfCancellationRequested();
if (counter++ > Const.MaxIterationsCount)
{
throw new CompileErrorException("Cyclic dependency has been found.", setup.Source.GetLocation(), LogId.ErrorCyclicDependency);
throw new CompileErrorException($"The composition is too large. Stopped on the #{counter} dependency.", setup.Source.GetLocation(), LogId.ErrorInvalidMetadata);
}

var targetNode = node.Node;
Expand Down
27 changes: 27 additions & 0 deletions src/Pure.DI.Core/Core/LinesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// ReSharper disable ReturnTypeCanBeEnumerable.Global
namespace Pure.DI.Core;

using System.Buffers;

internal sealed class LinesBuilder: IEnumerable<string>
{
private static readonly string[] Indents = new string[64];
Expand Down Expand Up @@ -87,6 +89,31 @@ public IEnumerator<string> GetEnumerator()
return _lines.Select(i => $"{GetIndent(i.Indent)}{i.Text}").GetEnumerator();
}

public int SaveToArray(Encoding encoding, out byte[] buffer)
{
var charCount = 0;
var newLine = Environment.NewLine;
foreach (var line in _lines)
{
charCount += GetIndent(line.Indent).Length;
charCount += line.Text.Length;
charCount += newLine.Length;
}

var size = encoding.GetMaxByteCount(charCount);
buffer = ArrayPool<byte>.Shared.Rent(size);
var position = 0;
foreach (var line in _lines)
{
var indent = GetIndent(line.Indent);
position += encoding.GetBytes(indent, 0, indent.Length, buffer, position);
position += encoding.GetBytes(line.Text, 0, line.Text.Length, buffer, position);
position += encoding.GetBytes(newLine, 0, newLine.Length, buffer, position);
}

return position;
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

private void FlushLines()
Expand Down

0 comments on commit ef67690

Please sign in to comment.