io.encodings.string: faster decode and encode

stream-contents is apparently way slow for decoders. Write decode out more directly as a read1/push loop so it's faster. encode isn't quite as bad, but we can still get a 25% speed improvement by writing to an appropriately sized byte-vector.
db4
Joe Groff 2011-10-12 21:12:57 -07:00
parent ffacdaf0da
commit 5c945595ee
1 changed files with 11 additions and 5 deletions

View File

@ -1,10 +1,16 @@
! Copyright (C) 2008 Daniel Ehrenberg. ! Copyright (C) 2008 Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: io io.streams.byte-array ; USING: byte-vectors io io.encodings io.streams.byte-array
io.streams.string kernel locals sbufs sequences ;
IN: io.encodings.string IN: io.encodings.string
: decode ( byte-array encoding -- string ) :: decode ( byte-array encoding -- string )
<byte-reader> stream-contents ; byte-array encoding <byte-reader> :> reader
byte-array length encoding guess-decoded-length <sbuf> :> buf
[ reader stream-read1 dup ] [ buf push ] while drop
buf "" like ; inline
: encode ( string encoding -- byte-array ) :: encode ( string encoding -- byte-array )
[ write ] with-byte-writer ; string length encoding guess-encoded-length <byte-vector> :> vec
string vec encoding <encoder> stream-write
vec B{ } like ; inline