on master help sign in
Improve list serialization by implicitly calculating its length
f744ebbAdithya Kumar
core/src/Streamly/Internal/Data/Serialize/Type.hs+6−7
@@ -23,7 +23,6 @@ module Streamly.Internal.Data.Serialize.Type
23import Control.Exception (assert)
23import Control.Exception (assert)
24#endif
24#endif
25
25
26−import Control.Monad (void)
27import Data.List (foldl')
26import Data.List (foldl')
28import Data.Proxy (Proxy (..))
27import Data.Proxy (Proxy (..))
29import Streamly.Internal.Data.Unbox
28import Streamly.Internal.Data.Unbox
@@ -240,13 +239,13 @@ instance forall a. Serialize a => Serialize [a] where
240 -- Inlining this causes large compilation times for tests
239 -- Inlining this causes large compilation times for tests
241 {-# INLINABLE serialize #-}
240 {-# INLINABLE serialize #-}
242 serialize off arr val = do
241 serialize off arr val = do
243− void $ serialize off arr ((fromIntegral :: Int -> Int64) (length val))
244− let off1 = off + Unbox.sizeOf (Proxy :: Proxy Int)
242+ let off1 = off + Unbox.sizeOf (Proxy :: Proxy Int64)
245− let pokeList o [] = pure o
243+ let pokeList acc o [] =
244+ Unbox.pokeByteIndex off arr (acc :: Int64) >> pure o
246− pokeList o (x:xs) = do
245+ pokeList acc o (x:xs) = do
247 o1 <- serialize o arr x
246 o1 <- serialize o arr x
248− pokeList o1 xs
247+ pokeList (acc + 1) o1 xs
249− pokeList off1 val
248+ pokeList 0 off1 val
250
249
251instance Serialize (Array a) where
250instance Serialize (Array a) where
252 {-# INLINE size #-}
251 {-# INLINE size #-}