Skip to content

Commit dcafd78

Browse files
authored
NFData1, NFData2 instances (#992)
1 parent dfeed8b commit dcafd78

File tree

7 files changed

+75
-6
lines changed

7 files changed

+75
-6
lines changed

containers/changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969

7070
* Add `Data.Graph.flattenSCC1`. (Andreas Abel)
7171

72+
* `NFData1`, `NFData2` instances for `SCC`, `IntMap`, `Map`, `Sequence`, `Set`,
73+
`Tree` and relevant internal dependencies (David Beacham)
74+
7275
## 0.7
7376

7477
### Breaking changes

containers/src/Data/Graph.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ import Data.Foldable as F
121121
#if MIN_VERSION_base(4,18,0)
122122
import qualified Data.Foldable1 as F1
123123
#endif
124-
import Control.DeepSeq (NFData(rnf))
124+
import Control.DeepSeq (NFData(rnf),NFData1(liftRnf))
125125
import Data.Maybe
126126
import Data.Array
127127
#if USE_UNBOXED_ARRAYS
@@ -245,6 +245,11 @@ instance NFData a => NFData (SCC a) where
245245
rnf (AcyclicSCC v) = rnf v
246246
rnf (NECyclicSCC vs) = rnf vs
247247

248+
-- | @since 0.7.1
249+
instance NFData1 SCC where
250+
liftRnf rnfx (AcyclicSCC v) = rnfx v
251+
liftRnf rnfx (NECyclicSCC vs) = liftRnf rnfx vs
252+
248253
-- | @since 0.5.4
249254
instance Functor SCC where
250255
fmap f (AcyclicSCC v) = AcyclicSCC (f v)

containers/src/Data/IntMap/Internal.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ import Data.Semigroup (Semigroup((<>)))
290290
import Data.Semigroup (stimesIdempotentMonoid)
291291
import Data.Functor.Classes
292292

293-
import Control.DeepSeq (NFData(rnf))
293+
import Control.DeepSeq (NFData(rnf),NFData1(liftRnf))
294294
import Data.Bits
295295
import qualified Data.Foldable as Foldable
296296
import Data.Maybe (fromMaybe)
@@ -511,6 +511,14 @@ instance NFData a => NFData (IntMap a) where
511511
rnf (Tip _ v) = rnf v
512512
rnf (Bin _ l r) = rnf l `seq` rnf r
513513

514+
-- | @since 0.7.1
515+
instance NFData1 IntMap where
516+
liftRnf rnfx = go
517+
where
518+
go Nil = ()
519+
go (Tip _ v) = rnfx v
520+
go (Bin _ l r) = go l `seq` go r
521+
514522
#if __GLASGOW_HASKELL__
515523

516524
{--------------------------------------------------------------------

containers/src/Data/Map/Internal.hs

Lines changed: 15 additions & 1 deletion
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
@@ -4440,6 +4440,20 @@ instance (NFData k, NFData a) => NFData (Map k a) where
44404440
rnf Tip = ()
44414441
rnf (Bin _ kx x l r) = rnf kx `seq` rnf x `seq` rnf l `seq` rnf r
44424442

4443+
-- | @since 0.7.1
4444+
instance NFData k => NFData1 (Map k) where
4445+
liftRnf rnfx = go
4446+
where
4447+
go Tip = ()
4448+
go (Bin _ kx x l r) = rnf kx `seq` rnfx x `seq` go l `seq` go r
4449+
4450+
-- | @since 0.7.1
4451+
instance NFData2 Map where
4452+
liftRnf2 rnfkx rnfx = go
4453+
where
4454+
go Tip = ()
4455+
go (Bin _ kx x l r) = rnfkx kx `seq` rnfx x `seq` go l `seq` go r
4456+
44434457
{--------------------------------------------------------------------
44444458
Read
44454459
--------------------------------------------------------------------}

containers/src/Data/Sequence/Internal.hs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ import Prelude ()
208208
import Control.Applicative ((<$>), (<**>), Alternative,
209209
liftA3)
210210
import qualified Control.Applicative as Applicative
211-
import Control.DeepSeq (NFData(rnf))
211+
import Control.DeepSeq (NFData(rnf),NFData1(liftRnf))
212212
import Control.Monad (MonadPlus(..))
213213
import Data.Monoid (Monoid(..))
214214
import Data.Functor (Functor(..))
@@ -518,6 +518,10 @@ instance Traversable Seq where
518518
instance NFData a => NFData (Seq a) where
519519
rnf (Seq xs) = rnf xs
520520

521+
-- | @since 0.7.1
522+
instance NFData1 Seq where
523+
liftRnf rnfx (Seq xs) = liftRnf (liftRnf rnfx) xs
524+
521525
instance Monad Seq where
522526
return = pure
523527
xs >>= f = foldl' add empty xs
@@ -1227,6 +1231,12 @@ instance NFData a => NFData (FingerTree a) where
12271231
rnf (Single x) = rnf x
12281232
rnf (Deep _ pr m sf) = rnf pr `seq` rnf sf `seq` rnf m
12291233

1234+
-- | @since 0.7.1
1235+
instance NFData1 FingerTree where
1236+
liftRnf _ EmptyT = ()
1237+
liftRnf rnfx (Single x) = rnfx x
1238+
liftRnf rnfx (Deep _ pr m sf) = liftRnf rnfx pr `seq` liftRnf (liftRnf rnfx) m `seq` liftRnf rnfx sf
1239+
12301240
{-# INLINE deep #-}
12311241
deep :: Sized a => Digit a -> FingerTree (Node a) -> Digit a -> FingerTree a
12321242
deep pr m sf = Deep (size pr + size m + size sf) pr m sf
@@ -1329,6 +1339,13 @@ instance NFData a => NFData (Digit a) where
13291339
rnf (Three a b c) = rnf a `seq` rnf b `seq` rnf c
13301340
rnf (Four a b c d) = rnf a `seq` rnf b `seq` rnf c `seq` rnf d
13311341

1342+
-- | @since 0.7.1
1343+
instance NFData1 Digit where
1344+
liftRnf rnfx (One a) = rnfx a
1345+
liftRnf rnfx (Two a b) = rnfx a `seq` rnfx b
1346+
liftRnf rnfx (Three a b c) = rnfx a `seq` rnfx b `seq` rnfx c
1347+
liftRnf rnfx (Four a b c d) = rnfx a `seq` rnfx b `seq` rnfx c `seq` rnfx d
1348+
13321349
instance Sized a => Sized (Digit a) where
13331350
{-# INLINE size #-}
13341351
size = foldl1 (+) . fmap size
@@ -1407,6 +1424,11 @@ instance NFData a => NFData (Node a) where
14071424
rnf (Node2 _ a b) = rnf a `seq` rnf b
14081425
rnf (Node3 _ a b c) = rnf a `seq` rnf b `seq` rnf c
14091426

1427+
-- | @since 0.7.1
1428+
instance NFData1 Node where
1429+
liftRnf rnfx (Node2 _ a b) = rnfx a `seq` rnfx b
1430+
liftRnf rnfx (Node3 _ a b c) = rnfx a `seq` rnfx b `seq` rnfx c
1431+
14101432
instance Sized (Node a) where
14111433
size (Node2 v _ _) = v
14121434
size (Node3 v _ _ _) = v
@@ -1467,6 +1489,10 @@ instance Traversable Elem where
14671489
instance NFData a => NFData (Elem a) where
14681490
rnf (Elem x) = rnf x
14691491

1492+
-- | @since 0.7.1
1493+
instance NFData1 Elem where
1494+
liftRnf rnfx (Elem x) = rnfx x
1495+
14701496
-------------------------------------------------------
14711497
-- Applicative construction
14721498
-------------------------------------------------------

containers/src/Data/Set/Internal.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ import Data.Semigroup (Semigroup(..), stimesIdempotentMonoid, stimesIdempotent)
243243
import Data.Functor.Classes
244244
import Data.Functor.Identity (Identity)
245245
import qualified Data.Foldable as Foldable
246-
import Control.DeepSeq (NFData(rnf))
246+
import Control.DeepSeq (NFData(rnf),NFData1(liftRnf))
247247
import Data.List.NonEmpty (NonEmpty(..))
248248

249249
import Utils.Containers.Internal.StrictPair
@@ -1404,6 +1404,13 @@ instance NFData a => NFData (Set a) where
14041404
rnf Tip = ()
14051405
rnf (Bin _ y l r) = rnf y `seq` rnf l `seq` rnf r
14061406

1407+
-- | @since 0.7.1
1408+
instance NFData1 Set where
1409+
liftRnf rnfx = go
1410+
where
1411+
go Tip = ()
1412+
go (Bin _ y l r) = rnfx y `seq` go l `seq` go r
1413+
14071414
{--------------------------------------------------------------------
14081415
Split
14091416
--------------------------------------------------------------------}

containers/src/Data/Tree.hs

Lines changed: 7 additions & 1 deletion
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)
@@ -301,6 +301,12 @@ foldlMap1 f g = -- Use a lambda to allow inlining with two arguments
301301
instance NFData a => NFData (Tree a) where
302302
rnf (Node x ts) = rnf x `seq` rnf ts
303303

304+
-- | @since 0.7.1
305+
instance NFData1 Tree where
306+
liftRnf rnfx = go
307+
where
308+
go (Node x ts) = rnfx x `seq` liftRnf go ts
309+
304310
-- | @since 0.5.10.1
305311
instance MonadZip Tree where
306312
mzipWith f (Node a as) (Node b bs)

0 commit comments

Comments
 (0)