on master help sign in
Try to build a Vector a little more efficiently
09b2ddcBryan O'Sullivan
Data/Aeson/Parser/Internal.hs+5−5
@@ -40,7 +40,7 @@ import Data.Text (Text)
40import Data.Text.Encoding (decodeUtf8')
40import Data.Text.Encoding (decodeUtf8')
41import Data.Text.Internal.Encoding.Utf8 (ord2, ord3, ord4)
41import Data.Text.Internal.Encoding.Utf8 (ord2, ord3, ord4)
42import Data.Text.Internal.Unsafe.Char (ord)
42import Data.Text.Internal.Unsafe.Char (ord)
43−import Data.Vector as Vector (Vector, empty, fromList)
43+import Data.Vector as Vector (Vector, empty, fromList, reverse)
44import Data.Word (Word8)
44import Data.Word (Word8)
45import Foreign.ForeignPtr (withForeignPtr)
45import Foreign.ForeignPtr (withForeignPtr)
46import Foreign.Ptr (minusPtr)
46import Foreign.Ptr (minusPtr)
@@ -142,14 +142,14 @@ arrayValues val = do
142 w <- A.peekWord8'
142 w <- A.peekWord8'
143 if w == CLOSE_SQUARE
143 if w == CLOSE_SQUARE
144 then A.anyWord8 >> return Vector.empty
144 then A.anyWord8 >> return Vector.empty
145− else Vector.fromList <$> loop
145+ else loop []
146 where
146 where
147− loop = do
147+ loop acc = do
148 v <- val <* skipSpace
148 v <- val <* skipSpace
149 ch <- A.satisfy $ \w -> w == COMMA || w == CLOSE_SQUARE
149 ch <- A.satisfy $ \w -> w == COMMA || w == CLOSE_SQUARE
150 if ch == COMMA
150 if ch == COMMA
151− then skipSpace >> (v:) <$> loop
151+ then skipSpace >> loop (v:acc)
152− else return [v]
152+ else return (Vector.reverse (Vector.fromList (v:acc)))
153{-# INLINE arrayValues #-}
153{-# INLINE arrayValues #-}
154
154
155-- | Parse any JSON value. You should usually 'json' in preference to
155-- | Parse any JSON value. You should usually 'json' in preference to