Skip to content

Commit

Permalink
Add a few INLINE pragmas to reduce closure allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
arybczak committed Apr 11, 2024
1 parent a235b07 commit 2a334e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 6 additions & 0 deletions effectful-core/src/Effectful/Dispatch/Dynamic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ localUnlift (LocalEnv les) strategy k = case strategy of
ConcUnlift p l -> unsafeEff $ \es -> do
concUnliftIO les p l $ \unlift -> do
(`unEff` es) $ k $ unsafeEff_ . unlift
{-# INLINE localUnlift #-}

-- | Create a local unlifting function with the given strategy.
localUnliftIO
Expand All @@ -577,6 +578,7 @@ localUnliftIO
localUnliftIO (LocalEnv les) strategy k = case strategy of
SeqUnlift -> liftIO $ seqUnliftIO les k
ConcUnlift p l -> liftIO $ concUnliftIO les p l k
{-# INLINE localUnliftIO #-}

----------------------------------------
-- Lifts
Expand Down Expand Up @@ -618,6 +620,7 @@ localLift !_ strategy k = case strategy of
ConcUnlift p l -> unsafeEff $ \es -> do
concUnliftIO es p l $ \unlift -> do
(`unEff` es) $ k $ unsafeEff_ . unlift
{-# INLINE localLift #-}

-- | Utility for lifting 'Eff' computations of type
--
Expand Down Expand Up @@ -708,6 +711,7 @@ localLiftUnlift (LocalEnv les) strategy k = case strategy of
concUnliftIO es p l $ \unliftEs -> do
concUnliftIO les p l $ \unliftLocalEs -> do
(`unEff` es) $ k (unsafeEff_ . unliftEs) (unsafeEff_ . unliftLocalEs)
{-# INLINE localLiftUnlift #-}

-- | Create a local unlifting function with the given strategy along with an
-- unrestricted lifting function.
Expand All @@ -728,6 +732,7 @@ localLiftUnliftIO
localLiftUnliftIO (LocalEnv les) strategy k = case strategy of
SeqUnlift -> liftIO $ seqUnliftIO les $ k unsafeEff_
ConcUnlift p l -> liftIO $ concUnliftIO les p l $ k unsafeEff_
{-# INLINE localLiftUnliftIO #-}

----------------------------------------
-- Misc
Expand Down Expand Up @@ -805,6 +810,7 @@ localHandle (LocalEnv les) strategy k = case strategy of
ConcUnlift p l -> unsafeEff $ \es -> do
eles <- copyRef es les
concUnliftIO eles p l $ \unlift -> (`unEff` es) $ k $ unsafeEff_ . unlift
{-# INLINE localHandle #-}

copyRef
:: forall e es localEs. e :> es
Expand Down
8 changes: 5 additions & 3 deletions effectful-core/src/Effectful/Internal/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,10 @@ withEffToIO
-> ((forall r. Eff es r -> IO r) -> IO a)
-- ^ Continuation with the unlifting function in scope.
-> Eff es a
withEffToIO strategy f = case strategy of
SeqUnlift -> unsafeEff $ \es -> seqUnliftIO es f
ConcUnlift p b -> unsafeEff $ \es -> concUnliftIO es p b f
withEffToIO strategy k = case strategy of
SeqUnlift -> unsafeEff $ \es -> seqUnliftIO es k
ConcUnlift p b -> unsafeEff $ \es -> concUnliftIO es p b k
{-# INLINE withEffToIO #-}

-- | Create an unlifting function with the 'ConcUnlift' strategy.
--
Expand Down Expand Up @@ -419,6 +420,7 @@ raiseWith strategy k = case strategy of
es <- tailEnv ees
concUnliftIO ees p l $ \unlift -> do
(`unEff` es) $ k $ unsafeEff_ . unlift
{-# INLINE raiseWith #-}

-- | Eliminate a duplicate effect from the top of the effect stack.
subsume :: e :> es => Eff (e : es) a -> Eff es a
Expand Down
2 changes: 2 additions & 0 deletions effectful-core/src/Effectful/Internal/Unlift.hs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ ephemeralConcUnlift uses k es0 unEff = do
es <- cloneEnv esTemplate
pure (n - 1, es)
unEff m es
{-# NOINLINE ephemeralConcUnlift #-}

-- | Concurrent unlift that preserves the environment between calls to the
-- unlifting function within a particular thread.
Expand Down Expand Up @@ -211,6 +212,7 @@ persistentConcUnlift cleanUp threads k es0 unEff = do
}
pure (newEntries, es)
unEff m es
{-# NOINLINE persistentConcUnlift #-}

----------------------------------------
-- Data types
Expand Down

0 comments on commit 2a334e3

Please sign in to comment.