Skip to content

Commit 6c883d0

Browse files
committed
Use global:: prefix for setup, cleanup, and async benchmarks.
Fix missing prefixes. Remove ValidateNamingConflicts and test more name conflicts. Remove unnecessary underscores.
1 parent c2b6dd7 commit 6c883d0

File tree

4 files changed

+36
-40
lines changed

4 files changed

+36
-40
lines changed

src/BenchmarkDotNet/Code/DeclarationsProvider.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ internal abstract class DeclarationsProvider
2828

2929
public abstract string GetWorkloadMethodCall(string passArguments);
3030

31-
protected string WorkloadMethodPrefix => Descriptor.WorkloadMethod.IsStatic ? Descriptor.WorkloadMethod.DeclaringType.GetCorrectCSharpTypeName() : "base";
31+
protected static string GetMethodPrefix(MethodInfo method)
32+
=> method.IsStatic ? method.DeclaringType.GetCorrectCSharpTypeName() : "base";
3233

3334
private string GetMethodName(MethodInfo method)
3435
{
@@ -43,22 +44,22 @@ private string GetMethodName(MethodInfo method)
4344
(method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>) ||
4445
method.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>))))
4546
{
46-
return $"() => BenchmarkDotNet.Helpers.AwaitHelper.GetResult({method.Name}())";
47+
return $"() => global::BenchmarkDotNet.Helpers.AwaitHelper.GetResult({GetMethodPrefix(Descriptor.WorkloadMethod)}.{method.Name}())";
4748
}
4849

49-
return method.Name;
50+
return $"{GetMethodPrefix(Descriptor.WorkloadMethod)}.{method.Name}";
5051
}
5152
}
5253

5354
internal class SyncDeclarationsProvider(Descriptor descriptor) : DeclarationsProvider(descriptor)
5455
{
5556
public override string GetWorkloadMethodCall(string passArguments)
56-
=> $"{WorkloadMethodPrefix}.{Descriptor.WorkloadMethod.Name}({passArguments})";
57+
=> $"{GetMethodPrefix(Descriptor.WorkloadMethod)}.{Descriptor.WorkloadMethod.Name}({passArguments})";
5758
}
5859

5960
internal class AsyncDeclarationsProvider(Descriptor descriptor) : DeclarationsProvider(descriptor)
6061
{
6162
public override string GetWorkloadMethodCall(string passArguments)
62-
=> $"BenchmarkDotNet.Helpers.AwaitHelper.GetResult({WorkloadMethodPrefix}.{Descriptor.WorkloadMethod.Name}({passArguments}))";
63+
=> $"global::BenchmarkDotNet.Helpers.AwaitHelper.GetResult({GetMethodPrefix(Descriptor.WorkloadMethod)}.{Descriptor.WorkloadMethod.Name}({passArguments}))";
6364
}
6465
}

src/BenchmarkDotNet/Templates/BenchmarkType.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,31 @@
101101
}
102102

103103
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
104-
private void __Overhead($ArgumentsDefinition$) // __ is to avoid possible name conflict
104+
private void Overhead($ArgumentsDefinition$)
105105
{
106106
}
107107

108-
[global::System.Runtime.CompilerServices.MethodImpl(BenchmarkDotNet.Portability.CodeGenHelper.AggressiveOptimizationOption)]
108+
[global::System.Runtime.CompilerServices.MethodImpl(global::BenchmarkDotNet.Portability.CodeGenHelper.AggressiveOptimizationOption)]
109109
private void OverheadActionUnroll(global::System.Int64 invokeCount)
110110
{
111111
$LoadArguments$
112112
while (--invokeCount >= 0)
113113
{
114-
this.__Overhead($PassArguments$);@Unroll@
114+
this.Overhead($PassArguments$);@Unroll@
115115
}
116116
}
117117

118-
[global::System.Runtime.CompilerServices.MethodImpl(BenchmarkDotNet.Portability.CodeGenHelper.AggressiveOptimizationOption)]
118+
[global::System.Runtime.CompilerServices.MethodImpl(global::BenchmarkDotNet.Portability.CodeGenHelper.AggressiveOptimizationOption)]
119119
private void OverheadActionNoUnroll(global::System.Int64 invokeCount)
120120
{
121121
$LoadArguments$
122122
while (--invokeCount >= 0)
123123
{
124-
this.__Overhead($PassArguments$);
124+
this.Overhead($PassArguments$);
125125
}
126126
}
127127

