@@ -100,19 +100,17 @@ streamUtf8 onErr bs0 = Stream next (T bs0 S0 0) unknownSize
100unstreamChunks :: Int -> Stream Word8 -> ByteString
100unstreamChunks :: Int -> Stream Word8 -> ByteString
101unstreamChunks chunkSize (Stream next s0 len0) = chunk s0 (upperBound 4 len0)
101unstreamChunks chunkSize (Stream next s0 len0) = chunk s0 (upperBound 4 len0)
102 where chunk s1 len1 = unsafePerformIO $ do
102 where chunk s1 len1 = unsafePerformIO $ do
103− let len = min len1 chunkSize
103+ let len = max 4 (min len1 chunkSize)
104 mallocByteString len >>= loop len 0 s1
104 mallocByteString len >>= loop len 0 s1
106 loop !n !off !s fp = case next s of
106 loop !n !off !s fp = case next s of
107 Done | off == 0 -> return Empty
107 Done | off == 0 -> return Empty
110− return $! Chunk bs Empty
108+ | otherwise -> return $! Chunk (trimUp fp off) Empty
111 Skip s' -> loop n off s' fp
109 Skip s' -> loop n off s' fp
112 Yield x s'
110 Yield x s'
113 | off == chunkSize -> do
111 | off == chunkSize -> do
114− bs <- trimUp fp off
112+ let !newLen = n - off
115− return (Chunk bs (chunk s (n - B.length bs)))
113+ return $! Chunk (trimUp fp off) (chunk s newLen)
116 | off == n -> realloc fp n off s' x
114 | off == n -> realloc fp n off s' x
117 | otherwise -> do
115 | otherwise -> do
118 withForeignPtr fp $ \p -> pokeByteOff p off x
116 withForeignPtr fp $ \p -> pokeByteOff p off x
@@ -123,8 +121,7 @@ unstreamChunks chunkSize (Stream next s0 len0) = chunk s0 (upperBound 4 len0)
123 fp' <- copy0 fp n n'
121 fp' <- copy0 fp n n'
124 withForeignPtr fp' $ \p -> pokeByteOff p off x
122 withForeignPtr fp' $ \p -> pokeByteOff p off x
125 loop n' (off+1) s fp'
123 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
128 copy0 :: ForeignPtr Word8 -> Int -> Int -> IO (ForeignPtr Word8)
125 copy0 :: ForeignPtr Word8 -> Int -> Int -> IO (ForeignPtr Word8)
129 copy0 !src !srcLen !destLen = assert (srcLen <= destLen) $ do
126 copy0 !src !srcLen !destLen = assert (srcLen <= destLen) $ do
130 dest <- mallocByteString destLen
127 dest <- mallocByteString destLen