-
Notifications
You must be signed in to change notification settings - Fork 720
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce memory usage of the create-staked command using lazy IO.
- Loading branch information
Showing
14 changed files
with
353 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
||
module Cardano.CLI.IO.Lazy | ||
( replicateM | ||
, sequenceM | ||
, traverseM | ||
, traverseStateM | ||
, forM | ||
, forStateM | ||
) where | ||
|
||
import Control.Applicative (Applicative((<*>), pure), (<$>)) | ||
import Control.Monad (Monad(..)) | ||
import Control.Monad.IO.Unlift (MonadIO(liftIO), MonadUnliftIO, askUnliftIO, UnliftIO(unliftIO)) | ||
import Data.Function (($), (.), flip) | ||
import Data.Int (Int) | ||
import System.IO (IO) | ||
|
||
import qualified Data.List as L | ||
import qualified System.IO.Unsafe as IO | ||
|
||
replicateM :: MonadUnliftIO m => Int -> m a -> m [a] | ||
replicateM n f = sequenceM (L.replicate n f) | ||
|
||
sequenceM :: MonadUnliftIO m => [m a] -> m [a] | ||
sequenceM as = do | ||
f <- askUnliftIO | ||
liftIO $ sequenceIO (L.map (unliftIO f) as) | ||
|
||
-- | Traverses the function over the list and produces a lazy list in a | ||
-- monadic context. | ||
-- | ||
-- It is intended to be like the "standard" 'traverse' except | ||
-- that the list is generated lazily. | ||
traverseM :: MonadUnliftIO m => (a -> m b) -> [a] -> m [b] | ||
traverseM f as = do | ||
u <- askUnliftIO | ||
liftIO $ IO.unsafeInterleaveIO (go u as) | ||
where | ||
go _ [] = pure [] | ||
go !u (v:vs) = do | ||
!res <- unliftIO u (f v) | ||
rest <- IO.unsafeInterleaveIO (go u vs) | ||
pure (res:rest) | ||
|
||
traverseStateM :: forall m s a b. MonadUnliftIO m => s -> (s -> a -> m (s, b)) -> [a] -> m [b] | ||
traverseStateM s f as = do | ||
u <- askUnliftIO | ||
liftIO $ IO.unsafeInterleaveIO (go s u as) | ||
where | ||
go :: s -> UnliftIO m -> [a] -> IO [b] | ||
go _ _ [] = pure [] | ||
go t !u (v:vs) = do | ||
(t', !res) <- unliftIO u (f t v) | ||
rest <- IO.unsafeInterleaveIO (go t' u vs) | ||
pure (res:rest) | ||
|
||
forM :: MonadUnliftIO m => [a] -> (a -> m b) -> m [b] | ||
forM = flip traverseM | ||
|
||
forStateM :: MonadUnliftIO m => s -> [a] -> (s -> a -> m (s, b)) -> m [b] | ||
forStateM s as f = traverseStateM s f as | ||
|
||
-- Internal | ||
sequenceIO :: [IO a] -> IO [a] | ||
sequenceIO = IO.unsafeInterleaveIO . go | ||
where go :: [IO a] -> IO [a] | ||
go [] = return [] | ||
go (fa:fas) = (:) <$> fa <*> IO.unsafeInterleaveIO (go fas) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.