Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 57cc685

Browse files
author
David Beacham
committedAug 20, 2024·
NFData1,NFData2 instances (#767)
1 parent fbade40 commit 57cc685

File tree

7 files changed

+66
-6
lines changed

7 files changed

+66
-6
lines changed
 

‎containers/changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545

4646
* Add `Data.Graph.flattenSCC1`. (Andreas Abel)
4747

48+
* `NFData1`, `NFData2` instances for `Data.Graph`, `Data.IntMap`,
49+
`Data.IntSet`, `Data.Map`, `Data.Sequence`, `Data.Set`, `Data.Tree` and
50+
relevant internal dependencies (David Beacham)
51+
4852
## 0.7
4953

5054
### Breaking changes

‎containers/src/Data/Graph.hs

+6-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ import Data.Foldable as F
118118
#if MIN_VERSION_base(4,18,0)
119119
import qualified Data.Foldable1 as F1
120120
#endif
121-
import Control.DeepSeq (NFData(rnf))
121+
import Control.DeepSeq (NFData(rnf),NFData1(liftRnf))
122122
import Data.Maybe
123123
import Data.Array
124124
#if USE_UNBOXED_ARRAYS
@@ -235,6 +235,11 @@ instance NFData a => NFData (SCC a) where
235235
rnf (AcyclicSCC v) = rnf v
236236
rnf (NECyclicSCC vs) = rnf vs
237237

238+
-- | @since 0.7.1
239+
instance NFData1 SCC where
240+
liftRnf rnfx (AcyclicSCC v) = rnfx v
241+
liftRnf rnfx (NECyclicSCC vs) = liftRnf rnfx vs
242+
238243
-- | @since 0.5.4
239244
instance Functor SCC where
240245
fmap f (AcyclicSCC v) = AcyclicSCC (f v)

‎containers/src/Data/IntMap/Internal.hs

+7-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ import Data.Semigroup (Semigroup((<>)))
294294
import Data.Semigroup (stimesIdempotentMonoid)
295295
import Data.Functor.Classes
296296

297-
import Control.DeepSeq (NFData(rnf))
297+
import Control.DeepSeq (NFData(rnf),NFData1(liftRnf))
298298
import Data.Bits
299299
import qualified Data.Foldable as Foldable
300300
import Data.Maybe (fromMaybe)
@@ -518,6 +518,12 @@ instance NFData a => NFData (IntMap a) where
518518
rnf (Tip _ v) = rnf v
519519
rnf (Bin _ l r) = rnf l `seq` rnf r
520520

521+
-- | @since 0.7.1
522+
instance NFData1 IntMap where
523+
liftRnf _ Nil = ()
524+
liftRnf rnfx (Tip _ v) = rnfx v
525+
liftRnf rnfx (Bin _ l r) = liftRnf rnfx l `seq` liftRnf rnfx r
526+
521527
#if __GLASGOW_HASKELL__
522528

523529
{--------------------------------------------------------------------

‎containers/src/Data/Map/Internal.hs

+11-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ import Data.Semigroup (Arg(..), Semigroup(stimes))
387387
import Data.Semigroup (Semigroup((<>)))
388388
#endif
389389
import Control.Applicative (Const (..))
390-
import Control.DeepSeq (NFData(rnf))
390+
import Control.DeepSeq (NFData(rnf),NFData1(liftRnf),NFData2(liftRnf2))
391391
import Data.Bits (shiftL, shiftR)
392392
import qualified Data.Foldable as Foldable
393393
import Data.Bifoldable
@@ -4413,6 +4413,16 @@ instance (NFData k, NFData a) => NFData (Map k a) where
44134413
rnf Tip = ()
44144414
rnf (Bin _ kx x l r) = rnf kx `seq` rnf x `seq` rnf l `seq` rnf r
44154415

4416+
-- | @since 0.7.1
4417+
instance NFData k => NFData1 (Map k) where
4418+
liftRnf _ Tip = ()
4419+
liftRnf rnfx (Bin _ kx x l r) = rnf kx `seq` rnfx x `seq` liftRnf rnfx l `seq` liftRnf rnfx r
4420+
4421+
-- | @since 0.7.1
4422+
instance NFData2 Map where
4423+
liftRnf2 _ _ Tip = ()
4424+
liftRnf2 rnfkx rnfx (Bin _ kx x l r) = rnfkx kx `seq` rnfx x `seq` liftRnf2 rnfkx rnfx l `seq` liftRnf2 rnfkx rnfx r
4425+
44164426
{--------------------------------------------------------------------
44174427
Read
44184428
--------------------------------------------------------------------}

‎containers/src/Data/Sequence/Internal.hs

+27-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ import Prelude ()
207207
import Control.Applicative ((<$>), (<**>), Alternative,
208208
liftA3)
209209
import qualified Control.Applicative as Applicative
210-
import Control.DeepSeq (NFData(rnf))
210+
import Control.DeepSeq (NFData(rnf),NFData1(liftRnf))
211211
import Control.Monad (MonadPlus(..))
212212
import Data.Monoid (Monoid(..))
213213
import Data.Functor (Functor(..))
@@ -504,6 +504,10 @@ instance Traversable Seq where
504504
instance NFData a => NFData (Seq a) where
505505
rnf (Seq xs) = rnf xs
506506

507+
-- | @since 0.7.1
508+
instance NFData1 Seq where
509+
liftRnf rnfx (Seq xs) = liftRnf (liftRnf rnfx) xs
510+
507511
instance Monad Seq where
508512
return = pure
509513
xs >>= f = foldl' add empty xs
@@ -1170,6 +1174,12 @@ instance NFData a => NFData (FingerTree a) where
11701174
rnf (Single x) = rnf x
11711175
rnf (Deep _ pr m sf) = rnf pr `seq` rnf sf `seq` rnf m
11721176

1177+
-- | @since 0.7.1
1178+
instance NFData1 FingerTree where
1179+
liftRnf _ EmptyT = ()
1180+
liftRnf rnfx (Single x) = rnfx x
1181+
liftRnf rnfx (Deep _ pr m sf) = liftRnf rnfx pr `seq` liftRnf (liftRnf rnfx) m `seq` liftRnf rnfx sf
1182+
11731183
{-# INLINE deep #-}
11741184
deep :: Sized a => Digit a -> FingerTree (Node a) -> Digit a -> FingerTree a
11751185
deep pr m sf = Deep (size pr + size m + size sf) pr m sf
@@ -1272,6 +1282,13 @@ instance NFData a => NFData (Digit a) where
12721282
rnf (Three a b c) = rnf a `seq` rnf b `seq` rnf c
12731283
rnf (Four a b c d) = rnf a `seq` rnf b `seq` rnf c `seq` rnf d
12741284

1285+
-- | @since 0.7.1
1286+
instance NFData1 Digit where
1287+
liftRnf rnfx (One a) = rnfx a
1288+
liftRnf rnfx (Two a b) = rnfx a `seq` rnfx b
1289+
liftRnf rnfx (Three a b c) = rnfx a `seq` rnfx b `seq` rnfx c
1290+
liftRnf rnfx (Four a b c d) = rnfx a `seq` rnfx b `seq` rnfx c `seq` rnfx d
1291+
12751292
instance Sized a => Sized (Digit a) where
12761293
{-# INLINE size #-}
12771294
size = foldl1 (+) . fmap size
@@ -1350,6 +1367,11 @@ instance NFData a => NFData (Node a) where
13501367
rnf (Node2 _ a b) = rnf a `seq` rnf b
13511368
rnf (Node3 _ a b c) = rnf a `seq` rnf b `seq` rnf c
13521369

1370+
-- | @since 0.7.1
1371+
instance NFData1 Node where
1372+
liftRnf rnfx (Node2 _ a b) = rnfx a `seq` rnfx b
1373+
liftRnf rnfx (Node3 _ a b c) = rnfx a `seq` rnfx b `seq` rnfx c
1374+
13531375
instance Sized (Node a) where
13541376
size (Node2 v _ _) = v
13551377
size (Node3 v _ _ _) = v
@@ -1410,6 +1432,10 @@ instance Traversable Elem where
14101432
instance NFData a => NFData (Elem a) where
14111433
rnf (Elem x) = rnf x
14121434

1435+
-- | @since 0.7.1
1436+
instance NFData1 Elem where
1437+
liftRnf rnfx (Elem x) = rnfx x
1438+
14131439
-------------------------------------------------------
14141440
-- Applicative construction
14151441
-------------------------------------------------------

‎containers/src/Data/Set/Internal.hs

+6-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ import Data.Semigroup (stimesIdempotentMonoid, stimesIdempotent)
248248
import Data.Functor.Classes
249249
import Data.Functor.Identity (Identity)
250250
import qualified Data.Foldable as Foldable
251-
import Control.DeepSeq (NFData(rnf))
251+
import Control.DeepSeq (NFData(rnf),NFData1(liftRnf))
252252

253253
import Utils.Containers.Internal.StrictPair
254254
import Utils.Containers.Internal.PtrEquality
@@ -1334,6 +1334,11 @@ instance NFData a => NFData (Set a) where
13341334
rnf Tip = ()
13351335
rnf (Bin _ y l r) = rnf y `seq` rnf l `seq` rnf r
13361336

1337+
-- | @since 0.7.1
1338+
instance NFData1 Set where
1339+
liftRnf _ Tip = ()
1340+
liftRnf rnfx (Bin _ y l r) = rnfx y `seq` liftRnf rnfx l `seq` liftRnf rnfx r
1341+
13371342
{--------------------------------------------------------------------
13381343
Split
13391344
--------------------------------------------------------------------}

‎containers/src/Data/Tree.hs

+5-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import Control.Monad (liftM)
6161
import Control.Monad.Fix (MonadFix (..), fix)
6262
import Data.Sequence (Seq, empty, singleton, (<|), (|>), fromList,
6363
ViewL(..), ViewR(..), viewl, viewr)
64-
import Control.DeepSeq (NFData(rnf))
64+
import Control.DeepSeq (NFData(rnf),NFData1(liftRnf))
6565

6666
#ifdef __GLASGOW_HASKELL__
6767
import Data.Data (Data)
@@ -300,6 +300,10 @@ foldlMap1 f g = -- Use a lambda to allow inlining with two arguments
300300
instance NFData a => NFData (Tree a) where
301301
rnf (Node x ts) = rnf x `seq` rnf ts
302302

303+
-- | @since 0.7.1
304+
instance NFData1 Tree where
305+
liftRnf rnfx (Node x ts) = rnfx x `seq` liftRnf (liftRnf rnfx) ts
306+
303307
-- | @since 0.5.10.1
304308
instance MonadZip Tree where
305309
mzipWith f (Node a as) (Node b bs)

0 commit comments

Comments
 (0)
Please sign in to comment.