on master help sign in
Make minor refactor/cleanup changes to array code

* Use defaultChunkSize in tests from Streamly.Internal.System.IO module

commit3c3ec6de4831a8667c415393e1dbc351ba508dbe authorHarendra Kumar <harendra@composewell.com> dateTue 10 Jan 2023, 18:10 (4 years ago) parent96a7c99
10 files changed+26−41
core/src/Streamly/Internal/Data/Array/ArrayMacros.h+6−3 core/src/Streamly/Internal/Data/Array/Mut/Type.hs+2 core/src/Streamly/Internal/Data/Stream/Chunked.hs+1−1 core/src/Streamly/Internal/System/IO.hs+4−2 core/src/Streamly/Internal/Unicode/Stream.hs+2−2 hie.yaml+4−2 test/Streamly/Test/Data/Array.hs+2−10 test/Streamly/Test/Data/Array/Common.hs+2−11 test/Streamly/Test/Data/Array/CommonImports.hs+2−1 test/Streamly/Test/FileSystem/Handle.hs+1−9
core/src/Streamly/Internal/Data/Array/ArrayMacros.h+6−3
@@ -1,5 +1,5 @@
11-------------------------------------------------------------------------------
2−-- Macros to access array pointers
2+-- Macros to access Storable pointers
33-------------------------------------------------------------------------------
44
55-- The Storable instance of () has size 0. We ensure that the size is non-zero
@@ -8,7 +8,6 @@
88--
99-- XXX Check the core to see if max can be statically eliminated. llvm can
1010-- eliminate the comparison, but not sure if GHC NCG can.
11−#define SIZE_OF(a) max 1 (sizeOf (Proxy :: Proxy a))
1211#define STORABLE_SIZE_OF(a) max 1 (sizeOf (undefined :: a))
1312
1413-- Move the pointer to ith element of specified type. Type is specified as the
@@ -27,9 +26,13 @@
2726#define PTR_INVALID(ptr,end,a) ptr `plusPtr` STORABLE_SIZE_OF(a) > end
2827
2928-------------------------------------------------------------------------------
30−-- Macros to access array indices
29+-- Macros to access array indices (using Unbox type class)
3130-------------------------------------------------------------------------------
3231
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+
3336#define INDEX_NEXT(i,a) i + SIZE_OF(a)
3437#define INDEX_PREV(i,a) i - SIZE_OF(a)
3538
core/src/Streamly/Internal/Data/Array/Mut/Type.hs+2
@@ -580,6 +580,8 @@ modify Array{..} f = liftIO $
580580 pokeWith arrContents i (f r)
581581 go (INDEX_NEXT(i,a))
582582
583+-- XXX We could specify the number of bytes to swap instead of Proxy. Need
584+-- to ensure that the memory does not overlap.
583585{-# INLINE swapArrayByteIndices #-}
584586swapArrayByteIndices ::
585587 forall a. Unbox a
core/src/Streamly/Internal/Data/Stream/Chunked.hs+1−1
@@ -170,7 +170,7 @@ interposeSuffix :: (Monad m, Unbox a)
170170-- interposeSuffix x = fromStreamD . A.unlines x . toStreamD
171171interposeSuffix x = S.interposeSuffix x A.reader
172172
173−data FlattenState s a =
173+data FlattenState s =
174174 OuterLoop s
175175 | InnerLoop s !MA.MutableByteArray !Int !Int
176176
core/src/Streamly/Internal/System/IO.hs+4−2
@@ -13,6 +13,7 @@ module Streamly.Internal.System.IO
1313 ( defaultChunkSize
1414 , arrayPayloadSize
1515 , unsafeInlineIO
16+ , byteArrayOverhead
1617 )
1718
1819where
@@ -21,7 +22,8 @@ where
2122-- Imports
2223-------------------------------------------------------------------------------
2324
24−import Foreign.Storable (Storable(sizeOf))
25+#include "MachDeps.h"
26+
2527import GHC.Base (realWorld#)
2628import GHC.IO (IO(IO))
2729
@@ -40,7 +42,7 @@ unsafeInlineIO (IO m) = case m realWorld# of (# _, r #) -> r
4042-- See https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/rts/storage/heap-objects#arrays
4143--
4244byteArrayOverhead :: Int
43−byteArrayOverhead = 2 * sizeOf (undefined :: Word)
45+byteArrayOverhead = 2 * SIZEOF_HSWORD
4446
4547-- | When we allocate a byte array of size @k@ the allocator actually allocates
4648-- memory of size @k + byteArrayOverhead@. @arrayPayloadSize n@ returns the
core/src/Streamly/Internal/Unicode/Stream.hs+2−2
@@ -684,13 +684,13 @@ decodeUtf8Lax = decodeUtf8
684684#ifndef __GHCJS__
685685{-# ANN type FlattenState Fuse #-}
686686#endif
687−data FlattenState s a
687+data FlattenState s
688688 = OuterLoop s !(Maybe (DecodeState, CodePoint))
689689 | InnerLoopDecodeInit s MutableByteArray !Int !Int
690690 | InnerLoopDecodeFirst s MutableByteArray !Int !Int Word8
691691 | InnerLoopDecoding s MutableByteArray !Int !Int
692692 !DecodeState !CodePoint
693− | YAndC !Char (FlattenState s a) -- These constructors can be
693+ | YAndC !Char (FlattenState s) -- These constructors can be
694694 -- encoded in the UTF8DecodeState
695695 -- type, I prefer to keep these
696696 -- flat even though that means
hie.yaml+4−2
@@ -84,8 +84,8 @@ cradle:
8484 cabal:
8585 - path: "./test/Streamly/Test/Data/Array.hs"
8686 component: "test:Data.Array"
87− - path: "./test/Streamly/Test/Data/Unbox.hs"
88− component: "test:Data.Unbox"
87+ - path: "./test/Streamly/Test/Data/Array/Generic.hs"
88+ component: "test:Data.Array.Generic"
8989 - path: "./test/Streamly/Test/Data/Array/Mut.hs"
9090 component: "test:Data.Array.Mut"
9191 - path: "./test/Streamly/Test/Data/Array/Stream.hs"
@@ -104,6 +104,8 @@ cradle:
104104 component: "test:Data.Parser.Chunked"
105105 - path: "./test/Streamly/Test/Data/Parser/ParserD.hs"
106106 component: "test:Data.Parser.ParserD"
107+ - path: "./test/Streamly/Test/Data/Unbox.hs"
108+ component: "test:Data.Unbox"
107109 - path: "./test/Streamly/Test/Data/Unfold.hs"
108110 component: "test:Data.Unfold"
109111 - path: "./test/Streamly/Test/FileSystem/Event.hs"
test/Streamly/Test/Data/Array.hs+2−10
@@ -14,22 +14,14 @@ import Data.Proxy (Proxy(..))
1414import Data.Word(Word8)
1515import Foreign.Storable (peek)
1616import GHC.Ptr (plusPtr)
17−import Streamly.Data.Fold (Fold)
18−import Streamly.Internal.Data.Stream (Stream)
1917import Streamly.Internal.Data.Unboxed (Unbox, sizeOf)
20−import Streamly.Test.Common (listEquals)
18+import Test.QuickCheck (chooseInt, listOf)
2119
22−import qualified Streamly.Data.Fold as Fold
2320import qualified Streamly.Internal.Data.Array as A
2421import qualified Streamly.Internal.Data.Array.Type as A
2522import qualified Streamly.Internal.Data.Array.Mut.Type as MA
26−import qualified Streamly.Internal.Data.Stream as S
2723
28−import Test.Hspec as H
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)
24+#include "Streamly/Test/Data/Array/CommonImports.hs"
3325
3426type Array = A.Array
3527
test/Streamly/Test/Data/Array/Common.hs+2−11
@@ -1,4 +1,6 @@
11
2+#include "MachDeps.h"
3+
24-- Coverage build takes too long with default number of tests
35maxTestCount :: Int
46#ifdef DEVBUILD
@@ -7,17 +9,6 @@ maxTestCount = 100
79maxTestCount = 10
810#endif
911
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−
2112maxArrLen :: Int
2213maxArrLen = defaultChunkSize * 8
2314
test/Streamly/Test/Data/Array/CommonImports.hs+2−1
@@ -6,7 +6,8 @@ import Test.Hspec as H
66
77import Streamly.Data.Fold (Fold)
88import Streamly.Internal.Data.Stream (Stream)
9+import Streamly.Internal.System.IO (defaultChunkSize)
910import Streamly.Test.Common (listEquals)
1011
1112import 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 @@
99module Streamly.Test.FileSystem.Handle (main) where
1010
1111import Data.Functor.Identity (runIdentity)
12−import Data.Proxy (Proxy(..))
1312import Data.Word (Word8)
14−import Streamly.Internal.Data.Unboxed (sizeOf)
1513import Streamly.Internal.Data.Stream (Stream)
14+import Streamly.Internal.System.IO (defaultChunkSize)
1615import System.FilePath ((</>))
1716import System.IO
1817 ( Handle
@@ -36,13 +35,6 @@ import qualified Streamly.Internal.Unicode.Stream as Unicode
3635import Test.Hspec as H
3736import Test.Hspec.QuickCheck
3837
39−allocOverhead :: Int
40−allocOverhead = 2 * sizeOf (Proxy :: Proxy Int)
41−
42−defaultChunkSize :: Int
43−defaultChunkSize = 32 * k - allocOverhead
44− where k = 1024
45−
4638maxArrLen :: Int
4739maxArrLen = defaultChunkSize * 8
4840