Skip to content

Commit fe0af09

Browse files
authored
Merge pull request #5545 from IntersectMBO/lehins/fix-nothunks-tests
Fix nightly CI
2 parents 324efe5 + a4d2f72 commit fe0af09

File tree

9 files changed

+118
-38
lines changed

9 files changed

+118
-38
lines changed

eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rewards.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ import Cardano.Ledger.Slot (epochInfoSize)
104104
import Cardano.Ledger.Val (Val (..), invert, (<+>), (<->))
105105
import Cardano.Protocol.Crypto (VRF, hashVerKeyVRF)
106106
import Cardano.Slotting.Slot (EpochSize (..))
107+
import Control.DeepSeq
107108
import Control.Monad (replicateM)
108109
import Control.Monad.Trans.Reader (asks, runReader)
109110
import Data.Foldable as F (fold, foldl')
@@ -823,7 +824,7 @@ mkSnapShot activeStake delegs stakePools =
823824
, ssTotalActiveStake = totalActiveStake
824825
, ssDelegations = delegs
825826
, ssPoolParams = stakePools
826-
, ssStakePoolsSnapShot = VMap.map snapShotFromStakePoolParams stakePools
827+
, ssStakePoolsSnapShot = force $ VMap.map snapShotFromStakePoolParams stakePools
827828
}
828829
where
829830
snapShotFromStakePoolParams stakePoolParams =

libs/cardano-ledger-binary/src/Cardano/Ledger/Binary/Decoding/Sharing.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ internsFromMap m
129129
}
130130
]
131131

132-
internsFromVMap :: Ord k => VMap VB kv k a -> Interns k
132+
internsFromVMap :: (Ord k, VMap.Vector kv a) => VMap VB kv k a -> Interns k
133133
internsFromVMap m
134134
| VMap.size m == 0 = mempty
135135
| otherwise =

