Skip to content

Commit bbe6abc

Browse files
authored
Reduce generated code size (#1984)
* don't append a new line for each @DummyUnroll@ (3 * 64 lines of code for ever benchmark) * remove redundant #if defines
1 parent ca43aaf commit bbe6abc

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

src/BenchmarkDotNet/Code/CodeGenerator.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal static string Generate(BuildPartition buildPartition)
3737

3838
string passArguments = GetPassArguments(benchmark);
3939

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

4242
AddNonEmptyUnique(additionalLogic, benchmark.Descriptor.AdditionalLogic);
4343

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

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

@@ -122,6 +123,7 @@ private static string Unroll(string text, int factor)
122123
const string unrollDirective = "@Unroll@";
123124
const string dummyUnrollDirective = "@DummyUnroll@";
124125
const int dummyUnrollFactor = 1 << 6;
126+
string dummyUnrolled = string.Join("", Enumerable.Repeat("dummyVar++;", dummyUnrollFactor));
125127
var oldLines = text.Split('\n');
126128
var newLines = new List<string>();
127129
foreach (string line in oldLines)
@@ -134,9 +136,7 @@ private static string Unroll(string text, int factor)
134136
}
135137
else if (line.Contains(dummyUnrollDirective))
136138
{
137-
string newLine = line.Replace(dummyUnrollDirective, "");
138-
for (int i = 0; i < dummyUnrollFactor; i++)
139-
newLines.Add(newLine);
139+
newLines.Add(line.Replace(dummyUnrollDirective, dummyUnrolled));
140140
}
141141
else
142142
newLines.Add(line);
@@ -307,6 +307,31 @@ public SmartStringBuilder Replace(string oldValue, string newValue)
307307
return this;
308308
}
309309

310+
public string RemoveRedundantIfDefines(string id)
311+
{
312+
var oldLines = builder.ToString().Split('\n');
313+
var newLines = new List<string>();
314+
bool keepAdding = true;
315+
316+
foreach (string line in oldLines)
317+
{
318+
if (line.StartsWith("#if RETURNS") || line.StartsWith("#elif RETURNS"))
319+
{
320+
keepAdding = line.Contains(id);
321+
}
322+
else if (line.StartsWith("#endif // RETURNS"))
323+
{
324+
keepAdding = true;
325+
}
326+
else if (keepAdding)
327+
{
328+
newLines.Add(line);
329+
}
330+
}
331+
332+
return string.Join("\n", newLines);
333+
}
334+
310335
public override string ToString() => builder.ToString();
311336
}
312337
}

src/BenchmarkDotNet/Code/DeclarationsProvider.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal abstract class DeclarationsProvider
3030

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

33-
public abstract string ExtraDefines { get; }
33+
public abstract string ReturnsDefinition { get; }
3434

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

@@ -74,7 +74,7 @@ internal class VoidDeclarationsProvider : DeclarationsProvider
7474
{
7575
public VoidDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }
7676

77-
public override string ExtraDefines => "#define RETURNS_VOID";
77+
public override string ReturnsDefinition => "RETURNS_VOID";
7878

7979
protected override Type OverheadMethodReturnType => typeof(void);
8080

@@ -113,10 +113,10 @@ public override string OverheadImplementation
113113
}
114114
}
115115

116-
public override string ExtraDefines
116+
public override string ReturnsDefinition
117117
=> Consumer.IsConsumable(WorkloadMethodReturnType) || Consumer.HasConsumableField(WorkloadMethodReturnType, out _)
118-
? "#define RETURNS_CONSUMABLE"
119-
: "#define RETURNS_NON_CONSUMABLE_STRUCT";
118+
? "RETURNS_CONSUMABLE"
119+
: "RETURNS_NON_CONSUMABLE_STRUCT";
120120
}
121121

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

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

134-
public override string ExtraDefines => "#define RETURNS_BYREF";
134+
public override string ReturnsDefinition => "RETURNS_BYREF";
135135

136136
public override string WorkloadMethodReturnTypeModifiers => "ref";
137137
}
@@ -140,7 +140,7 @@ internal class ByReadOnlyRefDeclarationsProvider : ByRefDeclarationsProvider
140140
{
141141
public ByReadOnlyRefDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }
142142

143-
public override string ExtraDefines => "#define RETURNS_BYREF_READONLY";
143+
public override string ReturnsDefinition => "RETURNS_BYREF_READONLY";
144144

145145
public override string WorkloadMethodReturnTypeModifiers => "ref readonly";
146146
}

src/BenchmarkDotNet/Templates/BenchmarkType.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,19 @@
8989
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
9090
private void Dummy1()
9191
{
92-
dummyVar++;@DummyUnroll@
92+
@DummyUnroll@
9393
}
9494

9595
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
9696
private void Dummy2()
9797
{
98-
dummyVar++;@DummyUnroll@
98+
@DummyUnroll@
9999
}
100100

101101
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
102102
private void Dummy3()
103103
{
104-
dummyVar++;@DummyUnroll@
104+
@DummyUnroll@
105105
}
106106

107107
private $OverheadMethodReturnTypeName$ __Overhead($ArgumentsDefinition$) // __ is to avoid possible name conflict
@@ -449,5 +449,5 @@
449449
$WorkloadMethodCall$;
450450
}
451451
}
452-
#endif
452+
#endif // RETURNS
453453
}

0 commit comments

Comments
 (0)