on master help sign in
Improve NFData instance

This would make the comparison with prim arrays fairer.

commitc48ef0464b029fe35eb9e2dfb7a84b33dd3844ce authorHarendra Kumar <harendra@composewell.com> dateWed 6 Oct 2021, 16:30 (5 years ago) parenta464be5
3 files changed+16−11
benchmark/Streamly/Benchmark/Data/Array.hs+4−7 src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+8−2 src/Streamly/Internal/Data/Array/Foreign/Type.hs+4−2
benchmark/Streamly/Benchmark/Data/Array.hs+4−7
@@ -22,7 +22,6 @@ import qualified Streamly.Benchmark.Prelude as P
2222
2323#ifdef MEMORY_ARRAY
2424import qualified GHC.Exts as GHC
25−import Foreign.Storable (Storable(..))
2625#endif
2726
2827#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
@@ -36,8 +35,7 @@ import Control.DeepSeq (deepseq)
3635#ifdef MEMORY_ARRAY
3736-- Drain a source that generates a pure array
3837{-# INLINE benchPureSrc #-}
39−benchPureSrc ::
40− (NFData a, Storable a) => String -> (Int -> Ops.Stream a) -> Benchmark
38+benchPureSrc :: String -> (Int -> Ops.Stream a) -> Benchmark
4139benchPureSrc name src = benchPure name src id
4240#endif
4341
@@ -49,11 +47,10 @@ benchIO name src f = bench name $ nfIO $
4947-- Drain a source that generates an array in the IO monad
5048{-# INLINE benchIOSrc #-}
5149benchIOSrc ::
52−#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
50+#if !defined(DATA_ARRAY_PRIM) \
51+ && !defined(DATA_ARRAY_PRIM_PINNED) \
52+ && !defined(MEMORY_ARRAY)
5353 NFData a =>
54−#endif
55−#ifdef MEMORY_ARRAY
56− Storable a =>
5754#endif
5855 String -> (Int -> IO (Ops.Stream a)) -> Benchmark
5956benchIOSrc name src = benchIO name src id
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+8−2
@@ -1144,9 +1144,15 @@ instance (Storable a, Eq a) => Eq (Array a) where
11441144 (==) = arrcmp
11451145 -- arr1 == arr2 = runIdentity $ D.eqBy (==) (toStreamD arr1) (toStreamD arr2)
11461146
1147−instance (Storable a, NFData a) => NFData (Array a) where
1147+-------------------------------------------------------------------------------
1148+-- NFData
1149+-------------------------------------------------------------------------------
1150+
1151+-- This is a Storable array, we cannot have unevaluated data in it so this is
1152+-- just a no op.
1153+instance NFData (Array a) where
11481154 {-# INLINE rnf #-}
1149− rnf = foldl' (\_ x -> rnf x) ()
1155+ rnf Array {} = ()
11501156
11511157#ifdef DEVBUILD
11521158-- Definitions using the Storable constraint from the Array type. These are to
src/Streamly/Internal/Data/Array/Foreign/Type.hs+4−2
@@ -612,9 +612,11 @@ instance (Storable a, Eq a) => Eq (Array a) where
612612 {-# INLINE (==) #-}
613613 arr1 == arr2 = runIdentity $ D.eqBy (==) (toStreamD arr1) (toStreamD arr2)
614614
615−instance (Storable a, NFData a) => NFData (Array a) where
615+-- Since this is a Storable array we cannot have unevaluated data in it so
616+-- this is just a no op.
617+instance NFData (Array a) where
616618 {-# INLINE rnf #-}
617− rnf = foldl' (\_ x -> rnf x) ()
619+ rnf Array {} = ()
618620
619621instance (Storable a, Ord a) => Ord (Array a) where
620622 {-# INLINE compare #-}