Skip to content

Commit

Permalink
Merge branch 'minify-histo' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
jlouis committed May 24, 2010
2 parents 8525df4 + 956aadf commit 0f6c19f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Data/PendingSet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Data.PendingSet
( PendingSet
, Data.PendingSet.empty
, Data.PendingSet.size
, remove
, have
, unhave
, haves
Expand Down Expand Up @@ -33,10 +34,14 @@ have pn = PendingSet . alter f pn . unPS
-- | A Peer does not have a given piece anymore (TODO: Not used in practice)
unhave :: PieceNum -> PendingSet -> PendingSet
unhave pn = PendingSet . alter f pn . unPS
where f Nothing = error "Data.PendingSet.unhave"
where f Nothing = Nothing
f (Just 1) = Nothing
f (Just x) = Just (x-1)

-- | Remove a piece from the histogram. Used when it completes
remove :: PieceNum -> PendingSet -> PendingSet
remove pn = PendingSet . delete pn . unPS

-- | Add all pieces in a bitfield
haves :: [PieceNum] -> PendingSet -> PendingSet
haves pns = flip (foldl f) pns
Expand Down
10 changes: 7 additions & 3 deletions src/Process/PieceMgr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ peerHave idxs tmv = do
inp <- gets inProgress
interesting <- filterM (mem ps inp) idxs
liftIO . atomically $ putTMVar tmv interesting
modify (\db -> db { histogram = PendS.haves idxs (histogram db)})
if null interesting
then return ()
else modify (\db -> db { histogram = PendS.haves interesting (histogram db)})
where mem ps inp p = do
q <- PS.member p ps
if q
Expand All @@ -234,7 +236,8 @@ peerHave idxs tmv = do


peerUnhave :: [PieceNum] -> Process CF ST ()
peerUnhave idxs = modify (\db -> db { histogram = PendS.unhaves idxs (histogram db)})
peerUnhave idxs =
modify (\db -> db { histogram = PendS.unhaves idxs (histogram db)})

endgameBroadcast :: PieceNum -> Block -> Process CF ST ()
endgameBroadcast pn blk = {-# SCC "endgameBroadCast" #-} do
Expand Down Expand Up @@ -275,7 +278,8 @@ createPieceDb mmap pmap = do
completePiece :: PieceNum -> PieceMgrProcess ()
completePiece pn = do
PS.insert pn =<< gets donePiece
modify (\db -> db { inProgress = M.delete pn (inProgress db) })
modify (\db -> db { inProgress = M.delete pn (inProgress db),
histogram = PendS.remove pn (histogram db )})

-- | Handle torrent completion
checkFullCompletion :: PieceMgrProcess ()
Expand Down

0 comments on commit 0f6c19f

Please sign in to comment.