Skip to content

Commit

Permalink
quasar: Improve futureDisposer for trivial and completed futures
Browse files Browse the repository at this point in the history
  • Loading branch information
queezle42 committed Apr 7, 2024
1 parent 95f30c5 commit 459c42d
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions quasar/src/Quasar/Resources/FutureDisposer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ instance Disposable FutureDisposer where

futureDisposer :: Future Disposer -> STMc NoRetry '[] Disposer
futureDisposer future = do
key <- newUniqueSTM
var <- newTOnce future
pure (getDisposer (FutureDisposer key var))
peekFuture future >>= \case
Just disposer ->
-- Simply pass through the disposer if the future is already completed or
-- trivial.
pure disposer
Nothing -> do
key <- newUniqueSTM
var <- newTOnce future
pure (getDisposer (FutureDisposer key var))

futureDisposerGeneric :: (Disposable a, MonadSTMc NoRetry '[] m) => Future a -> m Disposer
futureDisposerGeneric x = liftSTMc (futureDisposer (getDisposer <$> x))

0 comments on commit 459c42d

Please sign in to comment.