on master help sign in
Rename write* to create* in Array module

Export create/createOf from Array module Rearrange exports in Array module

commit8e8159f1561303af44bb3472145473c7e2037b4a authorHarendra Kumar <harendra@composewell.com> dateSat 6 Jan 2024, 15:38 (3 years ago) parentb7d8b96
9 files changed+126−73
core/src/DocTestFileSystemHandle.hs+1−1 core/src/Streamly/Data/Array.hs+28−24 core/src/Streamly/Internal/Data/Array.hs+3−1 core/src/Streamly/Internal/Data/Array/Type.hs+75−28 core/src/Streamly/Internal/FileSystem/File.hs+2−2 core/src/Streamly/Internal/FileSystem/Handle.hs+10−10 src/Streamly/Internal/Network/Inet/TCP.hs+3−3 src/Streamly/Internal/Network/Socket.hs+3−3 test/Streamly/Test/Data/Array.hs+1−1
core/src/DocTestFileSystemHandle.hs+1−1
@@ -8,7 +8,7 @@
88
99For APIs that have not been released yet.
1010
11−>>> import qualified Streamly.Internal.Data.Array as Array (writeNUnsafe)
11+>>> import qualified Streamly.Internal.Data.Array as Array (unsafeCreateOf)
1212>>> import qualified Streamly.Internal.Data.Unfold as Unfold (first)
1313>>> import qualified Streamly.Internal.FileSystem.Handle as Handle
1414>>> import qualified Streamly.Internal.System.IO as IO (defaultChunkSize)
core/src/Streamly/Data/Array.hs+28−24
@@ -31,42 +31,42 @@ module Streamly.Data.Array
3131 -- * The Array Type
3232 Array
3333
34+ -- * Pinning & Unpinning
35+ -- | Arrays are created unpinned by default unless pinned versions of
36+ -- creation APIs are used. Look for APIs with @pinned@ prefix in
37+ -- "Streamly.Internal.Data.Array" for some unreleased pinned creation APIs.
38+ -- If an array is to be sent to the OS without any further modification
39+ -- then it should be created pinned in the first place instead of pinning
40+ -- it later. Pinning an unpinned array has a copy overhead. OS interfacing
41+ -- APIs create a pinned array directly or convert an unpinned array to
42+ -- pinned array before sending it to the OS.
43+ , pin
44+ , unpin
45+ , isPinned
46+
3447 -- * Construction
3548 -- | When performance matters, the fastest way to generate an array is
36− -- 'writeN'. 'IsList' and 'IsString' instances can be
49+ -- 'createOf'. 'IsList' and 'IsString' instances can be
3750 -- used to conveniently construct arrays from literal values.
3851 -- 'OverloadedLists' extension or 'fromList' can be used to construct an
3952 -- array from a list literal. Similarly, 'OverloadedStrings' extension or
4053 -- 'fromList' can be used to construct an array from a string literal.
4154
42− -- Pure List APIs
55+ -- ** From Stream
56+ , createOf
57+ , create
58+ , writeLastN -- drop old (ring buffer)
59+
60+ -- ** From List
4361 , fromListN
4462 , fromList
4563
46− -- Monadic APIs
47− , writeN -- drop new
48− , write -- full buffer
49− , writeLastN -- drop old (ring buffer)
50−
51− -- * Conversion
64+ -- * To List
5265 -- 'GHC.Exts.toList' from "GHC.Exts" can be used to convert an array to a
5366 -- list.
5467 , toList
5568
56− -- * Pinning & Unpinning
57− -- | Arrays are created unpinned by default unless pinned versions of
58− -- creation APIs are used. Look for APIs with @pinned@ prefix in
59− -- "Streamly.Internal.Data.Array" for some unreleased pinned creation APIs.
60− -- If an array is to be sent to the OS without any further modification
61− -- then it should be created pinned in the first place instead of pinning
62− -- it later. Pinning an unpinned array has a copy overhead. OS interfacing
63− -- APIs create a pinned array directly or convert an unpinned array to
64− -- pinned array before sending it to the OS.
65− , pin
66− , unpin
67− , isPinned
68−
69− -- * Streams
69+ -- * To Stream
7070 , read
7171 , readRev
7272
@@ -90,6 +90,10 @@ module Streamly.Data.Array
9090 -- * Re-exports
9191 , Unbox (..)
9292 , Serialize(..)
93+
94+ -- * Deprecated
95+ , writeN -- drop new
96+ , write -- full buffer
9397 )
9498where
9599
@@ -122,7 +126,7 @@ import Prelude hiding (read, length)
122126--
123127-- Convert array to stream, transform, and fold back to array:
124128--
125−-- >>> amap f arr = Array.read arr & fmap f & Stream.fold Array.write
129+-- >>> amap f arr = Array.read arr & fmap f & Stream.fold Array.create
126130-- >>> amap (+1) (Array.fromList [1,2,3::Int])
127131-- fromList [2,3,4]
128132--
@@ -154,7 +158,7 @@ import Prelude hiding (read, length)
154158--
155159-- >>> pure = Stream.fromList [1,2,3] :: Stream Identity Int
156160-- >>> generally = Stream.morphInner (return . runIdentity)
157−-- >>> Stream.fold Array.write (generally pure :: Stream IO Int)
161+-- >>> Stream.fold Array.create (generally pure :: Stream IO Int)
158162-- fromList [1,2,3]
159163--
160164-- == Programming Tips
core/src/Streamly/Internal/Data/Array.hs+3−1
@@ -205,6 +205,8 @@ last = getIndexRev 0
205205-- Folds with Array as the container
206206-------------------------------------------------------------------------------
207207
208+-- XXX We should generate this from Ring.
209+
208210-- | @writeLastN n@ folds a maximum of @n@ elements from the end of the input
209211-- stream to an 'Array'.
210212--
@@ -447,7 +449,7 @@ runPipe f arr = P.runPipe (toArrayMinChunk (length arr)) $ f (read arr)
447449streamTransform :: forall m a b. (MonadIO m, Unbox a, Unbox b)
448450 => (Stream m a -> Stream m b) -> Array a -> m (Array b)
449451streamTransform f arr =
450− Stream.fold (writeWith (length arr)) $ f (read arr)
452+ Stream.fold (createWith (length arr)) $ f (read arr)
451453
452454-------------------------------------------------------------------------------
453455-- Casts
core/src/Streamly/Internal/Data/Array/Type.hs+75−28
@@ -45,15 +45,13 @@ module Streamly.Internal.Data.Array.Type
4545
4646 -- *** Stream Folds
4747 , unsafeMakePure
48− , writeWith
49− , writeN
50− , pinnedWriteN
51− , writeNUnsafe
52− , pinnedWriteNUnsafe
53− -- , MA.ArrayUnsafe (..)
54− , pinnedWriteNAligned
55− , write
56− , pinnedWrite
48+ , createOf
49+ , pinnedCreateOf
50+ , unsafeCreateOf
51+ , unsafePinnedCreateOf
52+ , create
53+ , pinnedCreate
54+ , createWith
5755
5856 -- *** From containers
5957 , fromListN
@@ -75,8 +73,8 @@ module Streamly.Internal.Data.Array.Type
7573 -- ** Reading
7674
7775 -- *** Indexing
78− , unsafeIndexIO -- XXX rename to getIndexUnsafeIO
79− , getIndexUnsafe
76+ , unsafeIndexIO -- XXX unsafeGetIndexIO
77+ , getIndexUnsafe -- XXX unsafeGetIndex
8078
8179 -- *** To Streams
8280 , read
@@ -143,6 +141,14 @@ module Streamly.Internal.Data.Array.Type
143141 , toStream
144142 , toStreamRev
145143 , nil
144+ , writeWith
145+ , writeN
146+ , pinnedWriteN
147+ , writeNUnsafe
148+ , pinnedWriteNUnsafe
149+ , pinnedWriteNAligned
150+ , write
151+ , pinnedWrite
146152 )
147153where
148154
@@ -745,17 +751,28 @@ toList s = build (\c n -> toListFB c n s)
745751-- Folds
746752-------------------------------------------------------------------------------
747753
748−-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
754+-- | @createOf n@ folds a maximum of @n@ elements from the input stream to an
749755-- 'Array'.
750756--
751−{-# INLINE_NORMAL writeN #-}
757+{-# INLINE_NORMAL createOf #-}
758+createOf :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
759+createOf = fmap unsafeFreeze . MA.createOf
760+
761+-- XXX Deprecate in major
762+-- {-# DEPRECATED writeN "Please use createOf instead." #-}
763+{-# INLINE writeN #-}
752764writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
753−writeN = fmap unsafeFreeze . MA.writeN
765+writeN = createOf
754766
755−-- | Like 'fromListN' but creates a pinned array.
756−{-# INLINE_NORMAL pinnedWriteN #-}
767+-- | Like 'createOf' but creates a pinned array.
768+{-# INLINE_NORMAL pinnedCreateOf #-}
769+pinnedCreateOf :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
770+pinnedCreateOf = fmap unsafeFreeze . MA.pinnedCreateOf
771+
772+{-# DEPRECATED pinnedWriteN "Please use pinnedCreateOf instead." #-}
773+{-# INLINE pinnedWriteN #-}
757774pinnedWriteN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
758−pinnedWriteN = fmap unsafeFreeze . MA.pinnedCreateOf
775+pinnedWriteN = pinnedCreateOf
759776
760777-- | @pinnedWriteNAligned alignment n@ folds a maximum of @n@ elements from the input
761778-- stream to an 'Array' aligned to the given size.
@@ -763,44 +780,74 @@ pinnedWriteN = fmap unsafeFreeze . MA.pinnedCreateOf
763780-- /Pre-release/
764781--
765782{-# INLINE_NORMAL pinnedWriteNAligned #-}
783+{-# DEPRECATED pinnedWriteNAligned "To be removed." #-}
766784pinnedWriteNAligned :: forall m a. (MonadIO m, Unbox a)
767785 => Int -> Int -> Fold m a (Array a)
768786pinnedWriteNAligned alignSize = fmap unsafeFreeze . MA.pinnedWriteNAligned alignSize
769787
770−-- | Like 'writeN' but does not check the array bounds when writing. The fold
788+-- | Like 'createOf' but does not check the array bounds when writing. The fold
771789-- driver must not call the step function more than 'n' times otherwise it will
772790-- corrupt the memory and crash. This function exists mainly because any
773791-- conditional in the step function blocks fusion causing 10x performance
774792-- slowdown.
775793--
776−{-# INLINE_NORMAL writeNUnsafe #-}
794+{-# INLINE_NORMAL unsafeCreateOf #-}
795+unsafeCreateOf :: forall m a. (MonadIO m, Unbox a)
796+ => Int -> Fold m a (Array a)
797+unsafeCreateOf n = unsafeFreeze <$> MA.unsafeCreateOf n
798+
799+{-# DEPRECATED writeNUnsafe "Please use unsafeCreateOf instead." #-}
800+{-# INLINE writeNUnsafe #-}
777801writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
778802 => Int -> Fold m a (Array a)
779−writeNUnsafe n = unsafeFreeze <$> MA.unsafeCreateOf n
803+writeNUnsafe = unsafeCreateOf
780804
781−{-# INLINE_NORMAL pinnedWriteNUnsafe #-}
805+{-# INLINE_NORMAL unsafePinnedCreateOf #-}
806+unsafePinnedCreateOf :: forall m a. (MonadIO m, Unbox a)
807+ => Int -> Fold m a (Array a)
808+unsafePinnedCreateOf n = unsafeFreeze <$> MA.unsafePinnedCreateOf n
809+
810+{-# DEPRECATED pinnedWriteNUnsafe "Please use unsafePinnedCreateOf instead." #-}
811+{-# INLINE pinnedWriteNUnsafe #-}
782812pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
783813 => Int -> Fold m a (Array a)
784−pinnedWriteNUnsafe n = unsafeFreeze <$> MA.unsafePinnedCreateOf n
814+pinnedWriteNUnsafe = unsafePinnedCreateOf
815+
816+{-# INLINE_NORMAL createWith #-}
817+createWith :: forall m a. (MonadIO m, Unbox a)
818+ => Int -> Fold m a (Array a)
819+-- createWith n = FL.rmapM spliceArrays $ toArraysOf n
820+createWith elemCount = unsafeFreeze <$> MA.createWith elemCount
785821
786−{-# INLINE_NORMAL writeWith #-}
822+{-# DEPRECATED writeWith "Please use createWith instead." #-}
823+{-# INLINE writeWith #-}
787824writeWith :: forall m a. (MonadIO m, Unbox a)
788825 => Int -> Fold m a (Array a)
789−-- writeWith n = FL.rmapM spliceArrays $ toArraysOf n
790−writeWith elemCount = unsafeFreeze <$> MA.createWith elemCount
826+writeWith = createWith
791827
792828-- | Fold the whole input to a single array.
793829--
794830-- /Caution! Do not use this on infinite streams./
795831--
832+{-# INLINE create #-}
833+create :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
834+create = fmap unsafeFreeze MA.create
835+
836+-- XXX Deprecate in major
837+-- {-# DEPRECATED write "Please use create instead." #-}
796838{-# INLINE write #-}
797839write :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
798−write = fmap unsafeFreeze MA.write
840+write = create
841+
842+-- | Like 'create' but creates a pinned array.
843+{-# INLINE pinnedCreate #-}
844+pinnedCreate :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
845+pinnedCreate = fmap unsafeFreeze MA.pinnedCreate
799846
800−-- | Like 'write' but creates a pinned array.
847+{-# DEPRECATED pinnedWrite "Please use pinnedCreate instead." #-}
801848{-# INLINE pinnedWrite #-}
802849pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
803−pinnedWrite = fmap unsafeFreeze MA.pinnedCreate
850+pinnedWrite = pinnedCreate
804851
805852-- | Fold "step" has a dependency on "initial", and each step is dependent on
806853-- the previous invocation of step due to state passing, finally extract
core/src/Streamly/Internal/FileSystem/File.hs+2−2
@@ -103,7 +103,7 @@ import qualified Control.Monad.Catch as MC
103103import qualified System.IO as SIO
104104
105105import Streamly.Data.Fold (groupsOf, drain)
106−import Streamly.Internal.Data.Array.Type (Array(..), pinnedWriteNUnsafe)
106+import Streamly.Internal.Data.Array.Type (Array(..))
107107import Streamly.Internal.Data.Fold.Type (Fold(..))
108108import Streamly.Data.Stream (Stream)
109109import Streamly.Internal.Data.Unfold.Type (Unfold(..))
@@ -462,7 +462,7 @@ writeChunks path = Fold step initial extract final
462462writeWith :: (MonadIO m, MonadCatch m)
463463 => Int -> FilePath -> Fold m Word8 ()
464464writeWith n path =
465− groupsOf n (pinnedWriteNUnsafe n) (writeChunks path)
465+ groupsOf n (A.unsafePinnedCreateOf n) (writeChunks path)
466466
467467{-# DEPRECATED writeWithBufferOf "Please use 'writeWith' instead" #-}
468468{-# INLINE writeWithBufferOf #-}
core/src/Streamly/Internal/FileSystem/Handle.hs+10−10
@@ -40,8 +40,8 @@ module Streamly.Internal.FileSystem.Handle
4040
4141 -- * Streams
4242 , read
43− , readWith
44− , readChunksWith
43+ , readWith -- readConcatChunksOf
44+ , readChunksWith -- readChunksOf
4545 , readChunks
4646
4747 -- * Unfolds
@@ -59,10 +59,10 @@ module Streamly.Internal.FileSystem.Handle
5959 -- , writeUtf8ByLines
6060 -- , writeByFrames
6161 -- , writeLines
62− , writeWith
62+ , writeWith -- writeChunksOf
6363 , writeChunks
64− , writeChunksWith
65− , writeMaybesWith
64+ , writeChunksWith -- writeCompactChunksOf
65+ , writeMaybesWith -- writeCompactJustsOf
6666
6767 -- * Refolds
6868 , writer
@@ -131,7 +131,7 @@ import Streamly.Internal.Data.Fold (Fold)
131131import Streamly.Internal.Data.Refold.Type (Refold(..))
132132import Streamly.Internal.Data.Unfold.Type (Unfold(..))
133133import Streamly.Internal.Data.Array.Type
134− (Array(..), pinnedWriteNUnsafe, unsafeFreezeWithShrink, byteLength)
134+ (Array(..), unsafeFreezeWithShrink, byteLength)
135135import Streamly.Internal.Data.Stream.Type (Stream)
136136-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
137137import Streamly.Internal.System.IO (defaultChunkSize)
@@ -489,11 +489,11 @@ writeChunksWithBufferOf = writeChunksWith
489489-- Bytes in the input stream are collected into a buffer until we have a chunk
490490-- of @reqSize@ and then written to the IO device.
491491--
492−-- >>> writeWith n h = Fold.groupsOf n (Array.writeNUnsafe n) (Handle.writeChunks h)
492+-- >>> writeWith n h = Fold.groupsOf n (Array.unsafeCreateOf n) (Handle.writeChunks h)
493493--
494494{-# INLINE writeWith #-}
495495writeWith :: MonadIO m => Int -> Handle -> Fold m Word8 ()
496−writeWith n h = FL.groupsOf n (pinnedWriteNUnsafe n) (writeChunks h)
496+writeWith n h = FL.groupsOf n (A.unsafePinnedCreateOf n) (writeChunks h)
497497
498498-- | Same as 'writeWith'
499499--
@@ -511,7 +511,7 @@ writeWithBufferOf = writeWith
511511writeMaybesWith :: (MonadIO m )
512512 => Int -> Handle -> Fold m (Maybe Word8) ()
513513writeMaybesWith n h =
514− let writeNJusts = FL.lmap fromJust $ A.pinnedWriteN n
514+ let writeNJusts = FL.lmap fromJust $ A.pinnedCreateOf n
515515 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
516516 in FL.many writeOnNothing (writeChunks h)
517517
@@ -521,7 +521,7 @@ writeMaybesWith n h =
521521{-# INLINE writerWith #-}
522522writerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
523523writerWith n =
524− FL.refoldMany (FL.take n $ pinnedWriteNUnsafe n) chunkWriter
524+ FL.refoldMany (FL.take n $ A.unsafePinnedCreateOf n) chunkWriter
525525
526526-- | Write a byte stream to a file handle. Accumulates the input in chunks of
527527-- up to 'Streamly.Internal.Data.Array.Type.defaultChunkSize' before writing
src/Streamly/Internal/Network/Inet/TCP.hs+3−3
@@ -116,7 +116,6 @@ import Prelude hiding (read)
116116import Streamly.Internal.Control.Concurrent (MonadAsync)
117117import Streamly.Internal.Control.ForkLifted (fork)
118118import Streamly.Data.Array (Array)
119−import Streamly.Internal.Data.Array (pinnedWriteNUnsafe)
120119import Streamly.Internal.Data.Fold ( Fold(..) )
121120import Streamly.Data.Stream (Stream)
122121import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
@@ -132,7 +131,8 @@ import qualified Streamly.Data.Array as A
132131import qualified Streamly.Data.Fold as FL
133132import qualified Streamly.Data.Stream as S
134133import qualified Streamly.Data.Unfold as UF
135−import qualified Streamly.Internal.Data.Array as A (pinnedChunksOf)
134+import qualified Streamly.Internal.Data.Array as A
135+ (pinnedChunksOf, unsafePinnedCreateOf)
136136import qualified Streamly.Internal.Data.Unfold as UF (bracketIO)
137137import qualified Streamly.Internal.Data.Fold as FL (Step(..), reduce)
138138
@@ -435,7 +435,7 @@ writeWithBufferOf
435435 -> PortNumber
436436 -> Fold m Word8 ()
437437writeWithBufferOf n addr port =
438− FL.groupsOf n (pinnedWriteNUnsafe n) (writeChunks addr port)
438+ FL.groupsOf n (A.unsafePinnedCreateOf n) (writeChunks addr port)
439439
440440-- | Write a stream to the supplied IPv4 host address and port number.
441441--
src/Streamly/Internal/Network/Socket.hs+3−3
@@ -99,7 +99,7 @@ import qualified Streamly.Data.Stream as S
9999import qualified Streamly.Data.Unfold as UF
100100import qualified Streamly.Internal.Data.Array as A
101101 ( unsafeFreeze, unsafePinnedAsPtr, byteLength, pinnedChunksOf,
102− pinnedWriteN, pinnedWriteNUnsafe, lCompactGE )
102+ pinnedCreateOf, unsafePinnedCreateOf, lCompactGE )
103103import qualified Streamly.Internal.Data.MutArray as MArray
104104 (MutArray(..), unsafePinnedAsPtr, pinnedEmptyOf)
105105import qualified Streamly.Internal.Data.Stream as S (fromStreamK, Stream(..), Step(..))
@@ -505,7 +505,7 @@ putBytesWith n h m = putChunks h $ A.pinnedChunksOf n m
505505--
506506{-# INLINE writeWith #-}
507507writeWith :: MonadIO m => Int -> Socket -> Fold m Word8 ()
508−writeWith n h = FL.groupsOf n (A.pinnedWriteNUnsafe n) (writeChunks h)
508+writeWith n h = FL.groupsOf n (A.unsafePinnedCreateOf n) (writeChunks h)
509509
510510-- | Same as 'writeWith'
511511--
@@ -523,7 +523,7 @@ writeWithBufferOf = writeWith
523523writeMaybesWith :: (MonadIO m )
524524 => Int -> Socket -> Fold m (Maybe Word8) ()
525525writeMaybesWith n h =
526− let writeNJusts = FL.lmap fromJust $ A.pinnedWriteN n
526+ let writeNJusts = FL.lmap fromJust $ A.pinnedCreateOf n
527527 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
528528 in FL.many writeOnNothing (writeChunks h)
529529
test/Streamly/Test/Data/Array.hs+1−1
@@ -229,7 +229,7 @@ main =
229229 prop "read . write === id" testFoldUnfold
230230 prop "fromList" testFromList
231231 prop "foldMany with writeNUnsafe concats to original"
232− (foldManyWith (\n -> Fold.take n (A.writeNUnsafe n)))
232+ (foldManyWith (\n -> Fold.take n (A.unsafeCreateOf n)))
233233 describe "unsafeSlice" $ do
234234 it "partial" $ unsafeSlice 2 4 [1..10]
235235 it "none" $ unsafeSlice 10 0 [1..10]