From 1d4660c7ec64f61952576a40260fd8b70b3bb7ee Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Nov 2011 20:45:16 -0800 Subject: [PATCH] random: go faster * Use int instead of 4 >le * Inline sequences:push-all * Types, hints, inlines Re: #328 --- basis/random/random.factor | 14 ++++++++------ basis/random/unix/unix.factor | 6 ++++-- core/sequences/sequences.factor | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/basis/random/random.factor b/basis/random/random.factor index e9f4b74c92..cba194b399 100644 --- a/basis/random/random.factor +++ b/basis/random/random.factor @@ -4,7 +4,7 @@ USING: accessors alien.c-types alien.data arrays assocs byte-arrays byte-vectors combinators fry io.backend io.binary kernel locals math math.bitwise math.constants math.functions math.order math.ranges namespaces sequences sequences.private -sets summary system vocabs ; +sets summary system vocabs hints typed ; IN: random SYMBOL: system-random-generator @@ -17,11 +17,13 @@ GENERIC: random-bytes* ( n tuple -- byte-array ) M: object random-bytes* ( n tuple -- byte-array ) [ [ ] keep 4 /mod ] dip - [ pick '[ _ random-32* 4 >le _ push-all ] times ] + [ pick '[ _ random-32* int _ push-all ] times ] [ over zero? - [ 2drop ] [ random-32* 4 >le swap head append! ] if - ] bi-curry bi* ; + [ 2drop ] [ random-32* int swap head append! ] if + ] bi-curry bi* B{ } like ; + +HINTS: M\ object random-bytes* { fixnum object } ; M: object random-32* ( tuple -- r ) 4 swap random-bytes* be> ; @@ -36,8 +38,8 @@ M: f random-32* ( obj -- * ) no-random-number-generator ; : random-32 ( -- n ) random-generator get random-32* ; -: random-bytes ( n -- byte-array ) - random-generator get random-bytes* ; +TYPED: random-bytes ( n: fixnum -- byte-array: byte-array ) + random-generator get random-bytes* ; inline ( path -- random ) binary unix-random boa ; @@ -13,6 +14,7 @@ M: unix-random dispose reader>> dispose ; M: unix-random random-bytes* ( n tuple -- byte-array ) reader>> stream-read ; +HINTS: M\ unix-random random-bytes* { fixnum unix-random } ; [ "/dev/random" &dispose secure-random-generator set-global diff --git a/core/sequences/sequences.factor b/core/sequences/sequences.factor index ad2a5ea482..5ab2579b1a 100644 --- a/core/sequences/sequences.factor +++ b/core/sequences/sequences.factor @@ -329,7 +329,7 @@ M: sequence clone-like M: immutable-sequence clone-like like ; inline -: push-all ( src dest -- ) [ length ] [ copy ] bi ; +: push-all ( src dest -- ) [ length ] [ copy ] bi ; inline