module main import scheme struct NumericCase { name string expression string paired bool } fn main() { $if !gcboehm ? { panic('Build with Boehm GC to inspect allocation totals.') } println('Compilation, warm-up, and result validation excluded; no timings.') println('Scalar operations should not allocate per iteration; paired results own a result container.') println('case iterations allocated bytes') for item in [ NumericCase{'floor-quotient', '(floor-quotient (- i) 3)', false}, NumericCase{'truncate-remainder', '(truncate-remainder (- i) 3)', false}, NumericCase{'square', '(square i)', false}, NumericCase{'paired-floor', '(floor/ (- i) 3)', true}, ] { mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 24 }) or { panic(err) } procedure := vm.eval('(lambda (n) (let loop ((i n)) (if (= i 0) 42 (begin ${item.expression} (loop (- i 1))))))') or { panic(err) } vm.call(procedure, [scheme.integer(100)]) or { panic(err) } counts := if item.paired { [1000, 10000, 100000] } else { [1000, 100000, 1000000] } for count in counts { args := [scheme.integer(count)] gc_collect() before := gc_heap_usage().total_bytes result := vm.call(procedure, args) or { panic(err) } allocated := gc_heap_usage().total_bytes - before if result.as_integer() or { panic(err) } != 42 { panic('incorrect ${item.name} result') } println('${item.name:24} ${count:10} ${allocated:20}') } } }