From 07a6123d56df03e5d7c40384c87f6c40ae2b5131 Mon Sep 17 00:00:00 2001 From: Sam T Date: Wed, 3 Jul 2013 00:17:16 +0400 Subject: ~ Use lazy bytestring This lead to the following consequences: * we could efficiently read from storage - if block intersects files boundaries then we will "view" the block in the two different bytestrings. To avoid concat we now return lazy bytestring; * we could read block from socket without "concat" - again, for the same reason. The pitfail is that now we have a bit more heap object, but blocks lifetime is very short and this shouldnt play the big difference. The lifetime is either (socket -> storage -> unreachable) or (storage -> socket -> unreachable) unless a lib user keep block for their own purposes. --- src/System/Torrent/Storage.hs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/System/Torrent') diff --git a/src/System/Torrent/Storage.hs b/src/System/Torrent/Storage.hs index 955c1746..363f94ef 100644 --- a/src/System/Torrent/Storage.hs +++ b/src/System/Torrent/Storage.hs @@ -143,9 +143,7 @@ putBlk blk @ Block {..} st @ Storage {..} -- let blkIx = undefined -- bm <- readTVarIO blocks -- unless (member blkIx bm) $ do - writeBytes (blkInterval (pieceLength session) blk) - (Lazy.fromChunks [blkData]) - payload + writeBytes (blkInterval (pieceLength session) blk) blkData payload markBlock blk st validatePiece blkPiece st @@ -166,7 +164,7 @@ getBlk ix @ BlockIx {..} st @ Storage {..} = liftIO $ {-# SCC getBlk #-} do -- TODO check if __piece__ is available bs <- readBytes (ixInterval (pieceLength session) ix) payload - return $ Block ixPiece ixOffset (Lazy.toStrict bs) + return $ Block ixPiece ixOffset bs getPiece :: PieceIx -> Storage -> IO ByteString getPiece pix st @ Storage {..} = {-# SCC getPiece #-} do @@ -220,4 +218,5 @@ ixInterval pieceSize BlockIx {..} = blkInterval :: Int -> Block -> FixedInterval blkInterval pieceSize Block {..} = - Fixed.interval (blkPiece * pieceSize + blkOffset) (B.length blkData) \ No newline at end of file + Fixed.interval (blkPiece * pieceSize + blkOffset) + (fromIntegral (Lazy.length blkData)) \ No newline at end of file -- cgit v1.2.3