]> gitweb.factorcode.org Git - factor.git/commitdiff
io:read: don't resize output buffer if unnecessary
authorJoe Groff <arcata@gmail.com>
Wed, 9 Nov 2011 22:29:12 +0000 (14:29 -0800)
committerJoe Groff <arcata@gmail.com>
Wed, 9 Nov 2011 22:29:12 +0000 (14:29 -0800)
Even with a short-circuit test in the VM primitives, resize still has an overly high runtime cost in tight loops. For #376.

core/io/io.factor

index 8e744936472cbf4993fd01d89ce8e35e20d6eb19..8eb16b7653dd535fcb3b6c1ef5d9725445bae862 100644 (file)
@@ -119,9 +119,13 @@ SYMBOL: error-stream
 : (new-sequence-for-stream) ( n stream -- seq )
     stream-exemplar new-sequence ; inline
 
+: resize-if-necessary ( wanted-n got-n seq -- seq' )
+    2over = [ [ 2drop ] dip ] [ resize nip ] if ; inline
+
 : (read-into-new) ( n stream quot -- seq/f )
+    [ dup ] 2dip
     [ 2dup (new-sequence-for-stream) swap ] dip curry keep
-    over 0 = [ 2drop f ] [ resize ] if ; inline
+    over 0 = [ 3drop f ] [ resize-if-necessary ] if ; inline
 
 : (read-into) ( buf stream quot -- buf-slice/f )
     [ dup length over ] 2dip call