module main import scheme struct NamespaceCase { name string expression string } fn main() { $if !gcboehm ? { panic('Build with Boehm GC to measure allocation totals.') } println('Compilation, namespace/import setup, warm-up, and validation excluded; no timings.') println('Allocation totals include fixed host-call overhead and GC allocator rounding.') println('case iterations allocated bytes') for item in [ NamespaceCase{'imported-variable', 'shared'}, NamespaceCase{'private-macro-read', '(private-get)'}, NamespaceCase{'private-macro-write', '(private-set i)'}, ] { mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 16 }) or { panic(err) } root := vm.interaction_environment() or { panic(err) } source := vm.new_environment() or { panic(err) } consumer := vm.new_environment() or { panic(err) } vm.import_bindings(consumer, root, { '-': '-' '=': '=' }) or { panic(err) } vm.eval_in('(define x 1) (define-syntax get (syntax-rules () ((_) x))) (define-syntax put (syntax-rules () ((_ v) (set! x v))))', source) or { panic(err) } vm.import_bindings(consumer, source, { 'x': 'shared' 'get': 'private-get' 'put': 'private-set' }) or { panic(err) } procedure := vm.eval_in('(lambda (n) (let loop ((i n)) (if (= i 0) shared (begin ${item.expression} (loop (- i 1))))))', consumer) or { panic(err) } vm.call(procedure, [scheme.integer(100)]) or { panic(err) } for count in [1000, 100000, 1000000] { 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) } != 1 { panic('incorrect ${item.name} result') } println('${item.name:24} ${count:10} ${allocated:20}') } } }