]> gitweb.factorcode.org Git - factor.git/commitdiff
tools.cat: significant performance improvement using binary.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 1 Aug 2018 19:52:33 +0000 (12:52 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 1 Aug 2018 19:52:33 +0000 (12:52 -0700)
Before (using strings): 77MiB/s
After (using byte-arrays): 3.06GiB/s

extra/tools/cat/cat.factor

index ea90e42a3821fe1bcecbdb116659b9ea9700f943..0798d757e03e68eaaf99eca2673455e8500cbe91 100644 (file)
@@ -1,16 +1,15 @@
 ! Copyright (C) 2010 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: command-line formatting kernel io io.encodings.binary
-io.files namespaces sequences strings ;
+USING: accessors command-line formatting fry io io.encodings
+io.encodings.binary io.files kernel namespaces sequences ;
 
 IN: tools.cat
 
-: cat-lines ( -- )
-    [ print flush ] each-line ;
-
 : cat-stream ( -- )
-    [ >string write flush ] each-block ;
+    input-stream get dup decoder? [ stream>> ] when
+    output-stream get dup encoder? [ stream>> ] when
+    '[ _ stream-write ] each-stream-block ;
 
 : cat-file ( path -- )
     dup exists? [
@@ -18,9 +17,9 @@ IN: tools.cat
     ] [ "%s: not found\n" printf flush ] if ;
 
 : cat-files ( paths -- )
-    [ dup "-" = [ drop cat-lines ] [ cat-file ] if ] each ;
+    [ dup "-" = [ drop cat-stream ] [ cat-file ] if ] each ;
 
 : run-cat ( -- )
-    command-line get [ cat-lines ] [ cat-files ] if-empty ;
+    command-line get [ cat-stream ] [ cat-files ] if-empty ;
 
 MAIN: run-cat