| 1 | module main |
| 2 | |
| 3 | import scheme |
| 4 | |
| 5 | struct SequenceCase { |
| 6 | name string |
| 7 | program string |
| 8 | text bool |
| 9 | } |
| 10 | |
| 11 | fn main() { |
| 12 | $if !gcboehm ? { |
| 13 | panic('Build with Boehm GC to measure allocation totals.') |
| 14 | } |
| 15 | println('Source compilation, input construction, warm-up, and validation excluded.') |
| 16 | println('Allocation totals include fixed VM.call costs and allocator rounding; no timings.') |
| 17 | println('case elements allocated bytes') |
| 18 | for item in [ |
| 19 | SequenceCase{'vector-copy', '(lambda (x) (vector-copy x))', false}, |
| 20 | SequenceCase{'vector-map', '(lambda (x) (vector-map (lambda (v) v) x))', false}, |
| 21 | SequenceCase{'vector-overlap', '(lambda (x) (vector-copy! x 1 x 0 (- (vector-length x) 1)) x)', false}, |
| 22 | SequenceCase{'string-copy', '(lambda (x) (string-copy x))', true}, |
| 23 | SequenceCase{'string-map', '(lambda (x) (string-map (lambda (v) v) x))', true}, |
| 24 | SequenceCase{'string-overlap', '(lambda (x) (string-copy! x 1 x 0 (- (string-length x) 1)) x)', true}, |
| 25 | ] { |
| 26 | mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 16 }) or { panic(err) } |
| 27 | procedure := vm.eval(item.program) or { panic(err) } |
| 28 | make := vm.lookup('make-vector') or { panic(err) } |
| 29 | size := vm.lookup(if item.text { 'string-length' } else { 'vector-length' }) or { panic(err) } |
| 30 | warm := if item.text { |
| 31 | scheme.string_value('λ'.repeat(10)) |
| 32 | } else { |
| 33 | vm.call(make, [scheme.integer(10), scheme.integer(1)]) or { panic(err) } |
| 34 | } |
| 35 | vm.call(procedure, [warm]) or { panic(err) } |
| 36 | for count in [1000, 10000, 100000] { |
| 37 | input := if item.text { |
| 38 | scheme.string_value('λ'.repeat(count)) |
| 39 | } else { |
| 40 | vm.call(make, [scheme.integer(count), scheme.integer(1)]) or { panic(err) } |
| 41 | } |
| 42 | call_args := [input] |
| 43 | gc_collect() |
| 44 | before := gc_heap_usage().total_bytes |
| 45 | result := vm.call(procedure, call_args) or { panic(err) } |
| 46 | allocated := gc_heap_usage().total_bytes - before |
| 47 | actual := vm.call(size, [result]) or { panic(err) }.as_integer() or { panic(err) } |
| 48 | if actual != count { panic('incorrect ${item.name} result length') } |
| 49 | println('${item.name:20} ${count:10} ${allocated:20}') |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |