random: go faster

* Use int <ref> instead of 4 >le
* Inline sequences:push-all
* Types, hints, inlines
Re: #328
db4
Joe Groff 2011-11-15 20:45:16 -08:00
parent 090dc2f410
commit 1d4660c7ec
3 changed files with 13 additions and 9 deletions

View File

@ -4,7 +4,7 @@ USING: accessors alien.c-types alien.data arrays assocs
byte-arrays byte-vectors combinators fry io.backend io.binary byte-arrays byte-vectors combinators fry io.backend io.binary
kernel locals math math.bitwise math.constants math.functions kernel locals math math.bitwise math.constants math.functions
math.order math.ranges namespaces sequences sequences.private math.order math.ranges namespaces sequences sequences.private
sets summary system vocabs ; sets summary system vocabs hints typed ;
IN: random IN: random
SYMBOL: system-random-generator SYMBOL: system-random-generator
@ -17,11 +17,13 @@ GENERIC: random-bytes* ( n tuple -- byte-array )
M: object random-bytes* ( n tuple -- byte-array ) M: object random-bytes* ( n tuple -- byte-array )
[ [ <byte-vector> ] keep 4 /mod ] dip [ [ <byte-vector> ] keep 4 /mod ] dip
[ pick '[ _ random-32* 4 >le _ push-all ] times ] [ pick '[ _ random-32* int <ref> _ push-all ] times ]
[ [
over zero? over zero?
[ 2drop ] [ random-32* 4 >le swap head append! ] if [ 2drop ] [ random-32* int <ref> swap head append! ] if
] bi-curry bi* ; ] bi-curry bi* B{ } like ;
HINTS: M\ object random-bytes* { fixnum object } ;
M: object random-32* ( tuple -- r ) 4 swap random-bytes* be> ; 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-32 ( -- n ) random-generator get random-32* ;
: random-bytes ( n -- byte-array ) TYPED: random-bytes ( n: fixnum -- byte-array: byte-array )
random-generator get random-bytes* ; random-generator get random-bytes* ; inline
<PRIVATE <PRIVATE

View File

@ -1,10 +1,11 @@
! Copyright (C) 2008 Doug Coleman ! Copyright (C) 2008 Doug Coleman
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: alien.c-types io io.files kernel namespaces random USING: alien.c-types io io.files kernel namespaces random
io.encodings.binary init accessors system destructors ; io.encodings.binary init accessors system destructors
io.ports hints math ;
IN: random.unix IN: random.unix
TUPLE: unix-random reader ; TUPLE: unix-random { reader input-port } ;
: <unix-random> ( path -- random ) : <unix-random> ( path -- random )
binary <file-reader> unix-random boa ; binary <file-reader> unix-random boa ;
@ -13,6 +14,7 @@ M: unix-random dispose reader>> dispose ;
M: unix-random random-bytes* ( n tuple -- byte-array ) M: unix-random random-bytes* ( n tuple -- byte-array )
reader>> stream-read ; reader>> stream-read ;
HINTS: M\ unix-random random-bytes* { fixnum unix-random } ;
[ [
"/dev/random" <unix-random> &dispose secure-random-generator set-global "/dev/random" <unix-random> &dispose secure-random-generator set-global

View File

@ -329,7 +329,7 @@ M: sequence clone-like
M: immutable-sequence clone-like like ; inline M: immutable-sequence clone-like like ; inline
: push-all ( src dest -- ) [ length ] [ copy ] bi ; : push-all ( src dest -- ) [ length ] [ copy ] bi ; inline
<PRIVATE <PRIVATE