summaryrefslogtreecommitdiff
path: root/src/System
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-07-03 00:17:16 +0400
committerSam T <pxqr.sta@gmail.com>2013-07-03 00:17:16 +0400
commit07a6123d56df03e5d7c40384c87f6c40ae2b5131 (patch)
treedaa508aaccfc5086dbef89bca5ac4a0bb9408ed8 /src/System
parent044672da343dd3139be0c8f95f1fbd45b7546d9c (diff)
~ 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.
Diffstat (limited to 'src/System')
-rw-r--r--src/System/Torrent/Storage.hs9
1 files changed, 4 insertions, 5 deletions
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 {..}
143-- let blkIx = undefined 143-- let blkIx = undefined
144-- bm <- readTVarIO blocks 144-- bm <- readTVarIO blocks
145-- unless (member blkIx bm) $ do 145-- unless (member blkIx bm) $ do
146 writeBytes (blkInterval (pieceLength session) blk) 146 writeBytes (blkInterval (pieceLength session) blk) blkData payload
147 (Lazy.fromChunks [blkData])
148 payload
149 147
150 markBlock blk st 148 markBlock blk st
151 validatePiece blkPiece st 149 validatePiece blkPiece st
@@ -166,7 +164,7 @@ getBlk ix @ BlockIx {..} st @ Storage {..}
166 = liftIO $ {-# SCC getBlk #-} do 164 = liftIO $ {-# SCC getBlk #-} do
167 -- TODO check if __piece__ is available 165 -- TODO check if __piece__ is available
168 bs <- readBytes (ixInterval (pieceLength session) ix) payload 166 bs <- readBytes (ixInterval (pieceLength session) ix) payload
169 return $ Block ixPiece ixOffset (Lazy.toStrict bs) 167 return $ Block ixPiece ixOffset bs
170 168
171getPiece :: PieceIx -> Storage -> IO ByteString 169getPiece :: PieceIx -> Storage -> IO ByteString
172getPiece pix st @ Storage {..} = {-# SCC getPiece #-} do 170getPiece pix st @ Storage {..} = {-# SCC getPiece #-} do
@@ -220,4 +218,5 @@ ixInterval pieceSize BlockIx {..} =
220 218
221blkInterval :: Int -> Block -> FixedInterval 219blkInterval :: Int -> Block -> FixedInterval
222blkInterval pieceSize Block {..} = 220blkInterval pieceSize Block {..} =
223 Fixed.interval (blkPiece * pieceSize + blkOffset) (B.length blkData) \ No newline at end of file 221 Fixed.interval (blkPiece * pieceSize + blkOffset)
222 (fromIntegral (Lazy.length blkData)) \ No newline at end of file