Skip to content

Commit 755031d

Browse files
committed
Apply NoOptimization instead of AggressiveOptimization to loop methods.
Count down loops instead of count up. Added IntroSmokeStringBuilder. Added more return type test cases.
1 parent 7730169 commit 755031d

21 files changed

+235
-620
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using BenchmarkDotNet.Attributes;
2+
using System.Text;
3+
4+
namespace BenchmarkDotNet.Samples
5+
{
6+
[MemoryDiagnoser(false)]
7+
public class IntroSmokeStringBuilder
8+
{
9+
[Benchmark]
10+
[Arguments(1)]
11+
[Arguments(1_000)]
12+
public StringBuilder Append_Strings(int repeat)
13+
{
14+
StringBuilder builder = new StringBuilder();
15+
16+
// strings are not sorted by length to mimic real input
17+
for (int i = 0; i < repeat; i++)
18+
{
19+
builder.Append("12345");
20+
builder.Append("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN");
21+
builder.Append("1234567890abcdefghijklmnopqrstuvwxy");
22+
builder.Append("1234567890");
23+
builder.Append("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHI");
24+
builder.Append("1234567890abcde");
25+
builder.Append("1234567890abcdefghijklmnopqrstuvwxyzABCD");
26+
builder.Append("1234567890abcdefghijklmnopqrst");
27+
builder.Append("1234567890abcdefghij");
28+
builder.Append("1234567890abcdefghijklmno");
29+
}
30+
31+
return builder;
32+
}
33+
}
34+
}

src/BenchmarkDotNet/Code/CodeGenerator.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,36 +146,21 @@ private static DeclarationsProvider GetDeclarationsProvider(Descriptor descripto
146146

147147
if (method.ReturnType == typeof(Task) || method.ReturnType == typeof(ValueTask))
148148
{
149-
return new TaskDeclarationsProvider(descriptor);
149+
return new AsyncDeclarationsProvider(descriptor);
150150
}
151151
if (method.ReturnType.GetTypeInfo().IsGenericType
152152
&& (method.ReturnType.GetTypeInfo().GetGenericTypeDefinition() == typeof(Task<>)
153153
|| method.ReturnType.GetTypeInfo().GetGenericTypeDefinition() == typeof(ValueTask<>)))
154154
{
155-
return new GenericTaskDeclarationsProvider(descriptor);
155+
return new AsyncDeclarationsProvider(descriptor);
156156
}
157157

158-
if (method.ReturnType == typeof(void))
158+
if (method.ReturnType == typeof(void) && method.HasAttribute<AsyncStateMachineAttribute>())
159159
{
160-
bool isUsingAsyncKeyword = method.HasAttribute<AsyncStateMachineAttribute>();
161-
if (isUsingAsyncKeyword)
162-
{
163-
throw new NotSupportedException("async void is not supported by design");
164-
}
165-
166-
return new VoidDeclarationsProvider(descriptor);
167-
}
168-
169-
if (method.ReturnType.IsByRef)
170-
{
171-
// System.Runtime.CompilerServices.IsReadOnlyAttribute is part of .NET Standard 2.1, we can't use it here..
172-
if (method.ReturnParameter.GetCustomAttributes().Any(attribute => attribute.GetType().Name == "IsReadOnlyAttribute"))
173-
return new ByReadOnlyRefDeclarationsProvider(descriptor);
174-
else
175-
return new ByRefDeclarationsProvider(descriptor);
160+
throw new NotSupportedException("async void is not supported by design");
176161
}
177162

178-
return new NonVoidDeclarationsProvider(descriptor);
163+
return new SyncDeclarationsProvider(descriptor);
179164
}
180165

181166
// internal for tests

src/BenchmarkDotNet/Code/DeclarationsProvider.cs

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
using System;
2-
using System.Linq;
3-
using System.Reflection;
1+
using System.Reflection;
42
using System.Threading.Tasks;
5-
using BenchmarkDotNet.Engines;
63
using BenchmarkDotNet.Extensions;
7-
using BenchmarkDotNet.Helpers;
84
using BenchmarkDotNet.Running;
95

106
namespace BenchmarkDotNet.Code
@@ -30,14 +26,6 @@ internal abstract class DeclarationsProvider
3026

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

