-
Notifications
You must be signed in to change notification settings - Fork 720
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
Reduce memory usage of create staked command #4021
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,8 +323,9 @@ instance HasTypeProxy StakeKey where | |
|
||
instance Key StakeKey where | ||
|
||
newtype VerificationKey StakeKey = | ||
StakeVerificationKey (Shelley.VKey Shelley.Staking StandardCrypto) | ||
newtype VerificationKey StakeKey = StakeVerificationKey | ||
{ unStakeVerificationKey :: Shelley.VKey Shelley.Staking StandardCrypto | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use the field accessor here: https://github.com/input-output-hk/cardano-node/pull/4021/files#r915507713 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, I am always in favour of added an accessor to a newtype. If its there and no one uses it, its not a big deal (especially with a totally un-ambiguous name like this), but when its needed having it already there is a huge win IMO. |
||
} | ||
deriving stock (Eq) | ||
deriving newtype (ToCBOR, FromCBOR) | ||
deriving anyclass SerialiseAsCBOR | ||
|
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think we should duplicate the functions used from
hw-lazy
. The node has enough dependencies and we need to be stricter about introducing new ones.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put the functions into
Cardano.CLI.IO.Lazy
for the moment.