Skip to content
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

add more NFData instances #305

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# This Travis job script has been generated by a script via
#
# haskell-ci '--benchmarks-jobs= >=7.10' '--installed=-containers' '--installed=-binary' 'unordered-containers.cabal' '--branches=master'
# haskell-ci '--benchmarks-jobs= >=7.10' '--installed=-containers' '--installed=-binary' '--installed=-bytestring' '--installed=-deepseq' '--installed=-process' '--installed=-time' '--installed=-unix' 'unordered-containers.cabal' '--branches=master'
#
# To regenerate the script (for example after adjusting tested-with) run
#
# haskell-ci regenerate
#
# For more information, see https://github.com/haskell-CI/haskell-ci
#
# version: 0.10.2
# version: 0.10.3
#
version: ~> 1.0
language: c
Expand Down Expand Up @@ -112,7 +112,7 @@ install:
- if [ $HCNUMVER -ge 80200 ] ; then echo 'package unordered-containers' >> cabal.project ; fi
- "if [ $HCNUMVER -ge 80200 ] ; then echo ' ghc-options: -Werror=missing-methods' >> cabal.project ; fi"
- |
- "for pkg in $($HCPKG list --simple-output); do echo $pkg | sed 's/-[^-]*$//' | (grep -vE -- '^(binary|containers|unordered-containers)$' || true) | sed 's/^/constraints: /' | sed 's/$/ installed/' >> cabal.project.local; done"
- "for pkg in $($HCPKG list --simple-output); do echo $pkg | sed 's/-[^-]*$//' | (grep -vE -- '^(binary|bytestring|containers|deepseq|process|time|unix|unordered-containers)$' || true) | sed 's/^/constraints: /' | sed 's/$/ installed/' >> cabal.project.local; done"
- cat cabal.project || true
- cat cabal.project.local || true
- if [ -f "./configure.ac" ]; then (cd "." && autoreconf -i); fi
Expand All @@ -139,7 +139,7 @@ script:
- if [ $HCNUMVER -ge 80200 ] ; then echo 'package unordered-containers' >> cabal.project ; fi
- "if [ $HCNUMVER -ge 80200 ] ; then echo ' ghc-options: -Werror=missing-methods' >> cabal.project ; fi"
- |
- "for pkg in $($HCPKG list --simple-output); do echo $pkg | sed 's/-[^-]*$//' | (grep -vE -- '^(binary|containers|unordered-containers)$' || true) | sed 's/^/constraints: /' | sed 's/$/ installed/' >> cabal.project.local; done"
- "for pkg in $($HCPKG list --simple-output); do echo $pkg | sed 's/-[^-]*$//' | (grep -vE -- '^(binary|bytestring|containers|deepseq|process|time|unix|unordered-containers)$' || true) | sed 's/^/constraints: /' | sed 's/$/ installed/' >> cabal.project.local; done"
- cat cabal.project || true
- cat cabal.project.local || true
# Building...
Expand All @@ -158,5 +158,5 @@ script:
- rm -f cabal.project.local
- ${CABAL} v2-build $WITHCOMPILER --disable-tests --disable-benchmarks all

# REGENDATA ("0.10.2",["--benchmarks-jobs= >=7.10","--installed=-containers","--installed=-binary","unordered-containers.cabal","--branches=master"])
# REGENDATA ("0.10.3",["--benchmarks-jobs= >=7.10","--installed=-containers","--installed=-binary","--installed=-bytestring","--installed=-deepseq","--installed=-process","--installed=-time","--installed=-unix","unordered-containers.cabal","--branches=master"])
# EOF
18 changes: 17 additions & 1 deletion Data/HashMap/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ import Data.Word (Word)
#if __GLASGOW_HASKELL__ >= 711
import Data.Semigroup (Semigroup((<>)))
#endif
import Control.DeepSeq (NFData(rnf))
import Control.DeepSeq (NFData(rnf), NFData2(liftRnf2), NFData1(liftRnf))
import Control.Monad.ST (ST)
import Data.Bits ((.&.), (.|.), complement, popCount, unsafeShiftL, unsafeShiftR)
import Data.Data hiding (Typeable)
Expand Down Expand Up @@ -200,6 +200,12 @@ data Leaf k v = L !k v
instance (NFData k, NFData v) => NFData (Leaf k v) where
rnf (L k v) = rnf k `seq` rnf v

