@@ -100,19 +100,17 @@ streamUtf8 onErr bs0 = Stream next (T bs0 S0 0) unknownSize
100100unstreamChunks :: Int -> Stream Word8 -> ByteString
101101unstreamChunks chunkSize (Stream next s0 len0) = chunk s0 (upperBound 4 len0)
102102 where chunk s1 len1 = unsafePerformIO $ do
103− let len = min len1 chunkSize
103+ let len = max 4 (min len1 chunkSize)
104104 mallocByteString len >>= loop len 0 s1
105105 where
106106 loop !n !off !s fp = case next s of
107107 Done | off == 0 -> return Empty
108− | otherwise -> do
109− bs <- trimUp fp off
110− return $! Chunk bs Empty
108+ | otherwise -> return $! Chunk (trimUp fp off) Empty
111109 Skip s' -> loop n off s' fp
112110 Yield x s'
113111 | off == chunkSize -> do
114− bs <- trimUp fp off
115− return (Chunk bs (chunk s (n - B.length bs)))
112+ let !newLen = n - off
113+ return $! Chunk (trimUp fp off) (chunk s newLen)
116114 | off == n -> realloc fp n off s' x
117115 | otherwise -> do
118116 withForeignPtr fp $ \p -> pokeByteOff p off x
@@ -123,8 +121,7 @@ unstreamChunks chunkSize (Stream next s0 len0) = chunk s0 (upperBound 4 len0)
123121 fp' <- copy0 fp n n'
124122 withForeignPtr fp' $ \p -> pokeByteOff p off x
125123 loop n' (off+1) s fp'
126− {-# NOINLINE trimUp #-}
127− trimUp fp off = return $! B.PS fp 0 off
124+ trimUp fp off = B.PS fp 0 off
128125 copy0 :: ForeignPtr Word8 -> Int -> Int -> IO (ForeignPtr Word8)
129126 copy0 !src !srcLen !destLen = assert (srcLen <= destLen) $ do
130127 dest <- mallocByteString destLen