Skip to content

Commit bc10beb

Browse files
authored
Merge pull request #4826 from IntersectMBO/lehins/reduce-memory-usage
Reduce memory usage with sharing
2 parents c9e2fda + 2ad0bf9 commit bc10beb

File tree

25 files changed

+412
-198
lines changed

25 files changed

+412
-198
lines changed

eras/conway/impl/cardano-ledger-conway.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ library
105105
deepseq,
106106
mempack,
107107
microlens,
108+
mtl,
108109
nothunks,
109110
plutus-ledger-api >=1.37,
110111
set-algebra,

eras/conway/impl/src/Cardano/Ledger/Conway/Governance.hs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,15 @@ import Cardano.Ledger.Binary (
191191
DecShareCBOR (..),
192192
EncCBOR (..),
193193
FromCBOR (..),
194+
Interns,
194195
ToCBOR (..),
195196
decNoShareCBOR,
197+
decodeRecordNamedT,
196198
)
197199
import Cardano.Ledger.Binary.Coders (
198-
Decode (..),
199200
Encode (..),
200-
decode,
201201
encode,
202202
(!>),
203-
(<!),
204203
)
205204
import Cardano.Ledger.CertState (
206205
CommitteeAuthorization (..),
@@ -247,6 +246,7 @@ import Cardano.Ledger.UMap
247246
import Cardano.Ledger.Val (Val (..))
248247
import Control.DeepSeq (NFData (..))
249248
import Control.Monad (guard)
249+
import Control.Monad.Trans
250250
import Control.Monad.Trans.Reader (ReaderT, ask)
251251
import Data.Aeson (KeyValue, ToJSON (..), object, pairs, (.=))
252252
import Data.Default (Default (..))
@@ -351,18 +351,24 @@ mkEnactState gs =
351351
, ensPrevGovActionIds = govStatePrevGovActionIds gs
352352
}
353353

