| 1 | module main |
| 2 | |
| 3 | import scheme |
| 4 | |
| 5 | struct RecordCase { |
| 6 | name string |
| 7 | program string |
| 8 | expected i64 |
| 9 | } |
| 10 | |
| 11 | fn main() { |
| 12 | $if !gcboehm ? { |
| 13 | panic('Build with Boehm GC to measure allocation totals.') |
| 14 | } |
| 15 | println('Compilation, declaration setup, warm-up, and validation excluded; no timings.') |
| 16 | println('Allocation totals include fixed host-call overhead and GC allocator rounding.') |
| 17 | println('case iterations allocated bytes') |
| 18 | for item in [ |
| 19 | RecordCase{'record-get', '(lambda (n) (let loop ((i n)) (if (= i 0) (point-x p) (begin (point-x p) (loop (- i 1))))))', 1}, |
| 20 | RecordCase{'record-set', '(lambda (n) (let loop ((i n)) (if (= i 0) (point-x p) (begin (set-point-x! p i) (loop (- i 1))))))', 1}, |
| 21 | RecordCase{'record-predicate', '(lambda (n) (let loop ((i n)) (if (= i 0) 42 (begin (point? p) (loop (- i 1))))))', 42}, |
| 22 | RecordCase{'record-construct', '(lambda (n) (let loop ((i n)) (if (= i 0) 42 (begin (make-point i i) (loop (- i 1))))))', 42}, |
| 23 | ] { |
| 24 | mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 16 }) or { panic(err) } |
| 25 | vm.eval('(define-record-type point (make-point x y) point? (x point-x set-point-x!) (y point-y)) (define p (make-point 1 2))') or { panic(err) } |
| 26 | procedure := vm.eval(item.program) or { panic(err) } |
| 27 | vm.call(procedure, [scheme.integer(100)]) or { panic(err) } |
| 28 | for count in [1000, 100000, 1000000] { |
| 29 | args := [scheme.integer(count)] |
| 30 | gc_collect() |
| 31 | before := gc_heap_usage().total_bytes |
| 32 | result := vm.call(procedure, args) or { panic(err) } |
| 33 | allocated := gc_heap_usage().total_bytes - before |
| 34 | if result.as_integer() or { panic(err) } != item.expected { |
| 35 | panic('incorrect ${item.name} result') |
| 36 | } |
| 37 | println('${item.name:24} ${count:10} ${allocated:20}') |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |