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