Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimization by class MonadThunk arguments order #859

Merged
merged 14 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Thunk: unflip the further instances
`forces` are already flipped, which gives tail recursion.

This one gives more straight-forward implementation & use, maybe with some
performance optimization.

M  src/Nix/Cited/Basic.hs
M  src/Nix/Standard.hs
M  src/Nix/Thunk.hs
M  src/Nix/Thunk/Basic.hs
  • Loading branch information
Anton-Latukha committed Feb 23, 2021
commit fcceddf525aece1823ae957a37b12544d23b6d4b
4 changes: 2 additions & 2 deletions src/Nix/Cited/Basic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ instance ( Has e Options
Provenance scope e@(Compose (Ann s _)) : _ ->
withFrame Info (ForcingExpr scope (wrapExprLoc s e)) (forceEff f t)

further :: Cited u f m t -> (m v -> m v) -> m (Cited u f m t)
further (Cited (NCited ps t)) f = Cited . NCited ps <$> further t f
further :: (m v -> m v) -> Cited u f m t -> m (Cited u f m t)
further f (Cited (NCited ps t)) = Cited . NCited ps <$> further f t
8 changes: 4 additions & 4 deletions src/Nix/Standard.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ instance ( MonadAtomicRef m
=> MonadThunk (StdThunk m) m (StdValue m) where

thunk :: m (StdValue m) -> m (StdThunk m)
thunk = fmap (StdThunk . StdCited) . thunk
thunk v = StdThunk . StdCited <$> thunk v

thunkId :: StdThunk m -> ThunkId m
thunkId = thunkId . _stdCited . _stdThunk
Expand All @@ -142,8 +142,8 @@ instance ( MonadAtomicRef m
forceEff :: (StdValue m -> m r) -> StdThunk m -> m r
forceEff f t = forceEff f (_stdCited $ _stdThunk t)

further :: StdThunk m -> (m (StdValue m) -> m (StdValue m)) -> m (StdThunk m)
further t f = ((fmap (StdThunk . StdCited) .) $ further $ _stdCited $ _stdThunk t) f
further :: (m (StdValue m) -> m (StdValue m)) -> StdThunk m -> m (StdThunk m)
further f t = StdThunk . StdCited <$> further f (_stdCited $ _stdThunk t)

instance ( MonadAtomicRef m
, MonadCatch m
Expand Down Expand Up @@ -172,7 +172,7 @@ instance ( MonadAtomicRef m
-> m (StdValue m)
)
-> m (StdValue m)
inform (Pure t) f = Pure <$> further t f
inform (Pure t) f = Pure <$> further f t
inform (Free v) f = Free <$> bindNValue' id (`inform` f) v

{------------------------------------------------------------------------}
Expand Down
2 changes: 1 addition & 1 deletion src/Nix/Thunk.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MonadThunkId m => MonadThunk t m a | t -> m, t -> a where

-- | Modify the action to be performed by the thunk. For some implicits
-- this modifies the thunk, for others it may create a new thunk.
further :: t -> (m a -> m a) -> m t
further :: (m a -> m a) -> t -> m t

newtype ThunkLoop = ThunkLoop String -- contains rendering of ThunkId
deriving Typeable
Expand Down
6 changes: 3 additions & 3 deletions src/Nix/Thunk/Basic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ forceEffects k (Thunk _ active ref) = do
k v

furtherThunk :: MonadVar m
=> NThunkF m v
-> (m v -> m v)
=> (m v -> m v)
-> NThunkF m v
-> m (NThunkF m v)
furtherThunk t@(Thunk _ _ ref) k = do
furtherThunk k t@(Thunk _ _ ref) = do
_ <- atomicModifyVar ref $ \x -> case x of
Computed _ -> (x, x)
Deferred d -> (Deferred (k d), x)
Expand Down