instance NFData k => NFData1 (Leaf k) where
liftRnf rnf2 = liftRnf2 rnf rnf2

instance NFData2 Leaf where
liftRnf2 rnf1 rnf2 (L k v) = rnf1 k `seq` rnf2 v

-- Invariant: The length of the 1st argument to 'Full' is
-- 2^bitsPerSubkey

Expand All @@ -222,6 +228,16 @@ instance (NFData k, NFData v) => NFData (HashMap k v) where
rnf (Full ary) = rnf ary
rnf (Collision _ ary) = rnf ary

instance NFData k => NFData1 (HashMap k) where
liftRnf rnf2 = liftRnf2 rnf rnf2

instance NFData2 HashMap where
liftRnf2 _ _ Empty = ()
liftRnf2 rnf1 rnf2 (BitmapIndexed _ ary) = liftRnf (liftRnf2 rnf1 rnf2) ary
liftRnf2 rnf1 rnf2 (Leaf _ l) = liftRnf2 rnf1 rnf2 l
liftRnf2 rnf1 rnf2 (Full ary) = liftRnf (liftRnf2 rnf1 rnf2) ary
liftRnf2 rnf1 rnf2 (Collision _ ary) = liftRnf (liftRnf2 rnf1 rnf2) ary

instance Functor (HashMap k) where
fmap = map

Expand Down
13 changes: 13 additions & 0 deletions Data/HashMap/Internal/Array.hs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ rnfArray ary0 = go ary0 n0 0
-- relevant rnf is strict, or in case it actually isn't.
{-# INLINE rnfArray #-}

instance NFData1 Array where
liftRnf = liftRnfArray

liftRnfArray :: (a -> ()) -> Array a -> ()
liftRnfArray rnf0 ary0 = go ary0 n0 0
where
n0 = length ary0
go !ary !n !i
| i >= n = ()
| (# x #) <- index# ary i
= rnf0 x `seq` go ary n (i+1)
{-# INLINE liftRnfArray #-}

-- | Create a new mutable array of specified size, in the specified
-- state thread, with each element containing the specified initial
-- value.
Expand Down
5 changes: 4 additions & 1 deletion Data/HashSet/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module Data.HashSet.Internal
, keysSet
) where

import Control.DeepSeq (NFData(..))
import Control.DeepSeq (NFData(..), NFData1(..), NFData2 (liftRnf2))
import Data.Data hiding (Typeable)
import Data.HashMap.Internal
( HashMap, foldMapWithKey, foldlWithKey, foldrWithKey
Expand Down Expand Up @@ -138,6 +138,9 @@ instance (NFData a) => NFData (HashSet a) where
rnf = rnf . asMap
{-# INLINE rnf #-}

instance NFData1 HashSet where
liftRnf rnf1 = liftRnf2 rnf1 rnf . asMap

-- | Note that, in the presence of hash collisions, equal @HashSet@s may
-- behave differently, i.e. substitutivity may be violated:
--
Expand Down
4 changes: 2 additions & 2 deletions unordered-containers.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ library

build-depends:
base >= 4.7 && < 5,
deepseq >= 1.1,
deepseq >= 1.4.3,
hashable >= 1.0.1.1 && < 1.4

default-language: Haskell2010
Expand Down Expand Up @@ -197,7 +197,7 @@ benchmark benchmarks
bytestring,
containers,
gauge >= 0.2.5 && < 0.3,
deepseq >= 1.4,
deepseq,
hashable >= 1.0.1.1,
hashmap,
mtl,
Expand Down