module main import scheme struct SequenceCase { name string program string text bool } fn main() { $if !gcboehm ? { panic('Build with Boehm GC to measure allocation totals.') } println('Source compilation, input construction, warm-up, and validation excluded.') println('Allocation totals include fixed VM.call costs and allocator rounding; no timings.') println('case elements allocated bytes') for item in [ SequenceCase{'vector-copy', '(lambda (x) (vector-copy x))', false}, SequenceCase{'vector-map', '(lambda (x) (vector-map (lambda (v) v) x))', false}, SequenceCase{'vector-overlap', '(lambda (x) (vector-copy! x 1 x 0 (- (vector-length x) 1)) x)', false}, SequenceCase{'string-copy', '(lambda (x) (string-copy x))', true}, SequenceCase{'string-map', '(lambda (x) (string-map (lambda (v) v) x))', true}, SequenceCase{'string-overlap', '(lambda (x) (string-copy! x 1 x 0 (- (string-length x) 1)) x)', true}, ] { mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 16 }) or { panic(err) } procedure := vm.eval(item.program) or { panic(err) } make := vm.lookup('make-vector') or { panic(err) } size := vm.lookup(if item.text { 'string-length' } else { 'vector-length' }) or { panic(err) } warm := if item.text { scheme.string_value('λ'.repeat(10)) } else { vm.call(make, [scheme.integer(10), scheme.integer(1)]) or { panic(err) } } vm.call(procedure, [warm]) or { panic(err) } for count in [1000, 10000, 100000] { input := if item.text { scheme.string_value('λ'.repeat(count)) } else { vm.call(make, [scheme.integer(count), scheme.integer(1)]) or { panic(err) } } call_args := [input] gc_collect() before := gc_heap_usage().total_bytes result := vm.call(procedure, call_args) or { panic(err) } allocated := gc_heap_usage().total_bytes - before actual := vm.call(size, [result]) or { panic(err) }.as_integer() or { panic(err) } if actual != count { panic('incorrect ${item.name} result length') } println('${item.name:20} ${count:10} ${allocated:20}') } } }