| 1 | module main |
| 2 | |
| 3 | import scheme |
| 4 | |
| 5 | struct ParameterCase { |
| 6 | name string |
| 7 | setup string |
| 8 | value i64 |
| 9 | } |
| 10 | |
| 11 | fn main() { |
| 12 | $if !gcboehm ? { |
| 13 | panic('Build with Boehm GC to measure allocation totals.') |
| 14 | } |
| 15 | println('Source compilation and warm-up excluded; VM.call includes fixed invocation overhead.') |
| 16 | println('No timing comparison: allocation counts should stay bounded as iterations grow.') |
| 17 | println('case iterations allocated bytes') |
| 18 | for item in [ |
| 19 | ParameterCase{'default-read', '(define p (make-parameter 42)) |
| 20 | (define (loop n) (if (= n 0) (p) (begin (p) (loop (- n 1))))) loop', 42}, |
| 21 | ParameterCase{'dynamic-read', '(define p (make-parameter 1)) |
| 22 | (define (loop n) (if (= n 0) (p) (begin (p) (loop (- n 1))))) |
| 23 | (lambda (n) (parameterize ((p 42)) (loop n)))', 42}, |
| 24 | ParameterCase{'default-write', '(define p (make-parameter 0)) |
| 25 | (define (loop n) (if (= n 0) (p) (begin (p n) (loop (- n 1))))) loop', 1}, |
| 26 | ParameterCase{'converted-write', '(define p (make-parameter 0 (lambda (x) x))) |
| 27 | (define (loop n) (if (= n 0) (p) (begin (p n) (loop (- n 1))))) loop', 1}, |
| 28 | ] { |
| 29 | mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 16 }) or { panic(err) } |
| 30 | procedure := vm.eval(item.setup) or { panic(err) } |
| 31 | vm.call(procedure, [scheme.integer(100)]) or { panic(err) } |
| 32 | for count in [1000, 100000, 1000000] { |
| 33 | call_args := [scheme.integer(count)] |
| 34 | gc_collect() |
| 35 | before := gc_heap_usage().total_bytes |
| 36 | result := vm.call(procedure, call_args) or { panic(err) } |
| 37 | allocated := gc_heap_usage().total_bytes - before |
| 38 | if result.as_integer() or { panic(err) } != item.value { |
| 39 | panic('incorrect ${item.name} result') |
| 40 | } |
| 41 | println('${item.name:19} ${count:10} ${allocated:20}') |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |