| 1 | module main |
| 2 | |
| 3 | import scheme |
| 4 | |
| 5 | fn main() { |
| 6 | $if !gcboehm ? { |
| 7 | panic('Build with Boehm GC to measure allocation totals.') |
| 8 | } |
| 9 | println('Compilation, source construction, and warm-up excluded; no timings.') |
| 10 | println('Totals include fixed host-call overhead and GC allocator rounding.') |
| 11 | println('case iterations/characters allocated bytes') |
| 12 | mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 16 }) or { panic(err) } |
| 13 | port := vm.open_output_string() or { panic(err) } |
| 14 | vm.set_output_port(port) or { panic(err) } |
| 15 | same := vm.lookup('eq?') or { panic(err) } |
| 16 | for bound in [false, true] { |
| 17 | procedure := vm.eval(if bound { |
| 18 | '(lambda (n p) (parameterize ((current-output-port p)) |
| 19 | (let loop ((i n)) (if (= i 0) (current-output-port) |
| 20 | (begin (current-output-port) (loop (- i 1)))))))' |
| 21 | } else { |
| 22 | '(lambda (n p) (let loop ((i n)) (if (= i 0) (current-output-port) |
| 23 | (begin (current-output-port) (loop (- i 1))))))' |
| 24 | }) or { panic(err) } |
| 25 | vm.call(procedure, [scheme.integer(100), port]) or { panic(err) } |
| 26 | for count in [1000, 100000, 1000000] { |
| 27 | args := [scheme.integer(count), port] |
| 28 | gc_collect() |
| 29 | before := gc_heap_usage().total_bytes |
| 30 | result := vm.call(procedure, args) or { panic(err) } |
| 31 | allocated := gc_heap_usage().total_bytes - before |
| 32 | if !(vm.call(same, [result, port]) or { panic(err) }).is_true() { |
| 33 | panic('incorrect selected port') |
| 34 | } |
| 35 | name := if bound { 'bound-current-port' } else { 'default-current-port' } |
| 36 | println('${name:23} ${count:15} ${allocated:20}') |
| 37 | } |
| 38 | } |
| 39 | for reading in [false, true] { |
| 40 | procedure := vm.eval(if reading { |
| 41 | '(lambda (s) (call-with-port (open-input-string s) (lambda (p) (read-line p))))' |
| 42 | } else { |
| 43 | '(lambda (s) (call-with-port (open-output-string) |
| 44 | (lambda (p) (write-string s p) (get-output-string p))))' |
| 45 | }) or { panic(err) } |
| 46 | vm.call(procedure, [scheme.string_value('λ')]) or { panic(err) } |
| 47 | for count in [1000, 10000, 100000] { |
| 48 | text := 'λ'.repeat(count) |
| 49 | args := [scheme.string_value(text)] |
| 50 | gc_collect() |
| 51 | before := gc_heap_usage().total_bytes |
| 52 | result := vm.call(procedure, args) or { panic(err) } |
| 53 | allocated := gc_heap_usage().total_bytes - before |
| 54 | if result.as_string() or { panic(err) } != text { panic('incorrect string result') } |
| 55 | name := if reading { 'string-port-read' } else { 'string-port-write' } |
| 56 | println('${name:23} ${count:15} ${allocated:20}') |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |