| 1 | module main |
| 2 | |
| 3 | import arena |
| 4 | import scheme |
| 5 | |
| 6 | fn measure_loop_allocation(name string, source string, alternate bool) { |
| 7 | mut region := arena.new() |
| 8 | region.push() |
| 9 | mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0 }) or { panic(err.msg()) } |
| 10 | if alternate { |
| 11 | vm.eval_alt(source) or { panic(err.msg()) } |
| 12 | } else { |
| 13 | vm.eval(source) or { panic(err.msg()) } |
| 14 | } |
| 15 | run := vm.lookup('run') or { panic(err.msg()) } |
| 16 | mut call_args := [scheme.integer(1)] |
| 17 | vm.call(run, call_args) or { panic(err.msg()) } |
| 18 | for count in [1, 101, 10001] { |
| 19 | call_args[0] = scheme.integer(count) |
| 20 | before := region.used() |
| 21 | result := vm.call(run, call_args) or { panic(err.msg()) } |
| 22 | allocated := region.used() - before |
| 23 | if result.as_integer() or { panic(err.msg()) } != 42 { panic('loop result mismatch') } |
| 24 | println('{"case":"${name}","iterations":${count},"arena_bytes":${allocated}}') |
| 25 | } |
| 26 | region.pop() |
| 27 | region.free() |
| 28 | } |
| 29 | |
| 30 | fn main() { |
| 31 | measure_loop_allocation('scheme-ordinary', '(define (run n) (do ((i 0 (+ i 1))) ((= i n) 42)))', false) |
| 32 | measure_loop_allocation('scheme-continue', '(define (run n) (do ((i 0 (+ i 1))) ((= i n) 42) (continue)))', false) |
| 33 | measure_loop_allocation('vile-ordinary', 'run(n):={for(i:=0,i<n,i=i+1){}\n42}', true) |
| 34 | measure_loop_allocation('vile-continue', 'run(n):={for(i:=0,i<n,i=i+1){continue}\n42}', true) |
| 35 | } |
| 36 | |