Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization Pass #25

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
</ItemGroup>

</Project>
9 changes: 6 additions & 3 deletions src/CodegenTests/CodegenTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.9.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NSubstitute" Version="5.1.0"/>
<PackageReference Include="Shouldly" Version="4.2.1"/>
</ItemGroup>
Expand Down
7 changes: 4 additions & 3 deletions src/CodegenTests/Compilation/end_to_end_compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void generate_dynamic_types_with_no_fields()
var multiplier = assembly.AddType("Multiplier", typeof(INumberGenerator));
multiplier.MethodFor(nameof(INumberGenerator.Generate))
.Frames.Append<MultiplyFrame>();

assembly.CompileAll();

Activator.CreateInstance(adder.CompiledType)
Expand All @@ -35,8 +35,9 @@ public void generate_dynamic_types_with_no_fields()
.As<INumberGenerator>()
.Generate(3, 4).ShouldBe(12);

adder.SourceCode.ShouldContain("public class Adder");
multiplier.SourceCode.ShouldContain("public class Multiplier");
adder.SourceCode.ShouldContain("System.CodeDom.Compiler.GeneratedCode");
adder.SourceCode.ShouldContain("public sealed class Adder");
multiplier.SourceCode.ShouldContain("public sealed class Multiplier");
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/CommandLineTests/CommandLineTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.9.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NSubstitute" Version="5.1.0"/>
<PackageReference Include="Shouldly" Version="4.2.1"/>
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/CoreTests/Core/StringExtensionsTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ public void ends_with_ignore_case(string one, string suffix, bool expected)
[InlineData("foo=bar;", 1)]
[InlineData("foo=bar;zap=42;", 2)]
[InlineData("foo=bar;zap=42", 2)]
[InlineData("foo=bar ;zap=42 ", 2)]
public void removes_empty_entries_when_splitting_strings_into_array(string one, int expectedCount)
{
var array = one.ToDelimitedArray(';');
Expand Down
1 change: 1 addition & 0 deletions src/CoreTests/Core/TimeSpanExtensionsTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void to_time_from_string()
"0700".ToTime().ShouldBe(new TimeSpan(7, 0, 0));
"1700".ToTime().ShouldBe(new TimeSpan(17, 0, 0));
"1850".ToTime().ShouldBe(new TimeSpan(18, 50, 0));
"19:05".ToTime().ShouldBe(new TimeSpan(19, 05, 00));
}

[Fact]
Expand Down
11 changes: 7 additions & 4 deletions src/CoreTests/CoreTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NSubstitute" Version="5.1.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="Shouldly" Version="4.2.1"/>
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.9.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0"/>
</ItemGroup>

Expand Down
9 changes: 6 additions & 3 deletions src/EventStoreTests/EventStoreTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.9.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NSubstitute" Version="5.1.0"/>
<PackageReference Include="Shouldly" Version="4.2.1"/>
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/JasperFx.RuntimeCompiler/JasperFx.RuntimeCompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<PackageId>JasperFx.RuntimeCompiler</PackageId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.11.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.Scripting" Version="4.11.0"/>
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.12.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Scripting" Version="4.12.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="[7.0.0, 9.0.0)"/>
</ItemGroup>

Expand Down
9 changes: 9 additions & 0 deletions src/JasperFx/CodeGeneration/CodeGenerationObjectPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text;
using Microsoft.Extensions.ObjectPool;

namespace JasperFx.CodeGeneration;