354-
-- TODO: Implement Sharing: https://github.com/intersectmbo/cardano-ledger/issues/3486
355354
instance EraPParams era => DecShareCBOR (ConwayGovState era) where
356-
decShareCBOR _ =
357-
decode $
358-
RecD ConwayGovState
359-
<! From
360-
<! From
361-
<! From
362-
<! From
363-
<! From
364-
<! From
365-
<! From
355+
type
356+
Share (ConwayGovState era) =
357+
( Interns (Credential 'Staking)
358+
, Interns (KeyHash 'StakePool)
359+
, Interns (Credential 'DRepRole)
360+
, Interns (Credential 'HotCommitteeRole)
361+
)
362+
decSharePlusCBOR =
363+
decodeRecordNamedT "ConwayGovState" (const 7) $ do
364+
cgsProposals <- decSharePlusCBOR
365+
cgsCommittee <- lift decCBOR
366+
cgsConstitution <- lift decCBOR
367+
cgsCurPParams <- lift decCBOR
368+
cgsPrevPParams <- lift decCBOR
369+
cgsFuturePParams <- lift decCBOR
370+
cgsDRepPulsingState <- decSharePlusCBOR
371+
pure ConwayGovState {..}
366372

367373
instance EraPParams era => DecCBOR (ConwayGovState era) where
368374
decCBOR = decNoShareCBOR

eras/conway/impl/src/Cardano/Ledger/Conway/Governance/DRepPulser.hs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ import Cardano.Ledger.Binary (
4343
DecShareCBOR (..),
4444
EncCBOR (..),
4545
FromCBOR (..),
46+
Interns,
4647
ToCBOR (..),
48+
decNoShareCBOR,
49+
decodeMap,
50+
decodeStrictSeq,
51+
interns,
4752
)
4853
import Cardano.Ledger.Binary.Coders (
4954
Decode (..),
@@ -146,24 +151,24 @@ instance EraPParams era => EncCBOR (PulsingSnapshot era) where
146151
!> To psDRepState
147152
!> To psPoolDistr
148153

149-
-- TODO: Implement Sharing: https://github.com/intersectmbo/cardano-ledger/issues/3486
150154
instance EraPParams era => DecShareCBOR (PulsingSnapshot era) where
151-
decShareCBOR _ =
155+
type
156+
Share (PulsingSnapshot era) =
157+
( Interns (Credential 'Staking)
158+
, Interns (KeyHash 'StakePool)
159+
, Interns (Credential 'DRepRole)
160+
, Interns (Credential 'HotCommitteeRole)
161+
)
162+
decShareCBOR is@(cs, ks, cd, _) =
152163
decode $
153164
RecD PulsingSnapshot
154-
<! From
155-
<! From
156-
<! From
157-
<! From
165+
<! D (decodeStrictSeq (decShareCBOR is))
166+
<! D (decodeMap (decShareCBOR cd) decCBOR)
167+
<! D (decodeMap (interns cd <$> decCBOR) (decShareCBOR cs))
168+
<! D (decodeMap (interns ks <$> decCBOR) decCBOR)
158169

159170
instance EraPParams era => DecCBOR (PulsingSnapshot era) where
160-
decCBOR =
161-
decode $
162-
RecD PulsingSnapshot
163-
<! From
164-
<! From
165-
<! From
166-
<! From
171+
decCBOR = decNoShareCBOR
167172

168173
instance EraPParams era => ToCBOR (PulsingSnapshot era) where
169174
toCBOR = toEraCBOR @era
@@ -436,13 +441,19 @@ instance EraPParams era => EncCBOR (DRepPulsingState era) where
436441
where
437442
(snap, ratstate) = finishDRepPulser x
438443

439-
-- TODO: Implement Sharing: https://github.com/intersectmbo/cardano-ledger/issues/3486
440444
instance EraPParams era => DecShareCBOR (DRepPulsingState era) where
441-
decShareCBOR _ =
445+
type
446+
Share (DRepPulsingState era) =
447+
( Interns (Credential 'Staking)
448+
, Interns (KeyHash 'StakePool)
449+
, Interns (Credential 'DRepRole)
450+
, Interns (Credential 'HotCommitteeRole)
451+
)
452+
decShareCBOR is =
442453
decode $
443454
RecD DRComplete
444-
<! From
445-
<! From
455+
<! D (decShareCBOR is)
456+
<! D (decShareCBOR is)
446457

447458
instance EraPParams era => DecCBOR (DRepPulsingState era) where
448459
decCBOR = decode (RecD DRComplete <! From <! From)

eras/conway/impl/src/Cardano/Ledger/Conway/Governance/Internal.hs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
{-# LANGUAGE ScopedTypeVariables #-}
1313
{-# LANGUAGE StandaloneDeriving #-}
1414
{-# LANGUAGE TypeApplications #-}
15+
{-# LANGUAGE TypeFamilies #-}
1516
{-# LANGUAGE UndecidableInstances #-}
1617
{-# OPTIONS_GHC -Wno-orphans #-}
1718

@@ -74,8 +75,12 @@ import Cardano.Ledger.Binary (
7475
DecShareCBOR (..),
7576
EncCBOR (..),
7677
FromCBOR (..),
78+
Interns,
7779
ToCBOR (..),
7880
decNoShareCBOR,
81+
decodeMap,
82+
decodeSeq,
83+
interns,
7984
)
8085
import Cardano.Ledger.Binary.Coders (
8186
Decode (..),
@@ -219,17 +224,17 @@ instance EraPParams era => Default (EnactState era) where
219224
instance EraPParams era => DecCBOR (EnactState era) where
220225
decCBOR = decNoShareCBOR
221226

222-
-- TODO: Implement Sharing: https://github.com/intersectmbo/cardano-ledger/issues/3486
223227
instance EraPParams era => DecShareCBOR (EnactState era) where
224-
decShareCBOR _ =
228+
type Share (EnactState era) = Interns (Credential 'Staking)
229+
decShareCBOR is =
225230
decode $
226231
RecD EnactState
227232
<! From
228233
<! From
229234
<! From
230235
<! From
231236
<! From
232-
<! From
237+
<! D (decodeMap (interns is <$> decCBOR) decCBOR)
233238
<! From
234239

235240
instance EraPParams era => EncCBOR (EnactState era) where
@@ -263,7 +268,9 @@ data RatifyState era = RatifyState
263268
-- ^ This is the currently active `EnactState`. It contains all the changes
264269
-- that were applied to it at the last epoch boundary by all the proposals
265270
-- that were enacted.
266-
, rsEnacted :: !(Seq (GovActionState era))
271+
, -- TODO: switch rsEnacted to StrictSeq for the sake of avoiding
272+
-- space leaks during ledger state deserialization
273+
rsEnacted :: !(Seq (GovActionState era))
267274
-- ^ Governance actions that are going to be enacted at the next epoch
268275
-- boundary.
269276
, rsExpired :: !(Set GovActionId)
@@ -678,12 +685,18 @@ instance EraPParams era => DecCBOR (RatifySignal era) where
678685
instance EraPParams era => DecCBOR (RatifyState era) where
679686
decCBOR = decode (RecD RatifyState <! From <! From <! From <! From)
680687

681-
-- TODO: Implement Sharing: https://github.com/intersectmbo/cardano-ledger/issues/3486
682688
instance EraPParams era => DecShareCBOR (RatifyState era) where
683-
decShareCBOR _ =
689+
type
690+
Share (RatifyState era) =
691+
( Interns (Credential 'Staking)
692+
, Interns (KeyHash 'StakePool)
693+
, Interns (Credential 'DRepRole)
694+
, Interns (Credential 'HotCommitteeRole)
695+
)
696+
decShareCBOR is@(cs, _, _, _) =
684697
decode $
685698
RecD RatifyState
686-
<! From
687-
<! From
699+
<! D (decShareCBOR cs)
700+
<! D (decodeSeq (decShareCBOR is))
688701
<! From
689702
<! From

eras/conway/impl/src/Cardano/Ledger/Conway/Governance/Procedures.hs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
{-# LANGUAGE StandaloneDeriving #-}
2020
{-# LANGUAGE TupleSections #-}
2121
{-# LANGUAGE TypeApplications #-}
22+
{-# LANGUAGE TypeFamilies #-}
2223
{-# LANGUAGE TypeOperators #-}
24+
{-# LANGUAGE TypeSynonymInstances #-}
2325
{-# LANGUAGE UndecidableInstances #-}
2426

2527
module Cardano.Ledger.Conway.Governance.Procedures (
@@ -92,16 +94,19 @@ import Cardano.Ledger.Binary (
9294
DecShareCBOR (..),
9395
EncCBOR (..),
9496
FromCBOR (fromCBOR),
97+
Interns,
9598
ToCBOR (toCBOR),
9699
decNoShareCBOR,
97100
decodeEnumBounded,
98101
decodeMapByKey,
99102
decodeNullStrictMaybe,
100103
decodeRecordNamed,
104+
decodeRecordNamedT,
101105
encodeEnum,
102106
encodeListLen,
103107
encodeNullStrictMaybe,
104108
encodeWord8,
109+
internsFromMap,
105110
invalidKey,
106111
)
107112
import Cardano.Ledger.Binary.Coders (
@@ -120,6 +125,8 @@ import Cardano.Ledger.TxIn (TxId (..))
120125
import Cardano.Slotting.Slot (EpochNo)
121126
import Control.DeepSeq (NFData (..), deepseq)
122127
import Control.Monad (when)
128+
import Control.Monad.Trans (lift)
129+
import Control.Monad.Trans.State.Strict (get, put)
123130
import Data.Aeson (
124131
FromJSON (..),
125132
KeyValue (..),
@@ -280,18 +287,30 @@ instance EraPParams era => NoThunks (GovActionState era)
280287

281288
instance EraPParams era => NFData (GovActionState era)
282289

283-
-- TODO: Implement Sharing: https://github.com/intersectmbo/cardano-ledger/issues/3486
284290
instance EraPParams era => DecShareCBOR (GovActionState era) where
285-
decShareCBOR _ =
286-
decode $
287-
RecD GovActionState
288-
<! From
289-
<! From
290-
<! From
291-
<! From
292-
<! From
293-
<! From
294-
<! From
291+
type
292+
Share (GovActionState era) =
293+
( Interns (Credential 'Staking)
294+
, Interns (KeyHash 'StakePool)
295+
, Interns (Credential 'DRepRole)
296+
, Interns (Credential 'HotCommitteeRole)
297+
)
298+
decSharePlusCBOR =
299+
decodeRecordNamedT "GovActionState" (const 7) $ do
300+
gasId <- lift decCBOR
301+
302+
(cs, ks, cd, ch) <- get
303+
gasCommitteeVotes <- lift $ decShareCBOR (ch, mempty)
304+
gasDRepVotes <- lift $ decShareCBOR (cd, mempty)
305+
gasStakePoolVotes <- lift $ decShareCBOR (ks, mempty)
306+
307+
-- DRep votes do not contain any new credentials, thus only additon of interns for SPOs and CCs
308+
put (cs, ks <> internsFromMap gasStakePoolVotes, cd, ch <> internsFromMap gasCommitteeVotes)
309+
310+
gasProposalProcedure <- lift decCBOR
311+
gasProposedIn <- lift decCBOR
312+
gasExpiresAfter <- lift decCBOR
313+
pure GovActionState {..}
295314

296315
instance EraPParams era => DecCBOR (GovActionState era) where
297316
decCBOR = decNoShareCBOR
@@ -308,7 +327,6 @@ instance EraPParams era => EncCBOR (GovActionState era) where
308327
!> To gasProposedIn
309328
!> To gasExpiresAfter
310329

311-
-- Ref: https://gitlab.haskell.org/ghc/ghc/-/issues/14046
312330
instance OMap.HasOKey GovActionId (GovActionState era) where
313331
okeyL = lens gasId $ \gas gi -> gas {gasId = gi}
314332

eras/conway/impl/src/Cardano/Ledger/Conway/Governance/Proposals.hs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
{-# LANGUAGE QuantifiedConstraints #-}
1616
{-# LANGUAGE ScopedTypeVariables #-}
1717
{-# LANGUAGE TypeApplications #-}
18+
{-# LANGUAGE TypeFamilies #-}
1819
{-# LANGUAGE UndecidableInstances #-}
1920

2021
-- | This module isolates all the types and functionality around
@@ -125,6 +126,10 @@ import Cardano.Ledger.Binary (
125126
DecCBOR (..),
126127
DecShareCBOR (..),
127128
EncCBOR (..),
129+
Interns,
130+
decodeListLenOrIndef,
131+
decodeListLikeWithCountT,
132+
decodeRecordNamedT,
128133
)
129134
import Cardano.Ledger.Coin (Coin, CompactForm (CompactCoin))
130135
import Cardano.Ledger.Conway.Governance.Procedures
@@ -134,6 +139,7 @@ import Cardano.Ledger.UMap (addCompact, toCompact)
134139
import Control.DeepSeq (NFData)
135140
import Control.Exception (assert)
136141
import Control.Monad (unless)
142+
import Control.Monad.Trans (lift)
137143
import Data.Aeson (ToJSON (..))
138144
import Data.Default (Default (..))
139145
import Data.Either (partitionEithers)
@@ -359,9 +365,20 @@ instance EraPParams era => EncCBOR (Proposals era) where
359365
instance EraPParams era => DecCBOR (Proposals era) where
360366
decCBOR = decCBOR >>= uncurry mkProposals
361367

362-
-- TODO: Implement Sharing: https://github.com/intersectmbo/cardano-ledger/issues/3486
363368
instance EraPParams era => DecShareCBOR (Proposals era) where
364-
decShareCBOR _ = decCBOR
369+
type
370+
Share (Proposals era) =
371+
( Interns (Credential 'Staking)
372+
, Interns (KeyHash 'StakePool)
373+
, Interns (Credential 'DRepRole)
374+
, Interns (Credential 'HotCommitteeRole)
375+
)
376+
decSharePlusCBOR = do
377+
decodeRecordNamedT "Proposals" (const 2) $ do
378+
gaid <- lift decCBOR
379+
(_, omap) <- decodeListLikeWithCountT (lift decodeListLenOrIndef) (flip (OMap.|>)) $ \_ ->
380+
decSharePlusCBOR
381+
mkProposals gaid omap
365382

366383
-- | Add a vote to an existing `GovActionState`. This is a no-op if the
367384
-- provided `GovActionId` does not already exist

0 commit comments

Comments
 (0)