Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellwrosen committed Oct 1, 2023
1 parent 7428d8e commit 96eccad
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/TimerWheel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ module TimerWheel
where

import qualified Data.Atomics as Atomics
import Data.Foldable (traverse_)
import Data.Primitive.Array (MutableArray)
import qualified Data.Primitive.Array as Array
import GHC.Base (RealWorld)
Expand Down Expand Up @@ -337,7 +336,7 @@ timerBucketInsert timestamp timer =
-- Fire every timer in a bucket, in order
timerBucketFire :: Counter -> TimerBucket -> IO ()
timerBucketFire numTimers =
traverse_ \case
TimestampMap.foreach \_ -> \case
Timers1 timer -> timerActionThenDecr timer
TimersN timers -> foldr f (pure ()) timers
where
Expand All @@ -346,6 +345,7 @@ timerBucketFire numTimers =
acc
timerActionThenDecr timer

timerActionThenDecr :: Timer -> IO ()
timerActionThenDecr timer = do
timerAction timer
decrCounter_ numTimers
Expand Down
2 changes: 1 addition & 1 deletion src/TimerWheel/Internal/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ where
import Control.Monad as X (when)
import Data.Coerce as X (coerce)
import Data.Fixed (E6, Fixed)
import Data.IORef as X (newIORef, readIORef, writeIORef)
import Data.IORef as X (IORef, newIORef, readIORef, writeIORef)
import Data.Map as X (Map)
import Data.Word as X (Word64)
import GHC.Generics as X (Generic)
Expand Down
22 changes: 10 additions & 12 deletions src/TimerWheel/Internal/TimestampMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module TimerWheel.Internal.TimestampMap
( TimestampMap,
delete,
empty,
foreach,
insert,
lookup,
null,
Expand Down Expand Up @@ -31,18 +32,6 @@ type Prefix = Word64

type Mask = Word64

instance Foldable TimestampMap where
-- Use lambda t to be inlinable with two arguments only.
foldr f z = \case
Bin _ _ l r -> go (go z r) l
t -> go z t
where
go acc = \case
Nil -> acc
Tip _ x -> f x acc
Bin _ _ l r -> go (go acc r) l
{-# INLINE foldr #-}

bin :: Prefix -> Mask -> TimestampMap a -> TimestampMap a -> TimestampMap a
bin _ _ l Nil = l
bin _ _ Nil r = r
Expand Down Expand Up @@ -79,6 +68,15 @@ empty =
Nil
{-# INLINE empty #-}

foreach :: (Timestamp -> a -> IO ()) -> TimestampMap a -> IO ()
foreach f =
go
where
go = \case
Nil -> pure ()
Tip k x -> f (coerce @Word64 @Timestamp k) x
Bin _ _ l r -> go l >> go r

insert :: forall a. Timestamp -> a -> TimestampMap a -> TimestampMap a
insert = coerce @(Word64 -> a -> TimestampMap a -> TimestampMap a) insert_
{-# INLINE insert #-}
Expand Down

0 comments on commit 96eccad

Please sign in to comment.