random: encourage inlining.

Reduces overhead to 6% over #%kernel's  for the integer case.
This commit is contained in:
Vincent St-Amour 2016-01-02 19:14:16 -06:00
parent 740b6dc198
commit 47f06f11e1

View File

@ -17,6 +17,7 @@
"member.rkt" "member.rkt"
"kernstruct.rkt" "kernstruct.rkt"
"norm-arity.rkt" "norm-arity.rkt"
"performance-hint.rkt"
"top-int.rkt" "top-int.rkt"
'#%builtin ; so it's attached '#%builtin ; so it's attached
(for-syntax "kw.rkt" (for-syntax "kw.rkt"
@ -109,35 +110,36 @@
'random 'random
(string-append "integer greater than " (number->string x)) (string-append "integer greater than " (number->string x))
y)))) y))))
(define-values (-random) ; more featureful than #%kernel's `random` (begin-encourage-inline
(case-lambda (define-values (-random) ; more featureful than #%kernel's `random`
[() (random)] ; no args, random float (case-lambda
[(x) ; one arg, either random float with prng, or random integer [() (random)] ; no args, random float
;; can just pass through to #%kernel's `random`, which will do the [(x) ; one arg, either random float with prng, or random integer
;; necessary checking ;; can just pass through to #%kernel's `random`, which will do the
(random x)] ;; necessary checking
[(x y) (random x)]
;; two args, either max and prng, or min and max [(x y)
(cond [(exact-positive-integer? y) ; min and max case ;; two args, either max and prng, or min and max
(enforce-random-int-range x) (cond [(exact-positive-integer? y) ; min and max case
(enforce-random-int-range y) (enforce-random-int-range x)
(enforce-greater x y) (enforce-random-int-range y)
(+ x (random (- y x)))] (enforce-greater x y)
[(pseudo-random-generator? y) ; int and prng case (+ x (random (- y x)))]
(enforce-random-int-range x) [(pseudo-random-generator? y) ; int and prng case
(random x y)] (enforce-random-int-range x)
[else (random x y)]
(raise-argument-error [else
'random (raise-argument-error
"(or/c (integer-in 1 4294967087) pseudo-random-generator?)" 'random
y)])] "(or/c (integer-in 1 4294967087) pseudo-random-generator?)"
[(min max prng) ; three args: min, max, and prng y)])]
(enforce-random-int-range min) [(min max prng) ; three args: min, max, and prng
(enforce-random-int-range max) (enforce-random-int-range min)
(enforce-greater min max) (enforce-random-int-range max)
(unless (pseudo-random-generator? prng) (enforce-greater min max)
(raise-argument-error 'random "pseudo-random-generator?" prng)) (unless (pseudo-random-generator? prng)
(+ min (random (- max min) prng))])) (raise-argument-error 'random "pseudo-random-generator?" prng))
(+ min (random (- max min) prng))])))
(define-values (new:collection-path) (define-values (new:collection-path)
(let ([collection-path (new-lambda (collection (let ([collection-path (new-lambda (collection