Skip to content

Commit f3495ec

Browse files
authored
Merge pull request #595 from jprider63/master
Memory usage improvements in unescapeText. #593
2 parents f0a3f10 + caca25e commit f3495ec

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

pure/Data/Aeson/Parser/UnescapePure.hs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ decode UtfTail1 point word = case word of
119119
w | 0x80 <= w && w <= 0xbf -> (UtfGround, setByte1 point word)
120120
_ -> throwDecodeError
121121

122-
{-# INLINE decode #-}
123-
124122
decodeHex :: Word8 -> Word16
125123
decodeHex 48 = 0 -- '0'
126124
decodeHex 49 = 1 -- '1'
@@ -145,12 +143,12 @@ decodeHex 101 = 14 -- 'e'
145143
decodeHex 70 = 15 -- 'F'
146144
decodeHex 102 = 15 -- 'f'
147145
decodeHex _ = throwDecodeError
148-
{-# INLINE decodeHex #-}
149146

150147
unescapeText' :: ByteString -> Text
151148
unescapeText' bs = runText $ \done -> do
152149
dest <- A.new len
153-
(pos, finalState) <- B.foldl' (f' dest) (return (0, StateNone)) bs
150+
151+
(pos, finalState) <- loop dest (0, StateNone) 0
154152

155153
-- Check final state. Currently pos gets only increased over time, so this check should catch overflows.
156154
when ( finalState /= StateNone || pos > len)
@@ -172,11 +170,12 @@ unescapeText' bs = runText $ \done -> do
172170
(st', p) ->
173171
return (pos, StateUtf st' p)
174172

175-
{-# INLINE runUtf #-}
176-
177-
f' dest m c = m >>= \s -> f dest s c
178-
179-
{-# INLINE f' #-}
173+
loop :: A.MArray s -> (Int, State) -> Int -> ST s (Int, State)
174+
loop _ ps i | i >= len = return ps
175+
loop dest ps i = do
176+
let c = B.index bs i -- JP: We can use unsafe index once we prove bounds with Liquid Haskell.
177+
ps' <- f dest ps c
178+
loop dest ps' $ i+1
180179

181180
-- No pending state.
182181
f dest (pos, StateNone) c = runUtf dest pos UtfGround 0 c
@@ -253,8 +252,6 @@ unescapeText' bs = runText $ \done -> do
253252
else
254253
writeAndReturn dest pos u StateNone
255254

256-
{-# INLINE f #-}
257-
258255
write :: A.MArray s -> Int -> Word16 -> ST s ()
259256
write dest pos char =
260257
A.unsafeWrite dest pos char
@@ -270,8 +267,6 @@ throwDecodeError :: a
270267
throwDecodeError =
271268
let desc = "Data.Text.Internal.Encoding.decodeUtf8: Invalid UTF-8 stream" in
272269
throw (DecodeError desc Nothing)
273-
{-# INLINE throwDecodeError #-}
274270

275271
unescapeText :: ByteString -> Either UnicodeException Text
276272
unescapeText = unsafeDupablePerformIO . try . evaluate . unescapeText'
277-
{-# INLINE unescapeText #-}

0 commit comments

Comments
 (0)