libs/cardano-ledger-core/src/Cardano/Ledger/State/SnapShots.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,10 @@ data SnapShot = SnapShot
284284
{ ssStake :: !Stake -- TODO: rename to `ssActiveStake`
285285

286286
-- ^ All of the stake for registered staking credentials that have a delegation to a stake pool.
287-
, ssTotalActiveStake :: NonZero Coin -- Note: lazy on purpose
288-
289-
-- ^ Total active stake, which is the sum of all of the stake from `ssStake`, which is why it is
290-
-- lazy. It is primarily used in a denominator, therefore it cannot be zero and is defaulted to
291-
-- 1. This is a reasonable assumption for a system that relies on non-zero active stake to produce
292-
-- blocks.
287+
, ssTotalActiveStake :: !(NonZero Coin)
288+
-- ^ Total active stake, which is the sum of all of the stake from `ssStake`. It is primarily used
289+
-- in a denominator, therefore it cannot be zero and is defaulted to 1. This is a reasonable
290+
-- assumption for a system that relies on non-zero active stake to produce blocks.
293291
, ssDelegations :: VMap VB VB (Credential Staking) (KeyHash StakePool) -- TODO: remove (lazy on purpose)
294292
, ssPoolParams :: VMap VB VB (KeyHash StakePool) StakePoolParams -- TODO: remove (lazy on purpose)
295293
, ssStakePoolsSnapShot :: !(VMap VB VB (KeyHash StakePool) StakePoolSnapShot)
@@ -299,7 +297,7 @@ data SnapShot = SnapShot
299297
deriving (ToJSON) via KeyValuePairs SnapShot
300298
deriving
301299
(NoThunks)
302-
via AllowThunksIn '["ssTotalActiveStake", "ssDelegations", "ssPoolParams"] SnapShot
300+
via AllowThunksIn '["ssDelegations", "ssPoolParams"] SnapShot
303301

304302
instance NFData SnapShot
305303

libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/Core/Arbitrary.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import Cardano.Ledger.Plutus.Language (Language (..), nonNativeLanguages)
8585
import Cardano.Ledger.Rewards (Reward (..), RewardType (..))
8686
import Cardano.Ledger.State
8787
import Cardano.Ledger.TxIn (TxId (..), TxIn (..))
88+
import Control.DeepSeq
8889
import Control.Monad (replicateM)
8990
import Control.Monad.Identity (Identity)
9091
import Control.Monad.Trans.Fail.String (errorFail)
@@ -696,7 +697,7 @@ instance Arbitrary SnapShot where
696697
stakePoolSnapShotFromParams poolId =
697698
mkStakePoolSnapShot ssStake ssTotalActiveStake
698699
. mkStakePoolState deposit (Map.findWithDefault mempty poolId delegationsPerStakePool)
699-
ssStakePoolsSnapShot = VMap.mapWithKey stakePoolSnapShotFromParams ssPoolParams
700+
ssStakePoolsSnapShot = force $ VMap.mapWithKey stakePoolSnapShotFromParams ssPoolParams
700701
pure SnapShot {..}
701702

702703
instance Arbitrary SnapShots where

libs/vector-map/src/Data/VMap.hs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ instance
110110
empty :: (VG.Vector kv k, VG.Vector vv v) => VMap kv vv k v
111111
empty = VMap VG.empty
112112

113-
size :: VG.Vector kv k => VMap kv vv k v -> Int
114-
size = VG.length . KV.keysVector . unVMap
113+
size :: (VG.Vector kv k, VG.Vector vv v) => VMap kv vv k v -> Int
114+
size = VG.length . unVMap
115115
{-# INLINE size #-}
116116

117117
lookup ::
@@ -230,11 +230,13 @@ fromDistinctAscListN n = VMap . KV.fromDistinctAscListN n
230230
{-# INLINE fromDistinctAscListN #-}
231231

232232
map ::
233-
(VG.Vector vv a, VG.Vector vv b) =>
233+
(VG.Vector kv k, VG.Vector vv a, VG.Vector vv b) =>
234234
(a -> b) ->
235235
VMap kv vv k a ->
236236
VMap kv vv k b
237-
map f (VMap vec) = VMap (KV.mapValsKVVector f vec)
237+
map f (VMap vec) = VMap (VG.map (\(k, v) -> (k, f v)) vec)
238+
-- TODO: benchmark and switch to this implementation when we switch to Data.Vector.Strict
239+
-- VMap (KV.mapValsKVVector f vec)
238240
{-# INLINE map #-}
239241

240242
mapMaybe ::
@@ -258,7 +260,9 @@ mapWithKey ::
258260
(k -> a -> b) ->
259261
VMap kv vv k a ->
260262
VMap kv vv k b
261-
mapWithKey f (VMap vec) = VMap (KV.mapWithKeyKVVector f vec)
263+
mapWithKey f (VMap vec) = VMap (VG.map (\(k, v) -> (k, f k v)) vec)
264+
-- TODO: benchmark and switch to this implementation when we switch to Data.Vector.Strict
265+
-- VMap (KV.mapWithKeyKVVector f vec)
262266
{-# INLINE mapWithKey #-}
263267

264268
foldMapWithKey ::

libs/vector-map/src/Data/VMap/KVVector.hs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE BangPatterns #-}
2+
{-# LANGUAGE CPP #-}
23
{-# LANGUAGE DeriveGeneric #-}
34
{-# LANGUAGE FlexibleContexts #-}
45
{-# LANGUAGE FlexibleInstances #-}
@@ -25,8 +26,8 @@ module Data.VMap.KVVector (
2526
fromDistinctAscListN,
2627
fromList,
2728
fromListN,
28-
mapValsKVVector,
29-
mapWithKeyKVVector,
29+
-- mapValsKVVector,
30+
-- mapWithKeyKVVector,
3031
memberKVVector,
3132
lookupKVVector,
3233
lookupDefaultKVVector,
@@ -58,6 +59,13 @@ import qualified Data.Vector.Storable as VS
5859
import qualified GHC.Exts as Exts
5960
import GHC.Generics
6061
import NoThunks.Class
62+
#if MIN_VERSION_vector(0, 13, 2)
63+
import qualified Data.Vector.Strict as VBS
64+
65+
instance NoThunks a => NoThunks (VBS.Vector a) where
66+
showTypeOf _ = "Boxed..StrictVector"
67+
wNoThunks ctxt = noThunksInValues ctxt . VBS.toList
68+
#endif
6169

6270
-- | Convert a __sorted__ key/value vector into a `Map.Map`
6371
toMap ::
@@ -173,26 +181,28 @@ fromAscListWithKeyN n f xs
173181
VG.create $ VGM.unsafeNew n >>= fillWithList xs >>= removeDuplicates (\k v1 v2 -> f k v2 v1)
174182
{-# INLINE fromAscListWithKeyN #-}
175183

176-
mapValsKVVector ::
177-
(VG.Vector vv a, VG.Vector vv b) =>
178-
(a -> b) ->
179-
KVVector kv vv (k, a) ->
180-
KVVector kv vv (k, b)
181-
mapValsKVVector f vec =
182-
KVVector {keysVector = keysVector vec, valsVector = VG.map f (valsVector vec)}
183-
{-# INLINE mapValsKVVector #-}
184-
185-
mapWithKeyKVVector ::
186-
(VG.Vector kv k, VG.Vector vv a, VG.Vector vv b) =>
187-
(k -> a -> b) ->
188-
KVVector kv vv (k, a) ->
189-
KVVector kv vv (k, b)
190-
mapWithKeyKVVector f KVVector {..} =
191-
KVVector
192-
{ keysVector = keysVector
193-
, valsVector = VG.imap (\i -> f (keysVector VG.! i)) valsVector
194-
}
195-
{-# INLINE mapWithKeyKVVector #-}
184+
-- These guys are too lazy for the KVVector and would require `Data.Vector.Strict`
185+
--
186+
-- mapValsKVVector ::
187+
-- (VG.Vector vv a, VG.Vector vv b) =>
188+
-- (a -> b) ->
189+
-- KVVector kv vv (k, a) ->
190+
-- KVVector kv vv (k, b)
191+
-- mapValsKVVector f vec =
192+
-- KVVector {keysVector = keysVector vec, valsVector = VG.map f (valsVector vec)}
193+
-- {-# INLINE mapValsKVVector #-}
194+
195+
-- mapWithKeyKVVector ::
196+
-- (VG.Vector kv k, VG.Vector vv a, VG.Vector vv b) =>
197+
-- (k -> a -> b) ->
198+
-- KVVector kv vv (k, a) ->
199+
-- KVVector kv vv (k, b)
200+
-- mapWithKeyKVVector f KVVector {..} =
201+
-- KVVector
202+
-- { keysVector = keysVector
203+
-- , valsVector = VG.imap (\i -> f (keysVector VG.! i)) valsVector
204+
-- }
205+
-- {-# INLINE mapWithKeyKVVector #-}
196206

197207
internKVVectorMaybe :: (VG.Vector kv k, Ord k) => k -> KVVector kv vv (k, v) -> Maybe k
198208
internKVVectorMaybe key (KVVector keys _values) =

libs/vector-map/test/Main.hs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
module Main where
22

3+
import System.IO (
4+
BufferMode (LineBuffering),
5+
hSetBuffering,
6+
hSetEncoding,
7+
stdout,
8+
utf8,
9+
)
310
import Test.Hspec
11+
import Test.Hspec.Core.Runner (ColorMode (ColorAlways), Config (..), defaultConfig, hspecWith)
412
import Test.VMap
513

614
-- ====================================================================================
715

16+
customSpecConfig :: Config
17+
customSpecConfig =
18+
defaultConfig
19+
{ configTimes = True
20+
, configColorMode = ColorAlways
21+
}
22+
23+
customSpecMainWithConfig :: Config -> Spec -> IO ()
24+
customSpecMainWithConfig conf spec = do
25+
hSetBuffering stdout LineBuffering
26+
hSetEncoding stdout utf8
27+
hspecWith conf spec
28+
29+
customSpecMain :: Spec -> IO ()
30+
customSpecMain = customSpecMainWithConfig customSpecConfig
31+
32+
-- ====================================================================================
33+
834
tests :: Spec
935
tests =
1036
describe "vector-map" $ do
1137
vMapTests
1238

1339
main :: IO ()
14-
main = hspec tests
40+
main = customSpecMain tests

libs/vector-map/test/Test/VMap.hs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
{-# LANGUAGE BangPatterns #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
13
{-# LANGUAGE TypeApplications #-}
24
{-# OPTIONS_GHC -Wno-orphans #-}
35

46
module Test.VMap where
57

8+
import Control.Exception
69
import qualified Data.List as List
710
import qualified Data.Map.Strict as Map
811
import Data.VMap as VMap
@@ -29,6 +32,11 @@ prop_AsMapTo fromVM fromM vm = fromVM vm === fromM (toMap vm)
2932
prop_AsMapFrom :: (a -> VMapT) -> (a -> MapT) -> a -> Property
3033
prop_AsMapFrom mkVMap mkMap a = toMap (mkVMap a) === mkMap a
3134

35+
data ThunkException = ThunkException
36+
deriving (Eq, Show)
37+
38+
instance Exception ThunkException
39+
3240
vMapTests :: Spec
3341
vMapTests =
3442
describe "VMap" $ do
@@ -38,6 +46,37 @@ vMapTests =
3846
prop "to/fromAscList" $ prop_Roundtrip VMap.toAscList VMap.fromAscList
3947
prop "to/fromList" $ prop_Roundtrip VMap.toAscList VMap.fromList
4048
prop "to/fromMap" $ prop_Roundtrip VMap.toMap VMap.fromMap
49+
describe "strictness" $ do
50+
let bottom :: a
51+
bottom = throw ThunkException
52+
prop "fromList" $ \xs ys -> do
53+
let
54+
vmapKeyThunk, vmapValueThunk :: VMap VB VB String String
55+
vmapKeyThunk = VMap.fromList $ xs ++ [(bottom, "value")] ++ ys
56+
vmapValueThunk = VMap.fromList $ xs ++ [("key", bottom)] ++ ys
57+
evaluate vmapKeyThunk `shouldThrow` (== ThunkException)
58+
evaluate vmapValueThunk `shouldThrow` (== ThunkException)
59+
prop "map" $ \xs ys -> do
60+
let
61+
vmapValueThunk :: VMap VB VB String String
62+
vmapValueThunk =
63+
VMap.map (\v -> if v == "bottom" then bottom else v) $
64+
VMap.fromList $
65+
xs ++ [("key", "bottom")] ++ ys
66+
evaluate vmapValueThunk `shouldThrow` (== ThunkException)
67+
prop "mapWithKey" $ \xs ys -> do
68+
let
69+
vmapKeyThunk, vmapValueThunk :: VMap VB VB String String
70+
vmapKeyThunk =
71+
VMap.mapWithKey (\k v -> if k == "bottom" then bottom else v) $
72+
VMap.fromList $
73+
xs ++ [("bottom", "value")] ++ ys
74+
vmapValueThunk =
75+
VMap.mapWithKey (\_ v -> if v == "bottom" then bottom else v) $
76+
VMap.fromList $
77+
xs ++ [("key", "bottom")] ++ ys
78+
evaluate vmapKeyThunk `shouldThrow` (== ThunkException)
79+
evaluate vmapValueThunk `shouldThrow` (== ThunkException)
4180
describe "asMap" $ do
4281
prop "mapMaybeWithKey" $ \xs f ->
4382
prop_AsMapFrom

libs/vector-map/vector-map.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ test-suite tests
8484
base,
8585
containers,
8686
hspec,
87+
hspec-core,
8788
quickcheck-classes-base,
8889
vector-map,
8990

0 commit comments

Comments
 (0)