128-
[global::System.Runtime.CompilerServices.MethodImpl(BenchmarkDotNet.Portability.CodeGenHelper.AggressiveOptimizationOption)]
128+
[global::System.Runtime.CompilerServices.MethodImpl(global::BenchmarkDotNet.Portability.CodeGenHelper.AggressiveOptimizationOption)]
129129
private void WorkloadActionUnroll(global::System.Int64 invokeCount)
130130
{
131131
$LoadArguments$
@@ -135,7 +135,7 @@
135135
}
136136
}
137137

138-
[global::System.Runtime.CompilerServices.MethodImpl(BenchmarkDotNet.Portability.CodeGenHelper.AggressiveOptimizationOption)]
138+
[global::System.Runtime.CompilerServices.MethodImpl(global::BenchmarkDotNet.Portability.CodeGenHelper.AggressiveOptimizationOption)]
139139
private void WorkloadActionNoUnroll(global::System.Int64 invokeCount)
140140
{
141141
$LoadArguments$

src/BenchmarkDotNet/Validators/CompilationValidator.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ private CompilationValidator() { }
2525

2626
public IEnumerable<ValidationError> Validate(ValidationParameters validationParameters)
2727
=> ValidateCSharpNaming(validationParameters.Benchmarks)
28-
.Union(ValidateNamingConflicts(validationParameters.Benchmarks))
2928
.Union(ValidateClassModifiers((validationParameters.Benchmarks))
3029
.Union(ValidateAccessModifiers(validationParameters.Benchmarks))
3130
.Union(ValidateBindingModifiers(validationParameters.Benchmarks))
@@ -71,16 +70,6 @@ private static IEnumerable<ValidationError> ValidateCSharpNaming(IEnumerable<Ben
7170
benchmark
7271
));
7372

74-
private static IEnumerable<ValidationError> ValidateNamingConflicts(IEnumerable<BenchmarkCase> benchmarks)
75-
=> benchmarks
76-
.Select(benchmark => benchmark.Descriptor.Type)
77-
.Distinct()
78-
.Where(type => type.GetAllMethods().Any(method => IsUsingNameUsedInternallyByOurTemplate(method.Name)))
79-
.Select(benchmark
80-
=> new ValidationError(
81-
true,
82-
"Using \"__Overhead\" for method name is prohibited. We are using it internally in our templates. Please rename your method"));
83-
8473
private static IEnumerable<ValidationError> ValidateAccessModifiers(IEnumerable<BenchmarkCase> benchmarks)
8574
=> benchmarks.Where(x => x.Descriptor.Type.IsGenericType
8675
&& HasPrivateGenericArguments(x.Descriptor.Type))
@@ -113,9 +102,6 @@ private static bool IsValidCSharpIdentifier(string identifier) // F# allows to u
113102
&& identifier.Skip(1).All(character => char.IsLetterOrDigit(character) || character == Underscore)
114103
&& !CsharpKeywords.Contains(identifier);
115104

116-
private static bool IsUsingNameUsedInternallyByOurTemplate(string identifier)
117-
=> identifier == "__Overhead";
118-
119105
private static bool HasPrivateGenericArguments(Type type) => type.GetGenericArguments().Any(a => !(a.IsPublic || a.IsNestedPublic));
120106

121107
// source: https://stackoverflow.com/a/19562316

tests/BenchmarkDotNet.IntegrationTests/ConflictingNamesTests.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,37 @@
22
using Xunit;
33
using Xunit.Abstractions;
44

5-
namespace BenchmarkDotNet.IntegrationTests
5+
namespace BenchmarkDotNet.IntegrationTests;
6+
7+
public class ConflictingNamesTests(ITestOutputHelper output) : BenchmarkTestExecutor(output)
68
{
7-
public class ConflictingNamesTests : BenchmarkTestExecutor
9+
[Fact]
10+
public void BenchmarkMethodsCanUseTemplateNames() => CanExecute<WithNamesUsedByTemplate>();
11+
12+
public class WithNamesUsedByTemplate
813
{
9-
public ConflictingNamesTests(ITestOutputHelper output) : base(output) { }
14+
[Benchmark]
15+
public void System()
16+
{
17+
18+
}
1019

11-
[Fact]
12-
public void BenchmarkMethodsCanUseTemplateNames() => CanExecute<WithNamesUsedByTemplate>();
20+
[Benchmark]
21+
public void BenchmarkDotNet()
22+
{
23+
24+
}
1325

14-
public class WithNamesUsedByTemplate
26+
[Benchmark]
27+
public void Overhead()
1528
{
16-
[Benchmark]
17-
public void System()
18-
{
1929

20-
}
30+
}
2131

22-
[Benchmark]
23-
public void BenchmarkDotNet()
24-
{
32+
[Benchmark]
33+
public void WorkloadActionUnroll()
34+
{
2535

26-
}
2736
}
2837
}
2938
}

0 commit comments

Comments
 (0)