3

I have the following function:

loopMyQ s q m = forever $ do
      q' <- atomically $ readTVar q
      let Just b = PSQ.findMin q' --irrefutable pattern here in case the queue has just been created
          duetime = (PSQ.prio b) + 2.000
      now <- getPOSIXTime
      when (now > duetime) (transMit2 s now q m)

The problem is that when the PSQ has "just" been created as empty PSQ the Just cannot match and gives me an irrefutable pattern error at runtime. This occurs exactly once since apparently the queue later is filled and Just b always matches.

I have tried to test whether the queue is empty or not and then act on it in my function BUT this made the whole thing run twice as slow.

Since this apparently does not hurt can this error somehow be suppressed with e.g. a compiler option or would I need to catch the exception and then ignore it (that also might cost extra time).

3
  • 1
    Stop worrying about might cost extra time. Write correct, checked code, and profile it. If the checks are what is slowing the code down, then deal with it. Premature optimization is the root of all evil.
    – rampion
    Mar 13, 2012 at 16:56
  • 2
    I think you need to explain more about how a failed pattern match can not hurt. For example, it's worth mentioning that loopMyQ is (as you stated in another comment) started inside a thread with forkIO, and presumably it's okay if that thread dies when the exception is thrown. I have a suspicion that the slowdown you see with other solutions has more to do with what your other threads are doing than what's happening here. Mar 14, 2012 at 0:15
  • @rampion , benmachine Thanks, your comments brought me on the right path. Through profiling I found out that the forever loop in loopMyQ was running as often as the CPU could do and did some action every 200ms. Whilst for me it was sufficient to run it every 200ms only and do some action if required.
    – J Fritsch
    Mar 14, 2012 at 12:48

2 Answers 2

9

You are probably better off by using retry if the queue is empty: the STM action will not be retried until the queue in the TVar has been updated!

loopMyQ s q m = forever $ do

      b <- atomically $ do q' <- readTVar q
                           case PSQ.findMin q' of
                              Just b  -> return b
                              Nothing -> retry

      let duetime = (PSQ.prio b) + 2.000
      now <- getPOSIXTime
      when (now > duetime) (transMit2 s now q m)
6
  • I found numerous solutions similar to this. All no joy in terms of execution time and speed. The above triples the execution time ... .
    – J Fritsch
    Mar 13, 2012 at 14:32
  • @JFritsch Sounds like you have a choice between "correct" and "fast". Choose carefully. Mar 13, 2012 at 14:41
  • @DanielWagner I still hope that someone comes up with something like '{-# DONT print f* runtime errors as terminal output #-}'
    – J Fritsch
    Mar 13, 2012 at 15:12
  • @JFritsch: interesting that your code runs slower with retry. I guessed that rampion's polling would be slower. Do you have a larger snippet that we can test? Anyway, just surpressing error output will not help your original code: loopMyQ cannot continue without the b bound in Just b.
    – danr
    Mar 13, 2012 at 15:31
  • 2
    @JFritsch Triples execution compared to what other implementation? As for retry, it's pretty efficient, in the sense that the transaction that's aborted won't be tried again until one of the TVars it read is changed. You might be able to recover some speed by having finer-grained TVars, so that retry has finer-grained information about when it has a chance of succeeding. Mar 13, 2012 at 16:55
5

Assuming that once your queue is non-empty, it will never be empty again, one thing you could do is do the expensive version (check for non-empty) only until it becomes non-empty, then switch to the cheap version.

loopMyQ s q m = do
    q' <- atomically $ readTVar q
    case PSQ.findMin q' of
      Nothing -> loopMyQ s q m
      Just b  -> do
        body b
        forever $ do
          q' <- atomically $ readTVar q
          let Just b <- PSQ.findMin q'
          body b              
  where body b = do
    let duetime = 2 + PSQ.prio b
    now <- getPOSIXTime
    when (now > duetime) (transMit2 s now q m)
4
  • Works, - just found out that in practice there can be more conditions where the queue runs empty. Is there no way to suppress / completely ignore such a runtime error? E.g. using PatternMatchFail?
    – J Fritsch
    Mar 13, 2012 at 7:56
  • 2
    You can catch run time errors such as pattern match failures using Control.Exception, but this will make your code buggy (because of order of execution), hard to maintain, unidiomatic, and it has to live in the IO monad since you can only catch those errors there. Using such machinery instead of simply using pattern matching is just downright wrong :)
    – danr
    Mar 13, 2012 at 8:25
  • J Fritsch: Why not alter your other code to only store non-empty queues in q?
    – rampion
    Mar 13, 2012 at 11:51
  • 2
    That outer loop, 'loopMyQ' looks like a busy wait. You ought to put a test and 'retry' call inside the first atomically. The current TVar design still feels wrong, however. Mar 13, 2012 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.