on master help sign in
rename Streamly.Array to Streamly.Mem.Array
commit8bcbc77e625a99e1eec79f8a6caeab4140b56db6 authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 15 May 2019, 19:22 (7 years ago) parent25cda4e
12 files changed+67−59
benchmark/Array.hs+1−1 benchmark/ArrayOps.hs+3−3 benchmark/FileIO.hs+1−1 benchmark/Linear.hs+2−2 src/Streamly/FileSystem/File.hs+9−9 src/Streamly/Fold.hs+2−2 src/Streamly/Array.hs → src/Streamly/Mem/Array.hs+5−6 src/Streamly/Array/Types.hs → src/Streamly/Mem/Array/Types.hs+2−2 src/Streamly/RingBuffer.hs → src/Streamly/Mem/Ring.hs+21−21 src/Streamly/Streams/StreamD.hs+5−4 streamly.cabal+14−5 test/Arrays.hs+2−3
benchmark/Array.hs+1−1
@@ -13,7 +13,7 @@ import System.Random (randomRIO)
1313import qualified GHC.Exts as GHC
1414
1515import qualified ArrayOps as Ops
16−import qualified Streamly.Array as A
16+import qualified Streamly.Mem.Array as A
1717import qualified Streamly.Prelude as S
1818
1919import Gauge
benchmark/ArrayOps.hs+3−3
@@ -25,9 +25,9 @@ import qualified GHC.Exts as GHC
2525-- import Control.DeepSeq (NFData)
2626-- import GHC.Generics (Generic)
2727
28−import qualified Streamly as S hiding (foldMapWith, runStream)
29−import qualified Streamly.Prelude as S
30−import qualified Streamly.Array as A
28+import qualified Streamly as S hiding (foldMapWith, runStream)
29+import qualified Streamly.Mem.Array as A
30+import qualified Streamly.Prelude as S
3131
3232value, maxValue :: Int
3333#ifdef LINEAR_ASYNC
benchmark/FileIO.hs+1−1
@@ -15,8 +15,8 @@ import System.Process.Typed (shell, runProcess_)
1515import Data.IORef
1616import Gauge
1717
18−import qualified Streamly.Array as A
1918import qualified Streamly.FileSystem.File as File
19+import qualified Streamly.Mem.Array as A
2020import qualified Streamly.Prelude as S
2121
2222#ifdef DEVBUILD
benchmark/Linear.hs+2−2
@@ -15,8 +15,8 @@ import qualified GHC.Exts as GHC
1515import qualified LinearOps as Ops
1616
1717import Streamly
18−import qualified Streamly.Array as A
19−import qualified Streamly.Fold as FL
18+import qualified Streamly.Fold as FL
19+import qualified Streamly.Mem.Array as A
2020import qualified Streamly.Prelude as S
2121-- import qualified Streamly.Sink as Sink
2222import Gauge
src/Streamly/FileSystem/File.hs+9−9
@@ -4,7 +4,7 @@
44{-# LANGUAGE RecordWildCards #-}
55{-# LANGUAGE UnboxedTuples #-}
66
7−#include "../Streams/inline.hs"
7+#include "inline.hs"
88
99-- |
1010-- Module : Streamly.FileSystem.File
@@ -16,11 +16,11 @@
1616-- Portability : GHC
1717--
1818-- Read and write streams and arrays to and from files. File IO APIs are quite
19−-- similar to "Streamly.Array" read write APIs. In that regard, arrays can be
20−-- considered as in-memory files or files can be considered as on-disk arrays.
21−-- IO APIs are divided into two categories, sequential streaming IO APIs and
22−-- random access IO APIs. Control over the file reading and writing behavior
23−-- in terms of buffering, encoding, decoding is in the hands of the
19+-- similar to "Streamly.Mem.Array" read write APIs. In that regard, arrays can
20+-- be considered as in-memory files or files can be considered as on-disk
21+-- arrays. IO APIs are divided into two categories, sequential streaming IO
22+-- APIs and random access IO APIs. Control over the file reading and writing
23+-- behavior in terms of buffering, encoding, decoding is in the hands of the
2424-- programmer, the 'TextEncoding', 'NewLineMode', and 'Buffering' options of
2525-- the underlying handle provided by GHC are not needed and ignored.
2626--
@@ -108,14 +108,14 @@ import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
108108import System.IO (Handle, hGetBufSome, hPutBuf)
109109import Prelude hiding (read)
110110
111−import Streamly.Array.Types (Array(..))
111+import Streamly.Mem.Array.Types (Array(..))
112112import Streamly.Streams.Serial (SerialT)
113113import Streamly.Streams.StreamK.Type (IsStream, mkStream)
114114-- import Streamly.Fold (Fold)
115115-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
116116
117−import qualified Streamly.Array as A
118−import qualified Streamly.Array.Types as A hiding (flattenArrays)
117+import qualified Streamly.Mem.Array as A
118+import qualified Streamly.Mem.Array.Types as A hiding (flattenArrays)
119119import qualified Streamly.Prelude as S
120120
121121-------------------------------------------------------------------------------
src/Streamly/Fold.hs+2−2
@@ -317,14 +317,14 @@ import Prelude
317317import qualified Data.Map.Strict as Map
318318
319319import Streamly (MonadAsync, parallel)
320−import Streamly.Array (Array)
321320import Streamly.Fold.Types (Fold(..))
321+import Streamly.Mem.Array (Array)
322322import Streamly.Streams.Serial (SerialT)
323323import Streamly.Streams.StreamK (IsStream())
324324
325325import Streamly.Strict
326326
327−import qualified Streamly.Array as A
327+import qualified Streamly.Mem.Array as A
328328import qualified Streamly.Prelude as S
329329import qualified Streamly.Streams.StreamD as D
330330import qualified Streamly.Streams.StreamK as K
src/Streamly/Array.hs → src/Streamly/Mem/Array.hsrenamed+5−6
@@ -5,10 +5,10 @@
55{-# LANGUAGE UnboxedTuples #-}
66{-# LANGUAGE ScopedTypeVariables #-}
77
8−#include "Streams/inline.hs"
8+#include "inline.hs"
99
1010-- |
11−-- Module : Streamly.Array
11+-- Module : Streamly.Mem.Array
1212-- Copyright : (c) 2019 Harendra Kumar
1313--
1414-- License : BSD3
@@ -76,7 +76,7 @@
7676-- arbitrarily large amounts of memory but still using small chunks of
7777-- contiguous memory.
7878
79−module Streamly.Array
79+module Streamly.Mem.Array
8080 (
8181 Array
8282
@@ -137,12 +137,11 @@ import Foreign.Ptr (minusPtr, plusPtr, castPtr)
137137import Foreign.Storable (Storable(..))
138138import Prelude hiding (length, null, last, map, (!!), read)
139139
140−import Streamly.Array.Types hiding (flattenArrays, newArray)
140+import Streamly.Mem.Array.Types hiding (flattenArrays, newArray)
141141import Streamly.Streams.Serial (SerialT)
142142import Streamly.Streams.StreamK.Type (IsStream)
143143
144−
145−import qualified Streamly.Array.Types as A
144+import qualified Streamly.Mem.Array.Types as A
146145import qualified Streamly.Prelude as S
147146import qualified Streamly.Streams.StreamD as D
148147
src/Streamly/Array/Types.hs → src/Streamly/Mem/Array/Types.hsrenamed+2−2
@@ -10,7 +10,7 @@
1010#include "inline.hs"
1111
1212-- |
13−-- Module : Streamly.Array
13+-- Module : Streamly.Mem.Array.Types
1414-- Copyright : (c) 2019 Composewell Technologies
1515--
1616-- License : BSD3
@@ -18,7 +18,7 @@
1818-- Stability : experimental
1919-- Portability : GHC
2020--
21−module Streamly.Array.Types
21+module Streamly.Mem.Array.Types
2222 (
2323 Array (..)
2424
src/Streamly/RingBuffer.hs → src/Streamly/Mem/Ring.hsrenamed+21−21
@@ -4,7 +4,7 @@
44{-# LANGUAGE ExistentialQuantification #-}
55
66-- |
7−-- Module : Streamly.RingBuffer
7+-- Module : Streamly.Mem.Ring
88-- Copyright : (c) 2019 Composewell Technologies
99-- License : BSD3
1010-- Maintainer : harendra.kumar@gmail.com
@@ -12,8 +12,8 @@
1212-- Portability : GHC
1313--
1414
15−module Streamly.RingBuffer
16− ( RingBuffer(..)
15+module Streamly.Mem.Ring
16+ ( Ring(..)
1717
1818 -- * Construction
1919 , new
@@ -40,7 +40,7 @@ import GHC.ForeignPtr (mallocPlainForeignPtrAlignedBytes)
4040import GHC.Ptr (Ptr(..))
4141import Prelude hiding (length, concat)
4242
43−import qualified Streamly.Array.Types as A
43+import qualified Streamly.Mem.Array.Types as A
4444
4545-- | A ring buffer is a mutable array of fixed size. Initially the array is
4646-- empty, with ringStart pointing at the start of allocated memory. We call the
@@ -54,7 +54,7 @@ import qualified Streamly.Array.Types as A
5454-- When using it we should keep in mind that a ringBuffer is a mutable data
5555-- structure. We should not leak out references to it for immutable use.
5656--
57−data RingBuffer a = RingBuffer
57+data Ring a = Ring
5858 { ringStart :: !(ForeignPtr a) -- first address
5959 , ringBound :: !(Ptr a) -- first address beyond allocated memory
6060 }
@@ -62,12 +62,12 @@ data RingBuffer a = RingBuffer
6262-- | Create a new ringbuffer and return the ring buffer and the ringHead.
6363-- Returns the ring and the ringHead, the ringHead is same as ringStart.
6464{-# INLINE new #-}
65−new :: forall a. Storable a => Int -> IO (RingBuffer a, Ptr a)
65+new :: forall a. Storable a => Int -> IO (Ring a, Ptr a)
6666new count = do
6767 let size = count * sizeOf (undefined :: a)
6868 fptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: a))
6969 let p = unsafeForeignPtrToPtr fptr
70− return $ (RingBuffer
70+ return $ (Ring
7171 { ringStart = fptr
7272 , ringBound = p `plusPtr` size
7373 }, p)
@@ -75,8 +75,8 @@ new count = do
7575-- | Advance the ringHead by 1 item, wrap around if we hit the end of the
7676-- array.
7777{-# INLINE advance #-}
78−advance :: forall a. Storable a => RingBuffer a -> Ptr a -> Ptr a
79−advance RingBuffer{..} ringHead =
78+advance :: forall a. Storable a => Ring a -> Ptr a -> Ptr a
79+advance Ring{..} ringHead =
8080 let ptr = ringHead `plusPtr` sizeOf (undefined :: a)
8181 in if ptr < ringBound
8282 then ptr
@@ -84,10 +84,10 @@ advance RingBuffer{..} ringHead =
8484
8585-- | Insert an item at the head of the ring, when the ring is full this
8686-- replaces the oldest item in the ring with the new item. This is unsafe
87−-- beause ringHead supplied is not verified to be within the RingBuffer. Also,
87+-- beause ringHead supplied is not verified to be within the Ring. Also,
8888-- the ringStart foreignPtr must be guaranteed to be alive by the caller.
8989{-# INLINE unsafeInsert #-}
90−unsafeInsert :: Storable a => RingBuffer a -> Ptr a -> a -> IO (Ptr a)
90+unsafeInsert :: Storable a => Ring a -> Ptr a -> a -> IO (Ptr a)
9191unsafeInsert rb ringHead newVal = do
9292 poke ringHead newVal
9393 -- touchForeignPtr (ringStart rb)
@@ -99,8 +99,8 @@ unsafeInsert rb ringHead newVal = do
9999-- the ring buffer. This is unsafe because the ringHead Ptr is not checked to
100100-- be in range.
101101{-# INLINE unsafeEqArrayN #-}
102−unsafeEqArrayN :: RingBuffer a -> Ptr a -> A.Array a -> Int -> Bool
103−unsafeEqArrayN RingBuffer{..} rh A.Array{..} n =
102+unsafeEqArrayN :: Ring a -> Ptr a -> A.Array a -> Int -> Bool
103+unsafeEqArrayN Ring{..} rh A.Array{..} n =
104104 let !res = A.unsafeInlineIO $ do
105105 let rs = unsafeForeignPtrToPtr ringStart
106106 let as = unsafeForeignPtrToPtr aStart
@@ -125,8 +125,8 @@ unsafeEqArrayN RingBuffer{..} rh A.Array{..} n =
125125-- supplied array must be equal to or bigger than the ringBuffer, ARRAY BOUNDS
126126-- ARE NOT CHECKED.
127127{-# INLINE unsafeEqArray #-}
128−unsafeEqArray :: RingBuffer a -> Ptr a -> A.Array a -> Bool
129−unsafeEqArray RingBuffer{..} rh A.Array{..} =
128+unsafeEqArray :: Ring a -> Ptr a -> A.Array a -> Bool
129+unsafeEqArray Ring{..} rh A.Array{..} =
130130 let !res = A.unsafeInlineIO $ do
131131 let rs = unsafeForeignPtrToPtr ringStart
132132 let as = unsafeForeignPtrToPtr aStart
@@ -151,8 +151,8 @@ unsafeEqArray RingBuffer{..} rh A.Array{..} =
151151-- Unsafe because the supplied Ptr is not checked to be in range.
152152{-# INLINE unsafeFoldRing #-}
153153unsafeFoldRing :: forall a b. Storable a
154− => Ptr a -> (b -> a -> b) -> b -> RingBuffer a -> b
155−unsafeFoldRing ptr f z RingBuffer{..} =
154+ => Ptr a -> (b -> a -> b) -> b -> Ring a -> b
155+unsafeFoldRing ptr f z Ring{..} =
156156 let !res = A.unsafeInlineIO $ withForeignPtr ringStart $ \p ->
157157 go z p ptr
158158 in res
@@ -166,8 +166,8 @@ unsafeFoldRing ptr f z RingBuffer{..} =
166166-- | Like unsafeFoldRing but with a monadic step function.
167167{-# INLINE unsafeFoldRingM #-}
168168unsafeFoldRingM :: forall m a b. (Monad m, Storable a)
169− => Ptr a -> (b -> a -> m b) -> b -> RingBuffer a -> m b
170−unsafeFoldRingM ptr f z RingBuffer{..} = go z (unsafeForeignPtrToPtr ringStart) ptr
169+ => Ptr a -> (b -> a -> m b) -> b -> Ring a -> m b
170+unsafeFoldRingM ptr f z Ring{..} = go z (unsafeForeignPtrToPtr ringStart) ptr
171171 where
172172 go !acc !start !end
173173 | start == end = return acc
@@ -182,8 +182,8 @@ unsafeFoldRingM ptr f z RingBuffer{..} = go z (unsafeForeignPtrToPtr ringStart)
182182-- the ring.
183183{-# INLINE unsafeFoldRingFullM #-}
184184unsafeFoldRingFullM :: forall m a b. (Monad m, Storable a)
185− => Ptr a -> (b -> a -> m b) -> b -> RingBuffer a -> m b
186−unsafeFoldRingFullM rh f z rb@RingBuffer{..} = go z rh
185+ => Ptr a -> (b -> a -> m b) -> b -> Ring a -> m b
186+unsafeFoldRingFullM rh f z rb@Ring{..} = go z rh
187187 where
188188 go !acc !start = do
189189 let !x = A.unsafeInlineIO $ peek start
src/Streamly/Streams/StreamD.hs+5−4
@@ -242,15 +242,16 @@ import Prelude
242242 (!!), scanl, scanl1, concatMap, replicate, enumFromTo, concat,
243243 reverse)
244244
245−import Streamly.Array.Types (Array(..))
245+import Streamly.Mem.Array.Types (Array(..))
246246import Streamly.Fold.Types (Fold(..))
247247import Streamly.SVar (MonadAsync, defState, adaptState)
248248import Streamly.Sink.Types (Sink(..))
249249
250250import Streamly.Streams.StreamD.Type
251+
252+import qualified Streamly.Mem.Array.Types as A
253+import qualified Streamly.Mem.Ring as RB
251254import qualified Streamly.Streams.StreamK as K
252−import qualified Streamly.Array.Types as A
253−import qualified Streamly.RingBuffer as RB
254255
255256------------------------------------------------------------------------------
256257-- Construction
@@ -1141,7 +1142,7 @@ data SplitOnState s a =
11411142 | GO_EMPTY_PAT s
11421143 | GO_SINGLE_PAT s a
11431144 | GO_SHORT_PAT s
1144− | GO_KARP_RABIN s !(RB.RingBuffer a) !(Ptr a)
1145+ | GO_KARP_RABIN s !(RB.Ring a) !(Ptr a)
11451146 | GO_DONE
11461147
11471148{-# INLINE_NORMAL splitOn #-}
streamly.cabal+14−5
@@ -162,9 +162,17 @@ library
162162 hs-source-dirs: src
163163 other-modules: Streamly.Atomics
164164 , Streamly.SVar
165+ , Streamly.Strict
166+ , Streamly.Enumeration
167+
168+ -- Time
165169 , Streamly.Time.Units
166170 , Streamly.Time.Clock
167171
172+ -- Memory storage
173+ , Streamly.Mem.Array.Types
174+ , Streamly.Mem.Ring
175+
168176 -- Base streams
169177 , Streamly.Streams.StreamK.Type
170178 , Streamly.Streams.StreamK
@@ -180,21 +188,22 @@ library
180188 , Streamly.Streams.Ahead
181189 , Streamly.Streams.Zip
182190 , Streamly.Streams.Combinators
191+
192+ -- Pure streams
183193 , Streamly.List
184194 , Streamly.String
185− , Streamly.Enumeration
186− , Streamly.Array.Types
187− , Streamly.RingBuffer
188− , Streamly.Strict
195+
196+ -- Folds
189197 , Streamly.Fold.Types
190198 , Streamly.Sink.Types
191199 , Streamly.Sink
192200
193201 exposed-modules: Streamly.Prelude
194− , Streamly.Array
195202 , Streamly.Time
196203 , Streamly
197204 , Streamly.Fold
205+
206+ , Streamly.Mem.Array
198207 , Streamly.FileSystem.File
199208
200209 , Streamly.Tutorial
test/Arrays.hs+2−3
@@ -6,14 +6,13 @@ module Main (main) where
66import Foreign.Storable (Storable(..))
77
88import Test.Hspec.QuickCheck
9−import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary,
10− choose)
9+import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, choose)
1110import Test.QuickCheck.Monadic (monadicIO, assert)
1211
1312import Test.Hspec as H
1413
14+import qualified Streamly.Mem.Array as A
1515import qualified Streamly.Prelude as S
16−import qualified Streamly.Array as A
1716
1817-- Coverage build takes too long with default number of tests
1918maxTestCount :: Int