-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmarkPrograms.cs
More file actions
286 lines (270 loc) · 11.5 KB
/
Copy pathBenchmarkPrograms.cs
File metadata and controls
286 lines (270 loc) · 11.5 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
274
275
276
277
278
279
280
281
282
283
284
285
286
using System.Globalization;
using System.Text;
using DotPython.Language.Text;
namespace DotPython.Benchmarks;
internal static class BenchmarkPrograms
{
internal static SourceText CreateFrontEndSource(int functionCount)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(functionCount);
var source = new StringBuilder(functionCount * 160);
for (var index = 0; index < functionCount; index++)
{
source
.Append("def calculate_")
.Append(index.ToString(CultureInfo.InvariantCulture))
.Append("(value):\n")
.Append(" result = value + 1\n")
.Append(" while result < 100:\n")
.Append(" result = result + 3\n")
.Append(" if result > 100:\n")
.Append(" return result\n")
.Append(" return value\n");
}
return new SourceText(source.ToString(), "front-end-benchmark.py");
}
internal static SourceText CreateRuntimeSource(int iterations)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(iterations);
var source =
"def calculate(limit):\n"
+ " total = 0\n"
+ " current = 0\n"
+ " while current < limit:\n"
+ " total = total + current * 2\n"
+ " current = current + 1\n"
+ " return total\n"
+ "result = calculate("
+ iterations.ToString(CultureInfo.InvariantCulture)
+ ")\n";
return new SourceText(source, "runtime-benchmark.py");
}
internal static SourceText CreateComparisonSpecializationSource(
ComparisonOperandFamily operandFamily,
OrderedComparisonOperation operation
)
{
var ascendingOperands = operandFamily switch
{
ComparisonOperandFamily.WholeNumber => (Left: "10000", Right: "11000"),
ComparisonOperandFamily.FloatingPoint => (Left: "10000.0", Right: "11000.0"),
_ => throw new ArgumentOutOfRangeException(nameof(operandFamily)),
};
var operands = operation
is OrderedComparisonOperation.LessThan
or OrderedComparisonOperation.LessThanOrEqual
? ascendingOperands
: (Left: ascendingOperands.Right, Right: ascendingOperands.Left);
var sourceOperator = operation switch
{
OrderedComparisonOperation.LessThan => "<",
OrderedComparisonOperation.LessThanOrEqual => "<=",
OrderedComparisonOperation.GreaterThan => ">",
OrderedComparisonOperation.GreaterThanOrEqual => ">=",
_ => throw new ArgumentOutOfRangeException(nameof(operation)),
};
return new SourceText(
"def compare(left, right): return left "
+ sourceOperator
+ " right\n"
+ "def compare_values():\n"
+ " current = 0\n"
+ " value = False\n"
+ " while current != 10000:\n"
+ " value = compare("
+ operands.Left
+ ", "
+ operands.Right
+ ")\n"
+ " current = current + 1\n"
+ " return value\n",
"comparison-specialization-benchmark.py"
);
}
internal static SourceText CreateManagedCallDispatchSource(int argumentCount)
{
var (callee, inlineAssignment, call) = argumentCount switch
{
0 => ("def callee(): return None\n", " value = None\n", "callee()"),
1 => ("def callee(value): return value\n", " value = value\n", "callee(value)"),
_ => throw new ArgumentOutOfRangeException(nameof(argumentCount)),
};
return new SourceText(
callee
+ "def inline_loop():\n"
+ " current = 0\n"
+ " value = None\n"
+ " while current != 10000:\n"
+ inlineAssignment
+ " current = current + 1\n"
+ " return value\n"
+ "def call_loop():\n"
+ " current = 0\n"
+ " value = None\n"
+ " while current != 10000:\n"
+ " value = "
+ call
+ "\n"
+ " current = current + 1\n"
+ " return value\n",
"managed-call-dispatch-benchmark.py"
);
}
internal static SourceText CreateManagedCallSpecializationSource(int argumentCount)
{
var (callee, call) = argumentCount switch
{
0 => ("def callee(): return None\n", "target()"),
1 => ("def callee(value): return value\n", "target(value)"),
_ => throw new ArgumentOutOfRangeException(nameof(argumentCount)),
};
return new SourceText(
callee
+ "target = print\n"
+ "def call_loop():\n"
+ " current = 0\n"
+ " value = None\n"
+ " while current != 10000:\n"
+ " value = "
+ call
+ "\n"
+ " current = current + 1\n"
+ " return value\n",
"managed-call-specialization-benchmark.py"
);
}
internal static SourceText CreateManagedFramePathSource(int argumentCount)
{
var (callee, inlineAssignment, call) = argumentCount switch
{
0 => ("def callee(): return None\n", " value = None\n", "target()"),
1 => ("def callee(value): return value\n", " value = value\n", "target(value)"),
_ => throw new ArgumentOutOfRangeException(nameof(argumentCount)),
};
return new SourceText(
callee
+ "def inline_loop(target):\n"
+ " current = 0\n"
+ " value = None\n"
+ " while current != 10000:\n"
+ inlineAssignment
+ " current = current + 1\n"
+ " return value\n"
+ "def call_loop(target):\n"
+ " current = 0\n"
+ " value = None\n"
+ " while current != 10000:\n"
+ " value = "
+ call
+ "\n"
+ " current = current + 1\n"
+ " return value\n"
+ "def run_inline(): return inline_loop(callee)\n"
+ "def run_calls(): return call_loop(callee)\n",
"managed-frame-path-benchmark.py"
);
}
internal static SourceText CreateManagedReturnPathSource() =>
new(
"def bare_return(): return\n"
+ "def explicit_none(): return None\n"
+ "def call_bare_return():\n"
+ " current = 0\n"
+ " value = None\n"
+ " while current != 10000:\n"
+ " value = bare_return()\n"
+ " current = current + 1\n"
+ " return value\n"
+ "def call_explicit_none():\n"
+ " current = 0\n"
+ " value = None\n"
+ " while current != 10000:\n"
+ " value = explicit_none()\n"
+ " current = current + 1\n"
+ " return value\n",
"managed-return-path-benchmark.py"
);
internal static SourceText CreateAllocationSource(RuntimeAllocationScenario scenario) =>
new(
scenario switch
{
RuntimeAllocationScenario.Empty => string.Empty,
RuntimeAllocationScenario.Constants => CreateConstantLoads(),
RuntimeAllocationScenario.IntegerLoop => "current = 0\n"
+ "while current < 1000:\n"
+ " current = current + 1\n",
RuntimeAllocationScenario.LargeIntegerLoop => "current = 10000\n"
+ "while current < 11000:\n"
+ " current = current + 1\n",
RuntimeAllocationScenario.LocalIntegerAddition => "def add_integers():\n"
+ " current = 10000\n"
+ " while current < 11000:\n"
+ " current = current + 1\n"
+ " return current\n"
+ "add_integers()\n",
RuntimeAllocationScenario.LocalFloatingPointAddition => "def add_floats():\n"
+ " current = 10000.0\n"
+ " while current < 11000.0:\n"
+ " current = current + 1.0\n"
+ " return current\n"
+ "add_floats()\n",
RuntimeAllocationScenario.IntegerLessThan => CreateLessThanCalls("10000", "11000"),
RuntimeAllocationScenario.FloatingPointLessThan => CreateLessThanCalls(
"10000.0",
"11000.0"
),
RuntimeAllocationScenario.TextLessThan => CreateLessThanCalls("'Dot'", "'Python'"),
RuntimeAllocationScenario.FunctionCallsNoArguments => "global_value = 42\n"
+ "def get_value(): return global_value\n"
+ "current = 0\n"
+ "while current < 1000:\n"
+ " value = get_value()\n"
+ " current = current + 1\n",
RuntimeAllocationScenario.FunctionCalls =>
"def increment(value): return value + 1\n"
+ "current = 0\n"
+ "while current < 1000:\n"
+ " current = increment(current)\n",
RuntimeAllocationScenario.GlobalLookup => "global_value = 42\n"
+ "current = 0\n"
+ "while current < 1000:\n"
+ " value = global_value\n"
+ " current = current + 1\n",
RuntimeAllocationScenario.MutableGlobalLookup => "global_value = 0\n"
+ "current = 0\n"
+ "while current < 1000:\n"
+ " value = global_value\n"
+ " global_value = global_value + 1\n"
+ " current = current + 1\n",
RuntimeAllocationScenario.BuiltinLookup => "def lookup_builtin():\n"
+ " current = 0\n"
+ " while current < 1000:\n"
+ " value = print\n"
+ " current = current + 1\n"
+ "lookup_builtin()\n",
_ => throw new ArgumentOutOfRangeException(nameof(scenario)),
},
"allocation-benchmark.py"
);
private static string CreateConstantLoads()
{
var source = new StringBuilder(1_100);
for (var index = 0; index < 100; index++)
{
source.Append("value = 42\n");
}
return source.ToString();
}
private static string CreateLessThanCalls(string left, string right) =>
"def less_than(left, right): return left < right\n"
+ "def compare_values():\n"
+ " current = 0\n"
+ " while current != 1000:\n"
+ " value = less_than("
+ left
+ ", "
+ right
+ ")\n"
+ " current = current + 1\n"
+ "compare_values()\n";
}