33-
public abstract string ReturnsDefinition { get; }
34-
35-
protected virtual Type WorkloadMethodReturnType => Descriptor.WorkloadMethod.ReturnType;
36-
37-
public virtual string WorkloadMethodReturnTypeName => WorkloadMethodReturnType.GetCorrectCSharpTypeName();
38-
39-
public virtual string WorkloadMethodReturnTypeModifiers => null;
40-
4129
public virtual string GetWorkloadMethodCall(string passArguments) => $"{Descriptor.WorkloadMethod.Name}({passArguments})";
4230

4331
private string GetMethodName(MethodInfo method)
@@ -60,55 +48,14 @@ private string GetMethodName(MethodInfo method)
6048
}
6149
}
6250

63-
internal class VoidDeclarationsProvider : DeclarationsProvider
64-
{
65-
public VoidDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }
66-
67-
public override string ReturnsDefinition => "RETURNS_VOID";
68-
}
69-
70-
internal class NonVoidDeclarationsProvider : DeclarationsProvider
71-
{
72-
public NonVoidDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }
73-
74-
public override string ReturnsDefinition => "RETURNS_NON_VOID";
75-
}
76-
77-
internal class ByRefDeclarationsProvider : NonVoidDeclarationsProvider
78-
{
79-
public ByRefDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }
80-
81-
public override string WorkloadMethodReturnTypeName => base.WorkloadMethodReturnTypeName.Replace("&", string.Empty);
82-
83-
public override string ReturnsDefinition => "RETURNS_BYREF";
84-
85-
public override string WorkloadMethodReturnTypeModifiers => "ref";
86-
}
87-
88-
internal class ByReadOnlyRefDeclarationsProvider : ByRefDeclarationsProvider
51+
internal class SyncDeclarationsProvider : DeclarationsProvider
8952
{
90-
public ByReadOnlyRefDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }
91-
92-
public override string WorkloadMethodReturnTypeModifiers => "ref readonly";
53+
public SyncDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }
9354
}
9455

95-
internal class TaskDeclarationsProvider : VoidDeclarationsProvider
56+
internal class AsyncDeclarationsProvider : DeclarationsProvider
9657
{
97-
public TaskDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }
98-
99-
public override string GetWorkloadMethodCall(string passArguments) => $"BenchmarkDotNet.Helpers.AwaitHelper.GetResult({Descriptor.WorkloadMethod.Name}({passArguments}))";
100-
101-
protected override Type WorkloadMethodReturnType => typeof(void);
102-
}
103-
104-
/// <summary>
105-
/// declarations provider for <see cref="Task{TResult}" /> and <see cref="ValueTask{TResult}" />
106-
/// </summary>
107-
internal class GenericTaskDeclarationsProvider : NonVoidDeclarationsProvider
108-
{
109-
public GenericTaskDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }
110-
111-
protected override Type WorkloadMethodReturnType => Descriptor.WorkloadMethod.ReturnType.GetTypeInfo().GetGenericArguments().Single();
58+
public AsyncDeclarationsProvider(Descriptor descriptor) : base(descriptor) { }
11259

11360
public override string GetWorkloadMethodCall(string passArguments) => $"BenchmarkDotNet.Helpers.AwaitHelper.GetResult({Descriptor.WorkloadMethod.Name}({passArguments}))";
11461
}

src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorCallExtensions.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,6 @@ namespace BenchmarkDotNet.Helpers.Reflection.Emit
77
{
88
internal static class IlGeneratorCallExtensions
99
{
10-
public static LocalBuilder DeclareOptionalLocalForInstanceCall(
11-
this ILGenerator ilBuilder,
12-
Type localType,
13-
MethodInfo methodToCall)
14-
{
15-
if (methodToCall.DeclaringType == null)
16-
throw new ArgumentException($"The {nameof(methodToCall)} should have non-null {nameof(methodToCall.DeclaringType)}.");
17-
18-
if (methodToCall.IsStatic)
19-
return null;
20-
21-
if (!methodToCall.DeclaringType.IsAssignableFrom(localType))
22-
throw new ArgumentException($"{methodToCall.DeclaringType} is not assignable from {localType}.");
23-
24-
return localType.IsValueType && localType != typeof(void)
25-
? ilBuilder.DeclareLocal(localType)
26-
: null;
27-
}
28-
2910
public static void EmitStaticCall(
3011
this ILGenerator ilBuilder,
3112
MethodInfo methodToCall,

src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorDefaultValueExtensions.cs

Lines changed: 0 additions & 169 deletions
This file was deleted.

0 commit comments

Comments
 (0)