Skip to content

Commit

Permalink
another IntSet replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Mar 4, 2024
1 parent 3f98a7a commit bd968b9
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/swarm-engine/Swarm/Game/State/Robot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import Data.List.NonEmpty qualified as NE
import Data.Map (Map)
import Data.Map qualified as M
import Data.Maybe (fromMaybe, mapMaybe)
import Data.Set qualified as S
import Data.Tuple (swap)
import GHC.Generics (Generic)
import Swarm.Game.CESK (CESK (Waiting))
Expand Down Expand Up @@ -312,8 +311,10 @@ wakeUpRobotsDoneSleeping time = do
case maybeWakeableRIDs of
Nothing -> return mempty
Just wakeableRIDs -> do

robots <- use robotMap
let robotIdSet = IM.keysSet robots

wakeableRIDsSet = IS.fromList wakeableRIDs

-- Limit ourselves to the robots that have not expired in their sleep
Expand Down Expand Up @@ -361,8 +362,8 @@ wakeWatchingRobots myID currentTick loc = do
wakeTimes :: [(RID, TickNumber)]
wakeTimes = mapMaybe (sequenceA . (view robotID &&& waitingUntil)) botsWatchingThisLoc

wakeTimesToPurge :: Map TickNumber (S.Set RID)
wakeTimesToPurge = M.fromListWith (<>) $ map (fmap S.singleton . swap) wakeTimes
wakeTimesToPurge :: Map TickNumber IntSet
wakeTimesToPurge = M.fromListWith (<>) $ map (fmap IS.singleton . swap) wakeTimes

-- Step 3: Take these robots out of their time-indexed slot in "waitingRobots".
-- To preserve performance, this should be done without iterating over the
Expand All @@ -373,7 +374,7 @@ wakeWatchingRobots myID currentTick loc = do
-- But we shall not worry about cleaning those up here;
-- they will be "garbage collected" as a matter of course
-- when their tick comes up in "wakeUpRobotsDoneSleeping".
f (k, botsToRemove) = M.adjust (filter (`S.notMember` botsToRemove)) k
f (k, botsToRemove) = M.adjust (filter (`IS.notMember` botsToRemove)) k

-- Step 4: Re-add the watching bots to be awakened ASAP:
wakeableBotIds = map fst wakeTimes
Expand Down

0 comments on commit bd968b9

Please sign in to comment.