on master help sign in
Refactor readArraysOfUpto to readArraysOf in Socket, Handle, File.

- And also in examples, benchmarking code.

commit07e954820de2db55c26442d612518dade19cf883 authorPranay Sashank <pranaysashank@gmail.com> dateTue 6 Aug 2019, 23:36 (7 years ago) parent5d5d16a
6 files changed+39−36
benchmark/FileIO.hs+2−2 examples/FileIOExamples.hs+3−3 examples/HandleIO.hs+2−2 src/Streamly/FileSystem/File.hs+10−9 src/Streamly/FileSystem/Handle.hs+14−12 src/Streamly/Network/Socket.hs+8−8
benchmark/FileIO.hs+2−2
@@ -117,7 +117,7 @@ main = do
117117 S.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
118118 , mkBench "cat" href $ do
119119 Handles inh _ <- readIORef href
120− FH.writeArrays devNull $ FH.readArraysOfUpto (256*1024) inh
120+ FH.writeArrays devNull $ FH.readArraysOf (256*1024) inh
121121 ]
122122 , bgroup "readStream"
123123 [ mkBench "last" href $ do
@@ -198,7 +198,7 @@ main = do
198198 FH.writeArraysPackedUpto (1024*1024) outh
199199 $ S.insertAfterEach (return $ A.fromList [10])
200200 $ AS.splitOn 10
201− $ FH.readArraysOfUpto (1024*1024) inh
201+ $ FH.readArraysOf (1024*1024) inh
202202 -}
203203 , mkBench "words-unwords" href $ do
204204 Handles inh outh <- readIORef href
examples/FileIOExamples.hs+3−3
@@ -7,13 +7,13 @@ import Data.Char (ord)
77import System.Environment (getArgs)
88
99cat :: FilePath -> IO ()
10−cat src = File.writeArrays "/dev/stdout" $ File.readArraysOfUpto (256*1024) src
10+cat src = File.writeArrays "/dev/stdout" $ File.readArraysOf (256*1024) src
1111
1212cp :: FilePath -> FilePath -> IO ()
13−cp src dst = File.writeArrays dst $ File.readArraysOfUpto (256*1024) src
13+cp src dst = File.writeArrays dst $ File.readArraysOf (256*1024) src
1414
1515append :: FilePath -> FilePath -> IO ()
16−append src dst = File.appendArrays dst $ File.readArraysOfUpto (256*1024) src
16+append src dst = File.appendArrays dst $ File.readArraysOf (256*1024) src
1717
1818ord' :: Num a => Char -> a
1919ord' = (fromIntegral . ord)
examples/HandleIO.hs+2−2
@@ -12,12 +12,12 @@ import System.Environment (getArgs)
1212import System.IO (IOMode(..), hSeek, SeekMode(..))
1313
1414cat :: FH.Handle -> IO ()
15−cat src = FH.writeArrays FH.stdout $ FH.readArraysOfUpto (256*1024) src
15+cat src = FH.writeArrays FH.stdout $ FH.readArraysOf (256*1024) src
1616-- byte stream version
1717-- cat src = FH.write FH.stdout $ FH.read src
1818
1919cp :: FH.Handle -> FH.Handle -> IO ()
20−cp src dst = FH.writeArrays dst $ FH.readArraysOfUpto (256*1024) src
20+cp src dst = FH.writeArrays dst $ FH.readArraysOf (256*1024) src
2121-- byte stream version
2222-- cp src dst = FH.write dst $ FH.read src
2323
src/Streamly/FileSystem/File.hs+10−9
@@ -63,7 +63,7 @@ module Streamly.FileSystem.File
6363 -- -- * Array Read
6464 -- , readArrayOf
6565
66− , readArraysOfUpto
66+ , readArraysOf
6767 -- , readArraysOf
6868 , readArrays
6969
@@ -183,26 +183,27 @@ appendArray file arr = SIO.withFile file AppendMode (\h -> FH.writeArray h arr)
183183-- Stream of Arrays IO
184184-------------------------------------------------------------------------------
185185
186−-- | @readArraysOfUpto size file@ reads a stream of arrays from file @file@.
186+-- | @readArraysOf size file@ reads a stream of arrays from file @file@.
187187-- The maximum size of a single array is specified by @size@. The actual size
188188-- read may be less than or equal to @size@.
189−{-# INLINABLE readArraysOfUpto #-}
190−readArraysOfUpto :: (IsStream t, MonadCatch m, MonadIO m)
189+{-# INLINABLE readArraysOf #-}
190+readArraysOf :: (IsStream t, MonadCatch m, MonadIO m)
191191 => Int -> FilePath -> t m (Array Word8)
192−readArraysOfUpto size file = withFile file ReadMode (FH.readArraysOfUpto size)
192+readArraysOf size file = withFile file ReadMode (FH.readArraysOf size)
193193
194194-- XXX read 'Array a' instead of Word8
195195--
196196-- | @readArrays file@ reads a stream of arrays from file @file@.
197−-- The maximum size of a single array is limited to @defaultChunkSize@.
197+-- The maximum size of a single array is limited to @defaultChunkSize@. The
198+-- actual size read may be less than @defaultChunkSize@.
198199--
199−-- > readArrays = readArraysOfUpto defaultChunkSize
200+-- > readArrays = readArraysOf defaultChunkSize
200201--
201202-- @since 0.7.0
202203{-# INLINE readArrays #-}
203204readArrays :: (IsStream t, MonadCatch m, MonadIO m)
204205 => FilePath -> t m (Array Word8)
205−readArrays = readArraysOfUpto A.defaultChunkSize
206+readArrays = readArraysOf A.defaultChunkSize
206207
207208-------------------------------------------------------------------------------
208209-- Read File to Stream
@@ -219,7 +220,7 @@ readArrays = readArraysOfUpto A.defaultChunkSize
219220--
220221{-# INLINE readByChunksUpto #-}
221222readByChunksUpto :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
222−readByChunksUpto chunkSize h = A.flattenArrays $ readArraysOfUpto chunkSize h
223+readByChunksUpto chunkSize h = A.flattenArrays $ readArraysOf chunkSize h
223224-}
224225
225226-- TODO
src/Streamly/FileSystem/Handle.hs+14−12
@@ -67,7 +67,7 @@ module Streamly.FileSystem.Handle
6767 -- , readArrayUpto
6868 -- , readArrayOf
6969
70− , readArraysOfUpto
70+ , readArraysOf
7171 -- , readArraysOf
7272 , readArrays
7373
@@ -187,13 +187,13 @@ writeArray h Array{..} = withForeignPtr aStart $ \p -> hPutBuf h p aLen
187187-- Stream of Arrays IO
188188-------------------------------------------------------------------------------
189189
190−-- | @readArraysOfUpto size h@ reads a stream of arrays from file handle @h@.
190+-- | @readArraysOf size h@ reads a stream of arrays from file handle @h@.
191191-- The maximum size of a single array is specified by @size@. The actual size
192192-- read may be less than or equal to @size@.
193−{-# INLINABLE _readArraysOfUpto #-}
194−_readArraysOfUpto :: (IsStream t, MonadIO m)
193+{-# INLINABLE _readArraysOf #-}
194+_readArraysOf :: (IsStream t, MonadIO m)
195195 => Int -> Handle -> t m (Array Word8)
196−_readArraysOfUpto size h = go
196+_readArraysOf size h = go
197197 where
198198 -- XXX use cons/nil instead
199199 go = mkStream $ \_ yld _ stp -> do
@@ -202,13 +202,14 @@ _readArraysOfUpto size h = go
202202 then stp
203203 else yld arr go
204204
205−-- | @readArraysOfUpto size handle@ reads a stream of arrays from the file
205+-- | @readArraysOf size handle@ reads a stream of arrays from the file
206206-- handle @handle@. The maximum size of a single array is limited to @size@.
207+-- The actual size read may be less than or equal to @size@.
207208--
208209-- @since 0.7.0
209−{-# INLINE_NORMAL readArraysOfUpto #-}
210−readArraysOfUpto :: (IsStream t, MonadIO m) => Int -> Handle -> t m (Array Word8)
211−readArraysOfUpto size h = D.fromStreamD (D.Stream step ())
210+{-# INLINE_NORMAL readArraysOf #-}
211+readArraysOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m (Array Word8)
212+readArraysOf size h = D.fromStreamD (D.Stream step ())
212213 where
213214 {-# INLINE_LATE step #-}
214215 step _ _ = do
@@ -222,14 +223,15 @@ readArraysOfUpto size h = D.fromStreamD (D.Stream step ())
222223--
223224-- | @readArrays handle@ reads a stream of arrays from the specified file
224225-- handle. The maximum size of a single array is limited to
226+-- @defaultChunkSize@. The actual size read may be less than or equal to
225227-- @defaultChunkSize@.
226228--
227−-- > readArrays = readArraysOfUpto defaultChunkSize
229+-- > readArrays = readArraysOf defaultChunkSize
228230--
229231-- @since 0.7.0
230232{-# INLINE readArrays #-}
231233readArrays :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
232−readArrays = readArraysOfUpto defaultChunkSize
234+readArrays = readArraysOf defaultChunkSize
233235
234236-------------------------------------------------------------------------------
235237-- Read File to Stream
@@ -245,7 +247,7 @@ readArrays = readArraysOfUpto defaultChunkSize
245247-- @since 0.7.0
246248{-# INLINE readByChunksUpto #-}
247249readByChunksUpto :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
248−readByChunksUpto chunkSize h = AS.flatten $ readArraysOfUpto chunkSize h
250+readByChunksUpto chunkSize h = AS.flatten $ readArraysOf chunkSize h
249251
250252-- TODO
251253-- Generate a stream of elements of the given type from a file 'Handle'.
src/Streamly/Network/Socket.hs+8−8
@@ -154,9 +154,9 @@ readArrayUptoWith f size h = do
154154-- handle it blocks until some data becomes available. If data is available
155155-- then it immediately returns that data without blocking. It reads a maximum
156156-- of up to the size requested.
157−{-# INLINABLE readArrayUpto #-}
158−readArrayUpto :: Int -> Socket -> IO (Array Word8)
159−readArrayUpto = readArrayUptoWith recvBuf
157+{-# INLINABLE readArrayOf #-}
158+readArrayOf :: Int -> Socket -> IO (Array Word8)
159+readArrayOf = readArrayUptoWith recvBuf
160160
161161-------------------------------------------------------------------------------
162162-- Array IO (output)
@@ -217,14 +217,14 @@ readArraysUptoWith f size h = go
217217 then stp
218218 else yld arr go
219219
220−-- | @readArraysUpto size h@ reads a stream of arrays from file handle @h@.
220+-- | @readArraysOf size h@ reads a stream of arrays from file handle @h@.
221221-- The maximum size of a single array is limited to @size@.
222222-- 'fromHandleArraysUpto' ignores the prevailing 'TextEncoding' and 'NewlineMode'
223223-- on the 'Handle'.
224−{-# INLINABLE readArraysUpto #-}
225−readArraysUpto :: (IsStream t, MonadIO m)
224+{-# INLINABLE readArraysOf #-}
225+readArraysOf :: (IsStream t, MonadIO m)
226226 => Int -> Socket -> t m (Array Word8)
227−readArraysUpto = readArraysUptoWith readArrayUpto
227+readArraysOf = readArraysUptoWith readArrayOf
228228
229229-- XXX read 'Array a' instead of Word8
230230--
@@ -236,7 +236,7 @@ readArraysUpto = readArraysUptoWith readArrayUpto
236236-- @since 0.7.0
237237{-# INLINE readArrays #-}
238238readArrays :: (IsStream t, MonadIO m) => Socket -> t m (Array Word8)
239−readArrays = readArraysUpto A.defaultChunkSize
239+readArrays = readArraysOf A.defaultChunkSize
240240
241241-------------------------------------------------------------------------------
242242-- Read File to Stream