Skip to content

Commit

Permalink
Reduce generated code size (#1984)
Browse files Browse the repository at this point in the history
* don't append a new line for each @DummyUnroll@ (3 * 64 lines of code for ever benchmark)

* remove redundant #if defines
  • Loading branch information
adamsitnik committed Apr 6, 2022
1 parent ca43aaf commit bbe6abc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
35 changes: 30 additions & 5 deletions src/BenchmarkDotNet/Code/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal static string Generate(BuildPartition buildPartition)

string passArguments = GetPassArguments(benchmark);

extraDefines.Add($"{provider.ExtraDefines}_{buildInfo.Id}");
string compilationId = $"{provider.ReturnsDefinition}_{buildInfo.Id}";

AddNonEmptyUnique(additionalLogic, benchmark.Descriptor.AdditionalLogic);

Expand All @@ -64,7 +64,8 @@ internal static string Generate(BuildPartition buildPartition)
.Replace("$EngineFactoryType$", GetEngineFactoryTypeName(benchmark))
.Replace("$MeasureExtraStats$", buildInfo.Config.HasExtraStatsDiagnoser() ? "true" : "false")
.Replace("$DisassemblerEntryMethodName$", DisassemblerConstants.DisassemblerEntryMethodName)
.Replace("$WorkloadMethodCall$", provider.GetWorkloadMethodCall(passArguments)).ToString();
.Replace("$WorkloadMethodCall$", provider.GetWorkloadMethodCall(passArguments))
.RemoveRedundantIfDefines(compilationId);

benchmarkTypeCode = Unroll(benchmarkTypeCode, benchmark.Job.ResolveValue(RunMode.UnrollFactorCharacteristic, EnvironmentResolver.Instance));

Expand Down Expand Up @@ -122,6 +123,7 @@ private static string Unroll(string text, int factor)
const string unrollDirective = "@Unroll@";
const string dummyUnrollDirective = "@DummyUnroll@";
const int dummyUnrollFactor = 1 << 6;
string dummyUnrolled = string.Join("", Enumerable.Repeat("dummyVar++;", dummyUnrollFactor));
var oldLines = text.Split('\n');
var newLines = new List<string>();
foreach (string line in oldLines)
Expand All @@ -134,9 +136,7 @@ private static string Unroll(string text, int factor)
}
else if (line.Contains(dummyUnrollDirective))
{
string newLine = line.Replace(dummyUnrollDirective, "");
for (int i = 0; i < dummyUnrollFactor; i++)
newLines.Add(newLine);
newLines.Add(line.Replace(dummyUnrollDirective, dummyUnrolled));
}
else
newLines.Add(line);
Expand Down Expand Up @@ -307,6 +307,31 @@ public SmartStringBuilder Replace(string oldValue, string newValue)
return this;
}

public string RemoveRedundantIfDefines(string id)
{
var oldLines = builder.ToString().Split('\n');
var newLines = new List<string>();
bool keepAdding = true;

foreach (string line in oldLines)
{
if (line.StartsWith("#if RETURNS") || line.StartsWith("#elif RETURNS"))
{
keepAdding = line.Contains(id);
}
else if (line.StartsWith("#endif // RETURNS"))
{
keepAdding = true;
}
else if (keepAdding)
{
newLines.Add(line);
}
}

return string.Join("\n", newLines);
}

public override string ToString() => builder.ToString();
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/BenchmarkDotNet/Code/DeclarationsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal abstract class DeclarationsProvider

public string IterationCleanupMethodName => Descriptor.IterationCleanupMethod?.Name ?? EmptyAction;

public abstract string ExtraDefines { get; }
public abstract string ReturnsDefinition { get; }

protected virtual Type WorkloadMethodReturnType => Descriptor.WorkloadMethod.ReturnType;

Expand Down Expand Up @@ -74,7 +74,7 @@ internal class VoidDeclarationsProvider : DeclarationsProvider
{
public VoidDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }

public override string ExtraDefines => "#define RETURNS_VOID";
public override string ReturnsDefinition => "RETURNS_VOID";

protected override Type OverheadMethodReturnType => typeof(void);

Expand Down Expand Up @@ -113,10 +113,10 @@ public override string OverheadImplementation
}
}

public override string ExtraDefines
public override string ReturnsDefinition
=> Consumer.IsConsumable(WorkloadMethodReturnType) || Consumer.HasConsumableField(WorkloadMethodReturnType, out _)
? "#define RETURNS_CONSUMABLE"
: "#define RETURNS_NON_CONSUMABLE_STRUCT";
? "RETURNS_CONSUMABLE"
: "RETURNS_NON_CONSUMABLE_STRUCT";
}

internal class ByRefDeclarationsProvider : NonVoidDeclarationsProvider
Expand All @@ -131,7 +131,7 @@ public ByRefDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }

public override string OverheadImplementation => $"return default(System.{nameof(IntPtr)});";

public override string ExtraDefines => "#define RETURNS_BYREF";
public override string ReturnsDefinition => "RETURNS_BYREF";

public override string WorkloadMethodReturnTypeModifiers => "ref";
}
Expand All @@ -140,7 +140,7 @@ internal class ByReadOnlyRefDeclarationsProvider : ByRefDeclarationsProvider
{
public ByReadOnlyRefDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }

public override string ExtraDefines => "#define RETURNS_BYREF_READONLY";
public override string ReturnsDefinition => "RETURNS_BYREF_READONLY";

public override string WorkloadMethodReturnTypeModifiers => "ref readonly";
}
Expand Down
8 changes: 4 additions & 4 deletions src/BenchmarkDotNet/Templates/BenchmarkType.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private void Dummy1()
{
dummyVar++;@DummyUnroll@
@DummyUnroll@
}

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private void Dummy2()
{
dummyVar++;@DummyUnroll@
@DummyUnroll@
}

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private void Dummy3()
{
dummyVar++;@DummyUnroll@
@DummyUnroll@
}

private $OverheadMethodReturnTypeName$ __Overhead($ArgumentsDefinition$) // __ is to avoid possible name conflict
Expand Down Expand Up @@ -449,5 +449,5 @@
$WorkloadMethodCall$;
}
}
#endif
#endif // RETURNS
}

0 comments on commit bbe6abc

Please sign in to comment.