]> gitweb.factorcode.org Git - factor.git/commitdiff
random: go faster
authorJoe Groff <arcata@gmail.com>
Wed, 16 Nov 2011 04:45:16 +0000 (20:45 -0800)
committerJoe Groff <arcata@gmail.com>
Wed, 16 Nov 2011 04:45:16 +0000 (20:45 -0800)
* Use int <ref> instead of 4 >le
* Inline sequences:push-all
* Types, hints, inlines
Re: #328

basis/random/random.factor
basis/random/unix/unix.factor
core/sequences/sequences.factor

index e9f4b74c9266db213a5277d1880d301c472d7d4e..cba194b399cdca3f9d90b5e378b66c6e0bbd798d 100644 (file)
@@ -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 )
     [ [ <byte-vector> ] keep 4 /mod ] dip
-    [ pick '[ _ random-32* 4 >le _ push-all ] times ]
+    [ pick '[ _ random-32* int <ref> _ push-all ] times ]
     [
         over zero?
-        [ 2drop ] [ random-32* 4 >le swap head append! ] if
-    ] bi-curry bi* ;
+        [ 2drop ] [ random-32* int <ref> 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
 
 <PRIVATE
 
index f06f029769422831ed945e6c60fb59ada0bfb304..d52458372e4696f13f85430ca60822c19b09ff2a 100644 (file)
@@ -1,10 +1,11 @@
 ! Copyright (C) 2008 Doug Coleman
 ! See http://factorcode.org/license.txt for BSD license.
 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
 
-TUPLE: unix-random reader ;
+TUPLE: unix-random { reader input-port } ;
 
 : <unix-random> ( path -- random )
     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 )
     reader>> stream-read ;
+HINTS: M\ unix-random random-bytes* { fixnum unix-random } ;
 
 [
     "/dev/random" <unix-random> &dispose secure-random-generator set-global
index ad2a5ea48282ad3a790568a910afa97f870323cc..5ab2579b1a41ca701ed643ce5e91da8b8690680d 100644 (file)
@@ -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
 
 <PRIVATE