| 1 | module main |
| 2 | |
| 3 | import scheme |
| 4 | |
| 5 | struct NamespaceCase { |
| 6 | name string |
| 7 | expression string |
| 8 | } |
| 9 | |
| 10 | fn main() { |
| 11 | $if !gcboehm ? { |
| 12 | panic('Build with Boehm GC to measure allocation totals.') |
| 13 | } |
| 14 | println('Compilation, namespace/import setup, warm-up, and validation excluded; no timings.') |
| 15 | println('Allocation totals include fixed host-call overhead and GC allocator rounding.') |
| 16 | println('case iterations allocated bytes') |
| 17 | for item in [ |
| 18 | NamespaceCase{'imported-variable', 'shared'}, |
| 19 | NamespaceCase{'private-macro-read', '(private-get)'}, |
| 20 | NamespaceCase{'private-macro-write', '(private-set i)'}, |
| 21 | ] { |
| 22 | mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 16 }) or { panic(err) } |
| 23 | root := vm.interaction_environment() or { panic(err) } |
| 24 | source := vm.new_environment() or { panic(err) } |
| 25 | consumer := vm.new_environment() or { panic(err) } |
| 26 | vm.import_bindings(consumer, root, { |
| 27 | '-': '-' |
| 28 | '=': '=' |
| 29 | }) or { panic(err) } |
| 30 | vm.eval_in('(define x 1) |
| 31 | (define-syntax get (syntax-rules () ((_) x))) |
| 32 | (define-syntax put (syntax-rules () ((_ v) (set! x v))))', source) or { panic(err) } |
| 33 | vm.import_bindings(consumer, source, { |
| 34 | 'x': 'shared' |
| 35 | 'get': 'private-get' |
| 36 | 'put': 'private-set' |
| 37 | }) or { panic(err) } |
| 38 | procedure := vm.eval_in('(lambda (n) (let loop ((i n)) |
| 39 | (if (= i 0) shared (begin ${item.expression} (loop (- i 1))))))', consumer) or { panic(err) } |
| 40 | vm.call(procedure, [scheme.integer(100)]) or { panic(err) } |
| 41 | for count in [1000, 100000, 1000000] { |
| 42 | args := [scheme.integer(count)] |
| 43 | gc_collect() |
| 44 | before := gc_heap_usage().total_bytes |
| 45 | result := vm.call(procedure, args) or { panic(err) } |
| 46 | allocated := gc_heap_usage().total_bytes - before |
| 47 | if result.as_integer() or { panic(err) } != 1 { panic('incorrect ${item.name} result') } |
| 48 | println('${item.name:24} ${count:10} ${allocated:20}') |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |