Skip to content

Commit

Permalink
Optimize Char#to_s when it's ascii and the IO has no encoding.
Browse files Browse the repository at this point in the history
This makes CSV and JSON parsing, and probably other parsers, significantly faster.
- For a CSV test, it goes down from 1.67s to 1.25s
- For a JSON test, it goes down from 3.93s to 3.38s
Ary Borenszweig committed Sep 10, 2016
1 parent 3aadab7 commit 6d867d9
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/char.cr
Original file line number Diff line number Diff line change
@@ -633,9 +633,15 @@ struct Char
#
# This appends this char's bytes as encoded by UTF-8 to the given `IO`.
def to_s(io : IO)
if ord <= 0x7f
if ascii?
byte = ord.to_u8
io.write_utf8 Slice.new(pointerof(byte), 1)

# Optimization: writing a slice is much slower than writing a byte
if io.@encoding
io.write_utf8 Slice.new(pointerof(byte), 1)
else
io.write_byte byte
end
else
chars = uninitialized UInt8[4]
i = 0

0 comments on commit 6d867d9

Please sign in to comment.