Skip to content

Commit c011c8f

Browse files
authored
Arm timeout only if necessary (haskell-distributed#490)
1 parent e606d40 commit c011c8f

4 files changed

Lines changed: 100 additions & 45 deletions

File tree

‎packages/distributed-process/ChangeLog‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ Unreleased
22

33
* Added support for `containers-0.8`.
44
* Reworked benchmarks, which can now be run using `cabal bench distributed-process`.
5+
* `receiveTimeout`, `expectTimeout` and `receiveChanTimeout` now arm the
6+
system timer only once they are about to block. This results in up to a 9x speedup
7+
in cases where a process receives lots of messages (in which case, a timeout isn't necessary).
8+
59

610
2025-02-04 Laurent P. René de Cotret <laurent.decotret@outlook.com> 0.7.8
711

‎packages/distributed-process/benchmarks/Main.hs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ benchmarks fx =
156156
messaging fx,
157157
channels fx,
158158
receiving fx,
159+
timeouts fx,
159160
messages fx,
160161
processes fx,
161162
monitoring fx,
@@ -301,6 +302,47 @@ receiving fx =
301302
selfSend :: (Serializable a) => a -> Process ()
302303
selfSend x = getSelfPid >>= \us -> unsafeSend us x
303304

305+
timeouts :: Fixture -> Benchmark
306+
timeouts fx =
307+
bgroup
308+
"timeouts"
309+
[ repsBench fx "expectTimeout 0 (message waiting)" 1000 $ do
310+
leaveInMailbox
311+
void (expectTimeout 0 :: Process (Maybe Int)),
312+
repsBench fx "expectTimeout 1s (message waiting)" 1000 $ do
313+
leaveInMailbox
314+
void (expectTimeout 1000000 :: Process (Maybe Int)),
315+
repsBench fx "receiveTimeout 0 (matchSTM ready)" 1000 $ do
316+
liftIO (atomically (writeTQueue q ()))
317+
void (receiveTimeout 0 [matchSTM (readTQueue q) return]),
318+
repsBench fx "receiveTimeout 1s (matchSTM ready)" 1000 $ do
319+
liftIO (atomically (writeTQueue q ()))
320+
void (receiveTimeout 1000000 [matchSTM (readTQueue q) return]),
321+
repsBench fx "receiveChanTimeout 0 (value waiting)" 1000 $ do
322+
sendChan sp ()
323+
barrier
324+
void (receiveChanTimeout 0 rp),
325+
repsBench fx "receiveChanTimeout 1s (value waiting)" 1000 $ do
326+
sendChan sp ()
327+
barrier
328+
void (receiveChanTimeout 1000000 rp)
329+
]
330+
where
331+
(sp, rp) = fxChan fx
332+
q = fxQueue fx
333+
334+
barrier :: Process ()
335+
barrier = do
336+
us <- getSelfPid
337+
unsafeSend us ()
338+
expect :: Process ()
339+
340+
leaveInMailbox :: Process ()
341+
leaveInMailbox = do
342+
us <- getSelfPid
343+
unsafeSend us (1 :: Int)
344+
barrier
345+
304346
messages :: Fixture -> Benchmark
305347
messages fx =
306348
bgroup

‎packages/distributed-process/src/Control/Distributed/Process/Internal/CQueue.hs‎

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import Control.Distributed.Process.Internal.StrictList
4343
( StrictList(..)
4444
, append
4545
)
46-
import Data.Maybe (fromJust)
46+
import Control.Monad (join)
4747
import GHC.MVar (MVar(MVar))
4848
import GHC.IO (IO(IO), unIO)
4949
import GHC.Exts (mkWeak#)
@@ -126,16 +126,21 @@ dequeue :: forall m a.
126126
-> [MatchOn m a] -- ^ List of matches
127127
-> IO (Maybe a) -- ^ 'Nothing' only on timeout
128128
dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJust $
129-
case blockSpec of
130-
Timeout n -> timeout n $ fmap fromJust run
131-
_other ->
132-
case chunks of
133-
[Right ports] -> -- channels only, this is easy:
134-
case blockSpec of
135-
NonBlocking -> atomically $ waitChans ports (return Nothing)
136-
_ -> atomically $ waitChans ports retry
137-
-- no onException needed
138-
_other -> run
129+
case chunks of
130+
[Right ports] -> -- channels only, this is easy:
131+
case blockSpec of
132+
NonBlocking -> atomically $ waitChans ports (return Nothing)
133+
Blocking -> atomically $ waitChans ports retry
134+
-- no onException needed
135+
Timeout n -> do
136+
-- Arming the timer is not cheap, and get can get
137+
-- much higher throughput in cases where the mailbox
138+
-- is not empty by first checking if we even need a timeout
139+
r <- atomically $ waitChans ports (return Nothing)
140+
case r of
141+
Just _ -> return r
142+
Nothing -> join <$> timeout n (atomically $ waitChans ports retry)
143+
_other -> run
139144
where
140145
-- Decrement counter is smth is returned from the queue,
141146
-- this is safe to use as method is called under a mask
@@ -155,7 +160,13 @@ dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJus
155160
Nothing -> return xs
156161
Just x -> grabNew (Snoc xs x)
157162
arr' <- grabNew arr
158-
goCheck chunks arr'
163+
checked <- goCheck chunks arr'
164+
case checked of
165+
Left r -> return r
166+
Right old -> case blockSpec of
167+
NonBlocking -> returnOld old Nothing
168+
Blocking -> goWait old
169+
Timeout n -> join <$> timeout n (goWait old)
159170

160171
-- Yields the value of the first succesful STM transaction as
161172
-- @Just (Left v)@. If all transactions fail, yields the value of the second
@@ -169,20 +180,20 @@ dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJus
169180
-- mailbox. For channel matches, we do a non-blocking check at
170181
-- this point.
171182
--
172-
-- Yields @Just (Left a)@ when a channel is matched, @Just (Right a)@
173-
-- when a message is matched and @Nothing@ when there are no messages and we
174-
-- aren't blocking.
175-
--
183+
-- Yields @Left (Just (Left a))@ when a channel is matched and
184+
-- @Left (Just (Right a))@ when a message is matched. When nothing
185+
-- matched it yields @Right old@: the messages to hold on to, for the
186+
-- caller to decide whether to wait for more.
176187
goCheck :: MatchChunks m a
177188
-> StrictList m -- messages to check, in this order
178-
-> IO (Maybe (Either a a))
189+
-> IO (Either (Maybe (Either a a)) (StrictList m))
179190

180-
goCheck [] old = goWait old
191+
goCheck [] old = return (Right old)
181192

182193
goCheck (Right ports : rest) old = do
183194
r <- atomically $ waitChans ports (return Nothing) -- does not block
184195
case r of
185-
Just _ -> returnOld old r
196+
Just _ -> Left <$> returnOld old r
186197
Nothing -> goCheck rest old
187198

188199
goCheck (Left matches : rest) old = do
@@ -192,7 +203,7 @@ dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJus
192203
-- of passing around restore and setting up exception handlers is
193204
-- high. So just don't use expensive matchIfs!
194205
case checkArrived matches old of
195-
(old', Just r) -> returnOld old' (Just (Right r))
206+
(old', Just r) -> Left <$> returnOld old' (Just (Right r))
196207
(old', Nothing) -> goCheck rest old'
197208
-- use the result list, which is now left-biased
198209

@@ -207,12 +218,8 @@ dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJus
207218
mkSTM (Right ports : rest)
208219
= foldr orElse (mkSTM rest) (map (fmap Right) ports)
209220

210-
waitIncoming :: IO (Maybe (Either m a))
211-
waitIncoming = case blockSpec of
212-
NonBlocking -> atomically $ fmap Just stm `orElse` return Nothing
213-
_ -> atomically $ fmap Just stm
214-
where
215-
stm = mkSTM chunks
221+
waitIncoming :: IO (Either m a)
222+
waitIncoming = atomically (mkSTM chunks)
216223

217224
--
218225
-- The initial pass didn't find a message, so now we go into blocking
@@ -223,23 +230,20 @@ dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJus
223230
--
224231
goWait :: StrictList m -> IO (Maybe (Either a a))
225232
goWait old = do
226-
r <- waitIncoming `onException` putMVar arrived old
227-
case r of
228-
-- Nothing => non-blocking and no message
229-
Nothing -> returnOld old Nothing
230-
Just e -> case e of
231-
--
232-
-- Left => message arrived in the process mailbox. We now have to
233-
-- run through the MatchChunks checking each one, because we might
234-
-- have a situation where the first chunk fails to match and the
235-
-- second chunk is a channel match and there *is* a message in the
236-
-- channel. In that case the channel wins.
237-
--
238-
Left m -> goCheck1 chunks m old
239-
--
240-
-- Right => message arrived on a channel first
241-
--
242-
Right a -> returnOld old (Just (Left a))
233+
e <- waitIncoming `onException` putMVar arrived old
234+
case e of
235+
--
236+
-- Left => message arrived in the process mailbox. We now have to
237+
-- run through the MatchChunks checking each one, because we might
238+
-- have a situation where the first chunk fails to match and the
239+
-- second chunk is a channel match and there *is* a message in the
240+
-- channel. In that case the channel wins.
241+
--
242+
Left m -> goCheck1 chunks m old
243+
--
244+
-- Right => message arrived on a channel first
245+
--
246+
Right a -> returnOld old (Just (Left a))
243247

244248
--
245249
-- A message arrived in the process inbox; check the MatchChunks for

‎packages/distributed-process/src/Control/Distributed/Process/Internal/Primitives.hs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,13 @@ receiveChan = liftIO . atomically . receiveSTM
363363
receiveChanTimeout :: Serializable a => Int -> ReceivePort a -> Process (Maybe a)
364364
receiveChanTimeout 0 ch = liftIO . atomically $
365365
(Just <$> receiveSTM ch) `orElse` return Nothing
366-
receiveChanTimeout n ch = liftIO . timeout n . atomically $
367-
receiveSTM ch
366+
receiveChanTimeout n ch = liftIO $ do
367+
-- Checking if the mailbox has a message /before/ arming,
368+
-- because arming a timeout can be expensive
369+
r <- atomically $ (Just <$> receiveSTM ch) `orElse` return Nothing
370+
case r of
371+
Just _ -> return r
372+
Nothing -> timeout n . atomically $ receiveSTM ch
368373

369374
-- | Merge a list of typed channels.
370375
--

0 commit comments

Comments
 (0)