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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#![allow(unused)]
extern crate dotscope;
use criterion::{criterion_group, criterion_main, Criterion};
use dotscope::assembly::{
decode_instruction, decode_stream, InstructionAssembler, InstructionEncoder,
};
use dotscope::metadata::token::Token;
use dotscope::Parser;
pub fn criterion_benchmark(c: &mut Criterion) {
// Simple method: basic arithmetic
c.bench_function("bench_assemble_simple_method", |b| {
b.iter(|| {
let mut asm = InstructionAssembler::new();
asm.ldarg_1()
.unwrap()
.ldarg_2()
.unwrap()
.add()
.unwrap()
.ret()
.unwrap();
asm.finish().unwrap()
});
});
// Complex method: loops, branches, and object operations
c.bench_function("bench_assemble_complex_method", |b| {
b.iter(|| {
let mut asm = InstructionAssembler::new();
asm.ldc_i4_0()
.unwrap() // int i = 0
.stloc_0()
.unwrap()
.br("loop_condition")
.unwrap()
.label("loop_start")
.unwrap()
.ldarg_0()
.unwrap() // Load array
.ldloc_0()
.unwrap() // Load index
.ldarg_1()
.unwrap() // Load value
.stelem_i4()
.unwrap() // array[i] = value
.ldloc_0()
.unwrap() // i++
.ldc_i4_1()
.unwrap()
.add()
.unwrap()
.stloc_0()
.unwrap()
.label("loop_condition")
.unwrap()
.ldloc_0()
.unwrap() // if (i < 10)
.ldc_i4_const(10)
.unwrap()
.clt()
.unwrap()
.brtrue("loop_start")
.unwrap()
.ret()
.unwrap();
asm.finish().unwrap()
});
});
// Object-heavy method: field access and method calls
c.bench_function("bench_assemble_object_method", |b| {
let field_token = Token::new(0x04000001);
let method_token = Token::new(0x06000001);
let type_token = Token::new(0x02000001);
b.iter(|| {
let mut asm = InstructionAssembler::new();
asm.ldarg_0()
.unwrap() // this
.ldfld(field_token)
.unwrap() // Load field
.ldnull()
.unwrap() // Compare with null
.ceq()
.unwrap()
.brfalse("not_null")
.unwrap()
.ldarg_0()
.unwrap() // Create new object
.newobj(method_token)
.unwrap()
.stfld(field_token)
.unwrap()
.label("not_null")
.unwrap()
.ldarg_0()
.unwrap() // Return field value
.ldfld(field_token)
.unwrap()
.callvirt(method_token)
.unwrap()
.ret()
.unwrap();
asm.finish().unwrap()
});
});
// Low-level encoder benchmark
c.bench_function("bench_assemble_encoder_direct", |b| {
b.iter(|| {
let mut encoder = InstructionEncoder::new();
encoder.emit_instruction("ldarg.1", None).unwrap();
encoder.emit_instruction("ldarg.2", None).unwrap();
encoder.emit_instruction("add", None).unwrap();
encoder.emit_instruction("ret", None).unwrap();
encoder.finalize().unwrap().0
});
});
// Roundtrip benchmark: assemble then disassemble
let simple_bytecode = {
let mut asm = InstructionAssembler::new();
asm.ldarg_1()
.unwrap()
.ldarg_2()
.unwrap()
.add()
.unwrap()
.ret()
.unwrap();
asm.finish().unwrap().0
};
c.bench_function("bench_roundtrip_simple", |b| {
b.iter(|| {
// Assemble
let mut asm = InstructionAssembler::new();
asm.ldarg_1()
.unwrap()
.ldarg_2()
.unwrap()
.add()
.unwrap()
.ret()
.unwrap();
let (bytecode, _max_stack, _) = asm.finish().unwrap();
// Disassemble
let mut parser = dotscope::Parser::new(&bytecode);
decode_stream(&mut parser, 0x1000).unwrap()
});
});
let complex_bytecode = {
let mut asm = InstructionAssembler::new();
asm.ldc_i4_0()
.unwrap()
.stloc_0()
.unwrap()
.br("loop_condition")
.unwrap()
.label("loop_start")
.unwrap()
.ldarg_0()
.unwrap()
.ldloc_0()
.unwrap()
.ldarg_1()
.unwrap()
.stelem_i4()
.unwrap()
.ldloc_0()
.unwrap()
.ldc_i4_1()
.unwrap()
.add()
.unwrap()
.stloc_0()
.unwrap()
.label("loop_condition")
.unwrap()
.ldloc_0()
.unwrap()
.ldc_i4_const(10)
.unwrap()
.clt()
.unwrap()
.brtrue("loop_start")
.unwrap()
.ret()
.unwrap();
asm.finish().unwrap().0
};
c.bench_function("bench_roundtrip_complex", |b| {
b.iter(|| {
// Assemble
let mut asm = InstructionAssembler::new();
asm.ldc_i4_0()
.unwrap()
.stloc_0()
.unwrap()
.br("loop_condition")
.unwrap()
.label("loop_start")
.unwrap()
.ldarg_0()
.unwrap()
.ldloc_0()
.unwrap()
.ldarg_1()
.unwrap()
.stelem_i4()
.unwrap()
.ldloc_0()
.unwrap()
.ldc_i4_1()
.unwrap()
.add()
.unwrap()
.stloc_0()
.unwrap()
.label("loop_condition")
.unwrap()
.ldloc_0()
.unwrap()
.ldc_i4_const(10)
.unwrap()
.clt()
.unwrap()
.brtrue("loop_start")
.unwrap()
.ret()
.unwrap();
let (bytecode, _max_stack, _) = asm.finish().unwrap();
// Disassemble
let mut parser = dotscope::Parser::new(&bytecode);
decode_stream(&mut parser, 0x1000).unwrap()
});
});
// Disassemble-only benchmarks for comparison
c.bench_function("bench_disassemble_simple", |b| {
b.iter(|| {
let mut parser = dotscope::Parser::new(&simple_bytecode);
decode_stream(&mut parser, 0x1000).unwrap()
});
});
c.bench_function("bench_disassemble_complex", |b| {
b.iter(|| {
let mut parser = dotscope::Parser::new(&complex_bytecode);
decode_stream(&mut parser, 0x1000).unwrap()
});
});
// Optimization benchmark: compare ldc_i4_const vs manual selection
c.bench_function("bench_assemble_with_optimizations", |b| {
b.iter(|| {
let mut asm = InstructionAssembler::new();
asm.ldc_i4_const(0)
.unwrap() // Should use ldc.i4.0
.ldc_i4_const(1)
.unwrap() // Should use ldc.i4.1
.ldc_i4_const(127)
.unwrap() // Should use ldc.i4.s
.ldc_i4_const(1000)
.unwrap() // Should use ldc.i4
.add()
.unwrap()
.add()
.unwrap()
.add()
.unwrap()
.ret()
.unwrap();
asm.finish().unwrap()
});
});
c.bench_function("bench_assemble_manual_selection", |b| {
b.iter(|| {
let mut asm = InstructionAssembler::new();
asm.ldc_i4_0()
.unwrap()
.ldc_i4_1()
.unwrap()
.ldc_i4_s(127)
.unwrap()
.ldc_i4(1000)
.unwrap()
.add()
.unwrap()
.add()
.unwrap()
.add()
.unwrap()
.ret()
.unwrap();
asm.finish().unwrap()
});
});
// Memory-intensive benchmark: large method with many labels
c.bench_function("bench_assemble_large_method", |b| {
b.iter(|| {
let mut asm = InstructionAssembler::new();
// Create a method with many branches and labels
for i in 0..50 {
asm.ldarg_0()
.unwrap()
.ldc_i4_const(i)
.unwrap()
.ceq()
.unwrap()
.brtrue(&format!("case_{i}"))
.unwrap();
}
asm.ldc_i4_m1().unwrap().ret().unwrap();
for i in 0..50 {
asm.label(&format!("case_{i}"))
.unwrap()
.ldc_i4_const(i * 2)
.unwrap()
.ret()
.unwrap();
}
asm.finish().unwrap()
});
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);