on master help sign in
Deserialize: Construct the list more efficiently
commitbf3e775de3c1f9b17905ccff7d133a3a261ab2b8 authorHarendra Kumar <harendra@composewell.com> dateSun 20 Aug 2023, 09:55 (3 years ago) parent7c81bfb
1 file changed+11−4
core/src/Streamly/Internal/Data/Serialize.hs+11−4
core/src/Streamly/Internal/Data/Serialize.hs+11−4
@@ -196,11 +196,18 @@ instance forall a. Serialize a => Serialize [a] where
196196 deserialize off arr = do
197197 len <- Unbox.peekByteIndex off arr :: IO Int
198198 let off1 = off + Unbox.sizeOf (Proxy :: Proxy Int)
199− let peekList buf o 0 = pure (o, reverse buf)
200− peekList buf o i = do
199+ let
200+ peekList f o i | i >= 3 = do
201+ -- Unfold the loop three times
202+ (o1, x1) <- deserialize o arr
203+ (o2, x2) <- deserialize o1 arr
204+ (o3, x3) <- deserialize o2 arr
205+ peekList (f . (\xs -> x1:x2:x3:xs)) o3 (i - 3)
206+ peekList f o 0 = pure (o, f [])
207+ peekList f o i = do
201208 (o1, x) <- deserialize o arr
202− peekList (x:buf) o1 (i - 1)
203− peekList [] off1 len
209+ peekList (f . (x:)) o1 (i - 1)
210+ peekList id off1 len
204211
205212 {-# INLINE serialize #-}
206213 serialize off arr val = do