| 1 | module main |
| 2 | |
| 3 | import scheme |
| 4 | |
| 5 | struct NumericCase { |
| 6 | name string |
| 7 | expression string |
| 8 | paired bool |
| 9 | } |
| 10 | |
| 11 | fn main() { |
| 12 | $if !gcboehm ? { |
| 13 | panic('Build with Boehm GC to inspect allocation totals.') |
| 14 | } |
| 15 | println('Compilation, warm-up, and result validation excluded; no timings.') |
| 16 | println('Scalar operations should not allocate per iteration; paired results own a result container.') |
| 17 | println('case iterations allocated bytes') |
| 18 | for item in [ |
| 19 | NumericCase{'floor-quotient', '(floor-quotient (- i) 3)', false}, |
| 20 | NumericCase{'truncate-remainder', '(truncate-remainder (- i) 3)', false}, |
| 21 | NumericCase{'square', '(square i)', false}, |
| 22 | NumericCase{'paired-floor', '(floor/ (- i) 3)', true}, |
| 23 | ] { |
| 24 | mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 24 }) or { panic(err) } |
| 25 | procedure := vm.eval('(lambda (n) (let loop ((i n)) (if (= i 0) 42 (begin ${item.expression} (loop (- i 1))))))') or { panic(err) } |
| 26 | vm.call(procedure, [scheme.integer(100)]) or { panic(err) } |
| 27 | counts := if item.paired { [1000, 10000, 100000] } else { [1000, 100000, 1000000] } |
| 28 | for count in counts { |
| 29 | args := [scheme.integer(count)] |
| 30 | gc_collect() |
| 31 | before := gc_heap_usage().total_bytes |
| 32 | result := vm.call(procedure, args) or { panic(err) } |
| 33 | allocated := gc_heap_usage().total_bytes - before |
| 34 | if result.as_integer() or { panic(err) } != 42 { |
| 35 | panic('incorrect ${item.name} result') |
| 36 | } |
| 37 | println('${item.name:24} ${count:10} ${allocated:20}') |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |