| 1 | module main |
| 2 | |
| 3 | import crypto.sha256 |
| 4 | import os |
| 5 | import scheme |
| 6 | import time |
| 7 | |
| 8 | struct ReturnBenchCase { |
| 9 | name string |
| 10 | source string |
| 11 | expected i64 |
| 12 | } |
| 13 | |
| 14 | fn return_cases(extended bool) []ReturnBenchCase { |
| 15 | mut cases := [ |
| 16 | ReturnBenchCase{'tail-100k', '(define (sum n a) (if (= n 0) a (sum (- n 1) (+ n a)))) (define (benchmark) (sum 100000 0))', 5000050000}, |
| 17 | ReturnBenchCase{'fib-20', '(define (fib n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) (define (benchmark) (fib 20))', 6765}, |
| 18 | ReturnBenchCase{'closure-5k', '(define (make-adder x) (lambda(y)(+ x y))) (define add7 (make-adder 7)) (define (loop n a) (if (= n 0) a (loop (- n 1) (add7 a)))) (define (benchmark) (loop 5000 0))', 35000}, |
| 19 | ReturnBenchCase{'list-3k', '(define (build n xs) (if (= n 0) xs (build (- n 1) (cons n xs)))) (define (walk xs a) (if (null? xs) a (walk (cdr xs) (+ a (car xs))))) (define (benchmark) (walk (build 3000 (list)) 0))', 4501500}, |
| 20 | ] |
| 21 | if extended { |
| 22 | cases << ReturnBenchCase{'return-tail-100k', '(define (sum n a) (if (= n 0) (return a) #f) (return (sum (- n 1) (+ n a))) missing) (define (benchmark) (sum 100000 0))', 5000050000} |
| 23 | cases << ReturnBenchCase{'return-scalar-100k', '(define (answer x) (return x) missing) (define (loop n) (if (= n 0) 42 (begin (answer 42) (loop (- n 1))))) (define (benchmark) (loop 100000))', 42} |
| 24 | cases << ReturnBenchCase{'return-helper-5k', '(define (answer x) (let () (return (+ x 0))) missing) (define (loop n) (if (= n 0) 42 (begin (answer 42) (loop (- n 1))))) (define (benchmark) (loop 5000))', 42} |
| 25 | cases << ReturnBenchCase{'return-parameter-5k', '(define p (make-parameter 1)) (define (answer x) (parameterize ((p x)) (return (p))) missing) (define (loop n) (if (= n 0) 42 (begin (answer 42) (loop (- n 1))))) (define (benchmark) (loop 5000))', 42} |
| 26 | } |
| 27 | return cases |
| 28 | } |
| 29 | |
| 30 | fn measure_return_case(c ReturnBenchCase, samples int, eval_source bool) ! { |
| 31 | mut prepared := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 64 })! |
| 32 | prepared.eval(c.source)! |
| 33 | procedure := prepared.lookup('benchmark')! |
| 34 | if prepared.call(procedure, [])!.as_integer()! != c.expected { |
| 35 | return error('warmup result mismatch') |
| 36 | } |
| 37 | source := c.source + ' (benchmark)' |
| 38 | mut times := []u64{len: samples} |
| 39 | mut allocated := []u64{len: samples} |
| 40 | for i in 0 .. samples { |
| 41 | mut fresh := if eval_source { |
| 42 | scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 64 })! |
| 43 | } else { |
| 44 | prepared |
| 45 | } |
| 46 | gc_collect() |
| 47 | before := gc_heap_usage().total_bytes |
| 48 | start := time.sys_mono_now() |
| 49 | value := if eval_source { fresh.eval(source)! } else { prepared.call(procedure, [])! } |
| 50 | times[i] = time.sys_mono_now() - start |
| 51 | allocated[i] = u64(gc_heap_usage().total_bytes - before) |
| 52 | if value.as_integer()! != c.expected { return error('${c.name}: result mismatch') } |
| 53 | } |
| 54 | times.sort() |
| 55 | allocated.sort() |
| 56 | stage := if eval_source { 'eval' } else { 'call' } |
| 57 | println('{"case":"${c.name}","stage":"${stage}","sha256":"${sha256.hexhash(source)}","samples":${samples},"ns":${times[samples / 2]},"allocated_bytes":${allocated[samples / 2]},"result":${c.expected}}') |
| 58 | } |
| 59 | |
| 60 | fn main() { |
| 61 | $if !gcboehm ? { |
| 62 | panic('Use the default Boehm GC for allocation counters.') |
| 63 | } |
| 64 | samples := if os.args.len > 1 { os.args[1].int() } else { 9 } |
| 65 | if samples < 1 || samples > 101 || samples % 2 == 0 { |
| 66 | panic('usage: early_return [odd samples 1..101] [--returns] [--loops]') |
| 67 | } |
| 68 | mut cases := return_cases(os.args.contains('--returns')) |
| 69 | if os.args.contains('--loops') { |
| 70 | cases << ReturnBenchCase{'do-100k', '(define (benchmark) (do ((i 0 (+ i 1)) (a 0 (+ a i))) ((= i 100000) a)))', 4999950000} |
| 71 | cases << ReturnBenchCase{'break-do-100k', '(define (benchmark) (define a 0) (do ((i 0 (+ i 1))) ((> i 100000) missing) (if (= i 100000) (break)) (set! a (+ a i))) a)', 4999950000} |
| 72 | cases << ReturnBenchCase{'break-parameter-100k', '(define p (make-parameter 1)) (define (benchmark) (define a 0) (do ((i 0 (+ i 1))) ((> i 100000) missing) (if (= i 100000) (parameterize ((p 42)) (break))) (set! a (+ a i))) a)', 4999950000} |
| 73 | cases << ReturnBenchCase{'continue-do-100k', '(define (benchmark) (do ((i 0 (+ i 1)) (a 0 (+ a i))) ((= i 100000) a) (continue) missing))', 4999950000} |
| 74 | } |
| 75 | for c in cases { |
| 76 | for eval_source in [false, true] { |
| 77 | measure_return_case(c, samples, eval_source) or { panic(err.msg()) } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |