module main import scheme fn main() { $if !gcboehm ? { panic('Build with Boehm GC to measure allocation totals.') } println('Compilation, source construction, and warm-up excluded; no timings.') println('Totals include fixed host-call overhead and GC allocator rounding.') println('case iterations/characters allocated bytes') mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 16 }) or { panic(err) } port := vm.open_output_string() or { panic(err) } vm.set_output_port(port) or { panic(err) } same := vm.lookup('eq?') or { panic(err) } for bound in [false, true] { procedure := vm.eval(if bound { '(lambda (n p) (parameterize ((current-output-port p)) (let loop ((i n)) (if (= i 0) (current-output-port) (begin (current-output-port) (loop (- i 1)))))))' } else { '(lambda (n p) (let loop ((i n)) (if (= i 0) (current-output-port) (begin (current-output-port) (loop (- i 1))))))' }) or { panic(err) } vm.call(procedure, [scheme.integer(100), port]) or { panic(err) } for count in [1000, 100000, 1000000] { args := [scheme.integer(count), port] gc_collect() before := gc_heap_usage().total_bytes result := vm.call(procedure, args) or { panic(err) } allocated := gc_heap_usage().total_bytes - before if !(vm.call(same, [result, port]) or { panic(err) }).is_true() { panic('incorrect selected port') } name := if bound { 'bound-current-port' } else { 'default-current-port' } println('${name:23} ${count:15} ${allocated:20}') } } for reading in [false, true] { procedure := vm.eval(if reading { '(lambda (s) (call-with-port (open-input-string s) (lambda (p) (read-line p))))' } else { '(lambda (s) (call-with-port (open-output-string) (lambda (p) (write-string s p) (get-output-string p))))' }) or { panic(err) } vm.call(procedure, [scheme.string_value('λ')]) or { panic(err) } for count in [1000, 10000, 100000] { text := 'λ'.repeat(count) args := [scheme.string_value(text)] 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_string() or { panic(err) } != text { panic('incorrect string result') } name := if reading { 'string-port-read' } else { 'string-port-write' } println('${name:23} ${count:15} ${allocated:20}') } } }