Skip to content

Commit 9985e30

Browse files
authored
Merge pull request #805 from ethercrow/decode-ascii-faster
Use Text.Encoding.decodeLatin1 for decoding ASCII strings
2 parents d0fd406 + 0c21a25 commit 9985e30

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Data/Aeson/Parser/Internal.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,20 @@ jstring_ = do
322322
-- not sure whether >= or bit hackery is faster
323323
-- perfectly, we shouldn't care, it's compiler job.
324324
s <- A.takeWhile (\w -> w /= DOUBLE_QUOTE && w /= BACKSLASH && w >= 0x20 && w < 0x80)
325-
let txt = TE.decodeUtf8 s
325+
let txt = unsafeDecodeASCII s
326326
mw <- A.peekWord8
327327
case mw of
328328
Nothing -> fail "string without end"
329329
Just DOUBLE_QUOTE -> A.anyWord8 $> txt
330330
Just w | w < 0x20 -> fail "unescaped control character"
331331
_ -> jstringSlow s
332332

333+
-- | The input is assumed to contain only 7bit ASCII characters (i.e. @< 0x80@).
334+
-- We use TE.decodeLatin1 here because TE.decodeASCII is currently (text-1.2.4.0)
335+
-- deprecated and equal to TE.decodeUtf8, which is slower than TE.decodeLatin1.
336+
unsafeDecodeASCII :: B.ByteString -> Text
337+
unsafeDecodeASCII = TE.decodeLatin1
338+
333339
jstringSlow :: B.ByteString -> Parser Text
334340
{-# INLINE jstringSlow #-}
335341
jstringSlow s' = {-# SCC "jstringSlow" #-} do

0 commit comments

Comments
 (0)