-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathMethodsSignature.cs
More file actions
273 lines (241 loc) · 9.81 KB
/
Copy pathMethodsSignature.cs
File metadata and controls
273 lines (241 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// <copyright file="MethodsSignature.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc.
// </copyright>
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
#pragma warning disable SA1402 // File may only contain a single type
#pragma warning disable SA1401 // Fields should be private
#pragma warning disable SA1201 // Elements should appear in the correct order
namespace Samples.Computer01
{
public class MethodsSignature : ScenarioBase
{
public override void OnProcess()
{
// The easiest way to test frames with sampling is to throw different exceptions
// because at least one of each type will be sampled.
// Allocations beyond 100 KB would be fine except that we could not test
// the .NET Framework runtime
WithResult().GetAwaiter().GetResult();
}
// Same shape as BuggyBits ProductsController.WithResult() / GetSyncOverAsync(): the exception
// is thrown from the synchronous segment of an async method, so the compiler-generated state
// machines and AsyncMethodBuilderCore.Start<TStateMachine> are on the stack when it is
// captured. Each state machine is a type nested in MethodsSignature: its name must keep both
// the namespace and the enclosing type.
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private async Task<int> WithResult()
{
return await GetSyncOverAsync();
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private async Task<int> GetSyncOverAsync()
{
// nothing is awaited before this call: the whole chain below runs in the synchronous
// segment of the state machine, i.e. under AsyncMethodBuilderCore.Start<...>
RunStateMachine();
await Task.Yield();
return 42;
}
// The state machines generated by the compiler for the two methods above are structs in release
// builds but classes in debug builds. In the latter case, the shared canonical instantiation of
// Start<TStateMachine> is used, so the exact state machine type is not available to the profiler.
// Driving our own state machine, always a struct, gives the same frames in both configurations.
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void RunStateMachine()
{
var builder = AsyncTaskMethodBuilder.Create();
var stateMachine = new SyncOverAsyncStateMachine { Owner = this };
// no need to complete the builder: nothing awaits its task and the exceptions thrown by
// the chain below are caught before they could escape MoveNext()
builder.Start(ref stateMachine);
}
private struct SyncOverAsyncStateMachine : IAsyncStateMachine
{
public MethodsSignature Owner;
public void MoveNext()
{
Owner.TriggerExceptions();
}
public void SetStateMachine(IAsyncStateMachine stateMachine)
{
}
}
// call methods with different signatures
// Note: return type is not meaningful
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void TriggerExceptions()
{
ThrowVoid();
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowVoid()
{
ThrowObject("this is the end");
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowObject(object val)
{
ThrowBool(true);
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowBool(bool bValue)
{
ThrowNumbers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowNumbers(byte b, sbyte sb, Int16 i16, UInt16 ui16, Int32 i32, UInt32 ui32, Int64 i64, UInt64 ui64, float s, double d)
{
ThrowStringAndChar("this is the end...", '.');
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowStringAndChar(string v, char c)
{
ThrowNative(IntPtr.Zero, UIntPtr.Zero);
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowNative(IntPtr ptr, UIntPtr uptr)
{
var matrix2 = new int[2, 2, 2];
matrix2[0, 0, 0] = 0;
matrix2[0, 0, 1] = 0;
matrix2[0, 1, 0] = 1;
matrix2[0, 1, 1] = 1;
matrix2[1, 0, 0] = 2;
matrix2[1, 0, 1] = 2;
matrix2[1, 1, 0] = 3;
matrix2[1, 1, 1] = 3;
byte[][] jagged = new byte[3][];
jagged[0] = new byte[] { 1, 3, 5, 7, 9 };
jagged[1] = new byte[] { 0, 2, 4, 6 };
jagged[2] = new byte[] { 11, 22 };
ThrowArrays(
new string[] { "this", "is", "the", "end" },
matrix2,
jagged);
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowArrays(string[] a1, int[,,] matrix2, byte[][] jaggedArray)
{
MyStruct ms;
ms.Member = 42;
ThrowStruct(ms);
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowStruct(MyStruct ms)
{
var mc = new MyClass();
mc.Member = "this is the end...";
ThrowClass(mc);
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowClass(MyClass mc)
{
MyStruct ms;
ms.Member = 42;
ThrowWithRefs(ref mc, ref ms);
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowWithRefs(ref MyClass mc, ref MyStruct ms)
{
ThrowGenericMethod1(true);
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowGenericMethod1<T>(T element)
{
if (element is bool)
{
ThrowGenericMethod1(new MyClass());
}
else
if (element is MyClass)
{
MyStruct ms;
ms.Member = 42;
ThrowGenericMethod1(ms);
}
else
if (element is MyStruct)
{
GlobalStruct gs;
gs.Member = 42;
ThrowGenericMethod1(gs);
}
else
if (element is GlobalStruct)
{
ThrowGenericMethod1("this");
}
else
if (element is string)
{
ThrowGenericMethod2<T, int, string, List<bool>>(element, 42, 666, "is", new List<bool> { true }, new List<int> { 42 });
}
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
// TKey1 = string
// TValue = int
// TKey2 = string
// TKey3 = List<bool>
// TKey4 = List<TValue>
private void ThrowGenericMethod2<TKey1, TValue, TKey2, TKey3>(TKey1 key1, TValue value1, TValue value2, TKey2 key2, TKey3 key3, List<TValue> listOfTValue)
{
var generator = new GenericClassForValueTypeTest<int, bool>();
generator.ThrowOneGenericFromType(true);
}
}
internal class GenericClassForValueTypeTest<TKey, TValue>
{
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public void ThrowOneGenericFromType(TValue value)
{
ThrowOneGenericFromMethod(value);
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public void ThrowOneGenericFromMethod<T>(T value)
{
var generator = new GenericClass<string, T>();
generator.ThrowOneGeneric(value);
}
}
internal class GenericClass<TKey, TValue>
{
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public void ThrowOneGeneric(TValue value)
{
ThrowGenericFromGeneric("this is the end...");
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowGenericFromGeneric<T>(T element)
{
ThrowFromGeneric(element, default(TKey), default(TValue), default(TKey));
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ThrowFromGeneric<T>(T element, TKey key1, TValue value, TKey key2)
{
try
{
Thread.Sleep(200);
throw new InvalidOperationException("IOE - " + value.ToString());
}
catch (Exception)
{
}
}
}
internal class MyClass
{
public string Member;
}
internal struct MyStruct
{
public int Member;
}
}
#pragma warning restore SA1201 // Elements should appear in the correct order
#pragma warning restore SA1401 // Fields should be private
#pragma warning restore SA1402 // File may only contain a single type