don't generate 4x the number of random bytes we actually need. map concat is slower than push-all

db4
Doug Coleman 2008-11-12 12:40:09 -06:00
parent 6cc08f76dd
commit b45c03877b
1 changed files with 13 additions and 5 deletions

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: alien.c-types kernel math namespaces sequences USING: alien.c-types kernel math namespaces sequences
io.backend io.binary combinators system vocabs.loader io.backend io.binary combinators system vocabs.loader
summary math.bitwise ; summary math.bitwise byte-vectors fry byte-arrays ;
IN: random IN: random
SYMBOL: system-random-generator SYMBOL: system-random-generator
@ -13,8 +13,17 @@ GENERIC: seed-random ( tuple seed -- )
GENERIC: random-32* ( tuple -- r ) GENERIC: random-32* ( tuple -- r )
GENERIC: random-bytes* ( n tuple -- byte-array ) GENERIC: random-bytes* ( n tuple -- byte-array )
<PRIVATE
: adjust-random ( n m -- n' )
3 mask 0 = [ 1+ ] unless ; inline
PRIVATE>
M: object random-bytes* ( n tuple -- byte-array ) M: object random-bytes* ( n tuple -- byte-array )
[ random-32* ] curry replicate [ 4 >le ] map concat ; [ [ 4 /i ] keep adjust-random ] dip
over 4 * <byte-vector>
[ '[ _ random-32* 4 >le _ push-all ] times ] keep ;
M: object random-32* ( tuple -- r ) 4 random-bytes* le> ; M: object random-32* ( tuple -- r ) 4 random-bytes* le> ;
@ -29,9 +38,8 @@ M: f random-32* ( obj -- * ) no-random-number-generator ;
: random-bytes ( n -- byte-array ) : random-bytes ( n -- byte-array )
[ [
dup 3 mask zero? [ 1+ ] unless dup adjust-random random-generator get random-bytes*
random-generator get random-bytes* ] keep head >byte-array ;
] keep head ;
<PRIVATE <PRIVATE