on master help sign in
Make minor refactor/cleanup changes to array code
3c3ec6dHarendra Kumar
core/src/Streamly/Internal/Data/Array/ArrayMacros.h+6−3
@@ -1,5 +1,5 @@
1-------------------------------------------------------------------------------
1-------------------------------------------------------------------------------
2−-- Macros to access array pointers
2+-- Macros to access Storable pointers
3-------------------------------------------------------------------------------
3-------------------------------------------------------------------------------
4
4
5-- The Storable instance of () has size 0. We ensure that the size is non-zero
5-- The Storable instance of () has size 0. We ensure that the size is non-zero
@@ -8,7 +8,6 @@
8--
8--
9-- XXX Check the core to see if max can be statically eliminated. llvm can
9-- XXX Check the core to see if max can be statically eliminated. llvm can
10-- eliminate the comparison, but not sure if GHC NCG can.
10-- eliminate the comparison, but not sure if GHC NCG can.
11−#define SIZE_OF(a) max 1 (sizeOf (Proxy :: Proxy a))
12#define STORABLE_SIZE_OF(a) max 1 (sizeOf (undefined :: a))
11#define STORABLE_SIZE_OF(a) max 1 (sizeOf (undefined :: a))
13
12
14-- Move the pointer to ith element of specified type. Type is specified as the
13-- Move the pointer to ith element of specified type. Type is specified as the
@@ -27,9 +26,13 @@
27#define PTR_INVALID(ptr,end,a) ptr `plusPtr` STORABLE_SIZE_OF(a) > end
26#define PTR_INVALID(ptr,end,a) ptr `plusPtr` STORABLE_SIZE_OF(a) > end
28
27
29-------------------------------------------------------------------------------
28-------------------------------------------------------------------------------
30−-- Macros to access array indices
29+-- Macros to access array indices (using Unbox type class)
31-------------------------------------------------------------------------------
30-------------------------------------------------------------------------------
32
31
32+-- This macro was originally defined as a wrapper to sizeOf so that we can
33+-- avoid a sizeOf value of 0 and make it 1.
34+#define SIZE_OF(a) sizeOf (Proxy :: Proxy a)
35+
33#define INDEX_NEXT(i,a) i + SIZE_OF(a)
36#define INDEX_NEXT(i,a) i + SIZE_OF(a)
34#define INDEX_PREV(i,a) i - SIZE_OF(a)
37#define INDEX_PREV(i,a) i - SIZE_OF(a)
35
38
core/src/Streamly/Internal/Data/Array/Mut/Type.hs+2
@@ -580,6 +580,8 @@ modify Array{..} f = liftIO $
580 pokeWith arrContents i (f r)
580 pokeWith arrContents i (f r)
581 go (INDEX_NEXT(i,a))
581 go (INDEX_NEXT(i,a))
582
582
583+-- XXX We could specify the number of bytes to swap instead of Proxy. Need
584+-- to ensure that the memory does not overlap.
583{-# INLINE swapArrayByteIndices #-}
585{-# INLINE swapArrayByteIndices #-}
584swapArrayByteIndices ::
586swapArrayByteIndices ::
585 forall a. Unbox a
587 forall a. Unbox a
core/src/Streamly/Internal/Data/Stream/Chunked.hs+1−1
@@ -170,7 +170,7 @@ interposeSuffix :: (Monad m, Unbox a)
170-- interposeSuffix x = fromStreamD . A.unlines x . toStreamD
170-- interposeSuffix x = fromStreamD . A.unlines x . toStreamD
171interposeSuffix x = S.interposeSuffix x A.reader
171interposeSuffix x = S.interposeSuffix x A.reader
172
172
173−data FlattenState s a =
173+data FlattenState s =
174 OuterLoop s
174 OuterLoop s
175 | InnerLoop s !MA.MutableByteArray !Int !Int
175 | InnerLoop s !MA.MutableByteArray !Int !Int
176
176
core/src/Streamly/Internal/System/IO.hs+4−2
@@ -13,6 +13,7 @@ module Streamly.Internal.System.IO
13 ( defaultChunkSize
13 ( defaultChunkSize
14 , arrayPayloadSize
14 , arrayPayloadSize
15 , unsafeInlineIO
15 , unsafeInlineIO
16+ , byteArrayOverhead
16 )
17 )
17
18
18where
19where
@@ -21,7 +22,8 @@ where
21-- Imports
22-- Imports
22-------------------------------------------------------------------------------
23-------------------------------------------------------------------------------
23
24
24−import Foreign.Storable (Storable(sizeOf))
25+#include "MachDeps.h"
26+
25import GHC.Base (realWorld#)
27import GHC.Base (realWorld#)
26import GHC.IO (IO(IO))
28import GHC.IO (IO(IO))
27
29
@@ -40,7 +42,7 @@ unsafeInlineIO (IO m) = case m realWorld# of (# _, r #) -> r
40-- See https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/rts/storage/heap-objects#arrays
42-- See https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/rts/storage/heap-objects#arrays
41--
43--
42byteArrayOverhead :: Int
44byteArrayOverhead :: Int
43−byteArrayOverhead = 2 * sizeOf (undefined :: Word)
45+byteArrayOverhead = 2 * SIZEOF_HSWORD
44
46
45-- | When we allocate a byte array of size @k@ the allocator actually allocates
47-- | When we allocate a byte array of size @k@ the allocator actually allocates
46-- memory of size @k + byteArrayOverhead@. @arrayPayloadSize n@ returns the
48-- memory of size @k + byteArrayOverhead@. @arrayPayloadSize n@ returns the
core/src/Streamly/Internal/Unicode/Stream.hs+2−2
@@ -684,13 +684,13 @@ decodeUtf8Lax = decodeUtf8
684#ifndef __GHCJS__
684#ifndef __GHCJS__
685{-# ANN type FlattenState Fuse #-}
685{-# ANN type FlattenState Fuse #-}
686#endif
686#endif
687−data FlattenState s a
687+data FlattenState s
688 = OuterLoop s !(Maybe (DecodeState, CodePoint))
688 = OuterLoop s !(Maybe (DecodeState, CodePoint))
689 | InnerLoopDecodeInit s MutableByteArray !Int !Int
689 | InnerLoopDecodeInit s MutableByteArray !Int !Int
690 | InnerLoopDecodeFirst s MutableByteArray !Int !Int Word8
690 | InnerLoopDecodeFirst s MutableByteArray !Int !Int Word8
691 | InnerLoopDecoding s MutableByteArray !Int !Int
691 | InnerLoopDecoding s MutableByteArray !Int !Int
692 !DecodeState !CodePoint
692 !DecodeState !CodePoint
693− | YAndC !Char (FlattenState s a) -- These constructors can be
693+ | YAndC !Char (FlattenState s) -- These constructors can be
694 -- encoded in the UTF8DecodeState
694 -- encoded in the UTF8DecodeState
695 -- type, I prefer to keep these
695 -- type, I prefer to keep these
696 -- flat even though that means
696 -- flat even though that means
hie.yaml+4−2
@@ -84,8 +84,8 @@ cradle:
84 cabal:
84 cabal:
85 - path: "./test/Streamly/Test/Data/Array.hs"
85 - path: "./test/Streamly/Test/Data/Array.hs"
86 component: "test:Data.Array"
86 component: "test:Data.Array"
87− - path: "./test/Streamly/Test/Data/Unbox.hs"
87+ - path: "./test/Streamly/Test/Data/Array/Generic.hs"
88− component: "test:Data.Unbox"
88+ component: "test:Data.Array.Generic"
89 - path: "./test/Streamly/Test/Data/Array/Mut.hs"
89 - path: "./test/Streamly/Test/Data/Array/Mut.hs"
90 component: "test:Data.Array.Mut"
90 component: "test:Data.Array.Mut"
91 - path: "./test/Streamly/Test/Data/Array/Stream.hs"
91 - path: "./test/Streamly/Test/Data/Array/Stream.hs"
@@ -104,6 +104,8 @@ cradle:
104 component: "test:Data.Parser.Chunked"
104 component: "test:Data.Parser.Chunked"
105 - path: "./test/Streamly/Test/Data/Parser/ParserD.hs"
105 - path: "./test/Streamly/Test/Data/Parser/ParserD.hs"
106 component: "test:Data.Parser.ParserD"
106 component: "test:Data.Parser.ParserD"
107+ - path: "./test/Streamly/Test/Data/Unbox.hs"
108+ component: "test:Data.Unbox"
107 - path: "./test/Streamly/Test/Data/Unfold.hs"
109 - path: "./test/Streamly/Test/Data/Unfold.hs"
108 component: "test:Data.Unfold"
110 component: "test:Data.Unfold"
109 - path: "./test/Streamly/Test/FileSystem/Event.hs"
111 - path: "./test/Streamly/Test/FileSystem/Event.hs"
test/Streamly/Test/Data/Array.hs+2−10
@@ -14,22 +14,14 @@ import Data.Proxy (Proxy(..))
14import Data.Word(Word8)
14import Data.Word(Word8)
15import Foreign.Storable (peek)
15import Foreign.Storable (peek)
16import GHC.Ptr (plusPtr)
16import GHC.Ptr (plusPtr)
17−import Streamly.Data.Fold (Fold)
18−import Streamly.Internal.Data.Stream (Stream)
19import Streamly.Internal.Data.Unboxed (Unbox, sizeOf)
17import Streamly.Internal.Data.Unboxed (Unbox, sizeOf)
20−import Streamly.Test.Common (listEquals)
18+import Test.QuickCheck (chooseInt, listOf)
21
19
22−import qualified Streamly.Data.Fold as Fold
23import qualified Streamly.Internal.Data.Array as A
20import qualified Streamly.Internal.Data.Array as A
24import qualified Streamly.Internal.Data.Array.Type as A
21import qualified Streamly.Internal.Data.Array.Type as A
25import qualified Streamly.Internal.Data.Array.Mut.Type as MA
22import qualified Streamly.Internal.Data.Array.Mut.Type as MA
26−import qualified Streamly.Internal.Data.Stream as S
27
23
28−import Test.Hspec as H
24+#include "Streamly/Test/Data/Array/CommonImports.hs"
29−import Test.Hspec.QuickCheck
30−import Test.QuickCheck (Property, chooseInt, listOf
31− , forAll, Gen, vectorOf, arbitrary, choose)
32−import Test.QuickCheck.Monadic (monadicIO, assert, run)
33
25
34type Array = A.Array
26type Array = A.Array
35
27
test/Streamly/Test/Data/Array/Common.hs+2−11
@@ -1,4 +1,6 @@
1
1
2+#include "MachDeps.h"
3+
2-- Coverage build takes too long with default number of tests
4-- Coverage build takes too long with default number of tests
3maxTestCount :: Int
5maxTestCount :: Int
4#ifdef DEVBUILD
6#ifdef DEVBUILD
@@ -7,17 +9,6 @@ maxTestCount = 100
7maxTestCount = 10
9maxTestCount = 10
8#endif
10#endif
9
11
10−allocOverhead :: Int
11−allocOverhead = 16
12−
13−-- XXX this should be in sync with the defaultChunkSize in Array code, or we
14−-- should expose that and use that. For fast testing we could reduce the
15−-- defaultChunkSize under CPP conditionals.
16−--
17−defaultChunkSize :: Int
18−defaultChunkSize = 32 * k - allocOverhead
19− where k = 1024
20−
21maxArrLen :: Int
12maxArrLen :: Int
22maxArrLen = defaultChunkSize * 8
13maxArrLen = defaultChunkSize * 8
23
14
test/Streamly/Test/Data/Array/CommonImports.hs+2−1
@@ -6,7 +6,8 @@ import Test.Hspec as H
6
6
7import Streamly.Data.Fold (Fold)
7import Streamly.Data.Fold (Fold)
8import Streamly.Internal.Data.Stream (Stream)
8import Streamly.Internal.Data.Stream (Stream)
9+import Streamly.Internal.System.IO (defaultChunkSize)
9import Streamly.Test.Common (listEquals)
10import Streamly.Test.Common (listEquals)
10
11
11import qualified Streamly.Data.Fold as Fold
12import qualified Streamly.Data.Fold as Fold
12−import qualified Streamly.Prelude as S
13+import qualified Streamly.Internal.Data.Stream as S
test/Streamly/Test/FileSystem/Handle.hs+1−9
@@ -9,10 +9,9 @@
9module Streamly.Test.FileSystem.Handle (main) where
9module Streamly.Test.FileSystem.Handle (main) where
10
10
11import Data.Functor.Identity (runIdentity)
11import Data.Functor.Identity (runIdentity)
12−import Data.Proxy (Proxy(..))
13import Data.Word (Word8)
12import Data.Word (Word8)
14−import Streamly.Internal.Data.Unboxed (sizeOf)
15import Streamly.Internal.Data.Stream (Stream)
13import Streamly.Internal.Data.Stream (Stream)
14+import Streamly.Internal.System.IO (defaultChunkSize)
16import System.FilePath ((</>))
15import System.FilePath ((</>))
17import System.IO
16import System.IO
18 ( Handle
17 ( Handle
@@ -36,13 +35,6 @@ import qualified Streamly.Internal.Unicode.Stream as Unicode
36import Test.Hspec as H
35import Test.Hspec as H
37import Test.Hspec.QuickCheck
36import Test.Hspec.QuickCheck
38
37
39−allocOverhead :: Int
40−allocOverhead = 2 * sizeOf (Proxy :: Proxy Int)
41−
42−defaultChunkSize :: Int
43−defaultChunkSize = 32 * k - allocOverhead
44− where k = 1024
45−
46maxArrLen :: Int
38maxArrLen :: Int
47maxArrLen = defaultChunkSize * 8
39maxArrLen = defaultChunkSize * 8
48
40