internal static class CodeGenerationObjectPool
{
public static readonly ObjectPool<StringBuilder> StringBuilderPool = new DefaultObjectPoolProvider().CreateStringBuilderPool();
}
6 changes: 3 additions & 3 deletions src/JasperFx/CodeGeneration/GeneratedType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,15 @@ private void writeFieldDeclarations(ISourceWriter writer, IList<InjectedField> a
private void writeDeclaration(ISourceWriter writer)
{
var implemented = implements().ToArray();

writer.WriteLine("[global::System.CodeDom.Compiler.GeneratedCode(\"JasperFx\", \"1.0.0\")]");
if (implemented.Any())
{
writer.Write(
$"BLOCK:public class {TypeName} : {implemented.Select(x => x.FullNameInCode()).Join(", ")}");
$"BLOCK:public sealed class {TypeName} : {implemented.Select(x => x.FullNameInCode()).Join(", ")}");
}
else
{
writer.Write($"BLOCK:public class {TypeName}");
writer.Write($"BLOCK:public sealed class {TypeName}");
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/JasperFx/CodeGeneration/ISourceWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface ISourceWriter
/// and decrements the current block level
/// </summary>
/// <param name="extra"></param>
void FinishBlock(string? extra = null);
void FinishBlock(ReadOnlySpan<char> extra = default);


/// <summary>
Expand All @@ -35,4 +35,18 @@ public interface ISourceWriter
/// </summary>
/// <param name="text"></param>
void WriteLine(string text);

/// <summary>
/// Writes a single line with this content to the code
/// at the current block level
/// </summary>
/// <param name="text"></param>
void WriteLine(ReadOnlySpan<char> value);

/// <summary>
/// Writes a single line with this content to the code
/// at the current block level
/// </summary>
/// <param name="text"></param>
void WriteLine(char value);
}
64 changes: 41 additions & 23 deletions src/JasperFx/CodeGeneration/SourceWriter.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
using System.Text;
using JasperFx.Core;

namespace JasperFx.CodeGeneration;

public class SourceWriter : ISourceWriter, IDisposable
{
private readonly StringWriter _writer = new();
private string _leadingSpaces = "";
private readonly StringBuilder _builder;

private int _level;

public void Dispose()
public SourceWriter()
{
_writer?.Dispose();
_builder = CodeGenerationObjectPool.StringBuilderPool.Get();
}

public int IndentionLevel
private const int IndentSize = 4;

public void Dispose()
{
get => _level;
set
{
_level = value;
_leadingSpaces = "".PadRight(_level * 4);
}
CodeGenerationObjectPool.StringBuilderPool.Return(_builder);
}

public int IndentionLevel { get; set; }

public void BlankLine()
{
_writer.WriteLine();
_builder.AppendLine();
}

public void Write(string? text = null)
Expand All @@ -47,12 +44,12 @@ public void Write(string? text = null)
}
else if (line.StartsWith("BLOCK:"))
{
WriteLine(line.Substring(6));
WriteLine(line.AsSpan(6));
StartBlock();
}
else if (line.StartsWith("END"))
{
FinishBlock(line.Substring(3));
FinishBlock(line.AsSpan(3));
}
else
{
Expand All @@ -63,10 +60,31 @@ public void Write(string? text = null)

public void WriteLine(string text)
{
_writer.WriteLine(_leadingSpaces + text);
Indent();
_builder.AppendLine(text);
}

public void WriteLine(ReadOnlySpan<char> value)
{
Indent();
_builder.Append(value);
_builder.AppendLine();
}

public void WriteLine(char value)
{
Indent();
_builder.Append(value);
_builder.AppendLine();
}


private void Indent()
{
_builder.Append(' ', IndentionLevel * IndentSize);
}

public void FinishBlock(string? extra = null)
public void FinishBlock(ReadOnlySpan<char> extra = default)
{
if (IndentionLevel == 0)
{
Expand All @@ -75,13 +93,13 @@ public void FinishBlock(string? extra = null)

IndentionLevel--;

if (extra.IsEmpty())
if (extra.IsEmpty)
{
WriteLine("}");
WriteLine('}');
}
else
{
WriteLine("}" + extra);
WriteLine($"}}{extra}");
}


Expand All @@ -90,13 +108,13 @@ public void FinishBlock(string? extra = null)

private void StartBlock()
{
WriteLine("{");
WriteLine('{');
IndentionLevel++;
}

public string Code()
{
return _writer.ToString();
return _builder.ToString();
}

internal class BlockMarker : IDisposable
Expand Down
6 changes: 3 additions & 3 deletions src/JasperFx/Core/FileSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private IEnumerable<string> getAllDistinctFiles(string path, string? pattern)
{
if (pattern.IsEmpty())
{
return new string[0];
return Array.Empty<string>();
}

return pattern.Split(';').SelectMany(x =>
Expand All @@ -116,11 +116,11 @@ private IEnumerable<string> getAllDistinctFiles(string path, string? pattern)
{
return directory.Exists
? Directory.GetFiles(fullPath, filePattern, searchOption)
: new string[0];
: [];
}
catch (DirectoryNotFoundException)
{
return new string[0];
return Array.Empty<string>();
}
}).Distinct();
}
Expand Down
12 changes: 9 additions & 3 deletions src/JasperFx/Core/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static string ReadAllText(this Stream stream)
/// <returns></returns>
public static byte[] ReadAllBytes(this Stream stream)
{
if (stream is MemoryStream ms)
return ms.ToArray();

using var content = new MemoryStream();
stream.CopyTo(content);
return content.ToArray();
Expand All @@ -33,10 +36,10 @@ public static byte[] ReadAllBytes(this Stream stream)
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static Task<string> ReadAllTextAsync(this Stream stream)
public static async Task<string> ReadAllTextAsync(this Stream stream)
{
using var sr = new StreamReader(stream, leaveOpen: true);
return sr.ReadToEndAsync();
return await sr.ReadToEndAsync();
}

/// <summary>
Expand All @@ -47,6 +50,9 @@ public static Task<string> ReadAllTextAsync(this Stream stream)
/// <returns></returns>
public static async Task<byte[]> ReadAllBytesAsync(this Stream stream)
{
if (stream is MemoryStream ms)
return ms.ToArray();

using var content = new MemoryStream();
await stream.CopyToAsync(content).ConfigureAwait(false);
return content.ToArray();
Expand All @@ -65,7 +71,7 @@ public static async Task<byte[]> ReadBytesAsync(this Stream stream, long length)
int current;
do
{
current = await stream.ReadAsync(buffer, totalRead, buffer.Length - totalRead).ConfigureAwait(false);
current = await stream.ReadAsync(buffer.AsMemory(totalRead, buffer.Length - totalRead)).ConfigureAwait(false);
totalRead += current;
} while (totalRead < length && current > 0);

Expand Down
Loading
Loading