on master help sign in
Refactor readArraysOfUpto to readArraysOf in Socket, Handle, File.
07e9548Pranay Sashank
benchmark/FileIO.hs+2−2
@@ -117,7 +117,7 @@ main = do
117 S.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
117 S.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
118 , mkBench "cat" href $ do
118 , mkBench "cat" href $ do
119 Handles inh _ <- readIORef href
119 Handles inh _ <- readIORef href
120− FH.writeArrays devNull $ FH.readArraysOfUpto (256*1024) inh
120+ FH.writeArrays devNull $ FH.readArraysOf (256*1024) inh
121 ]
121 ]
122 , bgroup "readStream"
122 , bgroup "readStream"
123 [ mkBench "last" href $ do
123 [ mkBench "last" href $ do
@@ -198,7 +198,7 @@ main = do
198 FH.writeArraysPackedUpto (1024*1024) outh
198 FH.writeArraysPackedUpto (1024*1024) outh
199 $ S.insertAfterEach (return $ A.fromList [10])
199 $ S.insertAfterEach (return $ A.fromList [10])
200 $ AS.splitOn 10
200 $ AS.splitOn 10
201− $ FH.readArraysOfUpto (1024*1024) inh
201+ $ FH.readArraysOf (1024*1024) inh
202 -}
202 -}
203 , mkBench "words-unwords" href $ do
203 , mkBench "words-unwords" href $ do
204 Handles inh outh <- readIORef href
204 Handles inh outh <- readIORef href
examples/FileIOExamples.hs+3−3
@@ -7,13 +7,13 @@ import Data.Char (ord)
7import System.Environment (getArgs)
7import System.Environment (getArgs)
8
8
9cat :: FilePath -> IO ()
9cat :: 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
11
11
12cp :: FilePath -> FilePath -> IO ()
12cp :: 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
14
14
15append :: FilePath -> FilePath -> IO ()
15append :: 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
17
17
18ord' :: Num a => Char -> a
18ord' :: Num a => Char -> a
19ord' = (fromIntegral . ord)
19ord' = (fromIntegral . ord)
examples/HandleIO.hs+2−2
@@ -12,12 +12,12 @@ import System.Environment (getArgs)
12import System.IO (IOMode(..), hSeek, SeekMode(..))
12import System.IO (IOMode(..), hSeek, SeekMode(..))
13
13
14cat :: FH.Handle -> IO ()
14cat :: 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
16-- byte stream version
16-- byte stream version
17-- cat src = FH.write FH.stdout $ FH.read src
17-- cat src = FH.write FH.stdout $ FH.read src
18
18
19cp :: FH.Handle -> FH.Handle -> IO ()
19cp :: 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
21-- byte stream version
21-- byte stream version
22-- cp src dst = FH.write dst $ FH.read src
22-- cp src dst = FH.write dst $ FH.read src
23
23
src/Streamly/FileSystem/File.hs+10−9
@@ -63,7 +63,7 @@ module Streamly.FileSystem.File
63 -- -- * Array Read
63 -- -- * Array Read
64 -- , readArrayOf
64 -- , readArrayOf
65
65
66− , readArraysOfUpto
66+ , readArraysOf
67 -- , readArraysOf
67 -- , readArraysOf
68 , readArrays
68 , readArrays
69
69
@@ -183,26 +183,27 @@ appendArray file arr = SIO.withFile file AppendMode (\h -> FH.writeArray h arr)
183-- Stream of Arrays IO
183-- Stream of Arrays IO
184-------------------------------------------------------------------------------
184-------------------------------------------------------------------------------
185
185
186−-- | @readArraysOfUpto size file@ reads a stream of arrays from file @file@.
186+-- | @readArraysOf size file@ reads a stream of arrays from file @file@.
187-- The maximum size of a single array is specified by @size@. The actual size
187-- The maximum size of a single array is specified by @size@. The actual size
188-- read may be less than or equal to @size@.
188-- read may be less than or equal to @size@.
189−{-# INLINABLE readArraysOfUpto #-}
189+{-# INLINABLE readArraysOf #-}
190−readArraysOfUpto :: (IsStream t, MonadCatch m, MonadIO m)
190+readArraysOf :: (IsStream t, MonadCatch m, MonadIO m)
191 => Int -> FilePath -> t m (Array Word8)
191 => 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)
193
193
194-- XXX read 'Array a' instead of Word8
194-- XXX read 'Array a' instead of Word8
195--
195--
196-- | @readArrays file@ reads a stream of arrays from file @file@.
196-- | @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@.
198--
199--
199−-- > readArrays = readArraysOfUpto defaultChunkSize
200+-- > readArrays = readArraysOf defaultChunkSize
200--
201--
201-- @since 0.7.0
202-- @since 0.7.0
202{-# INLINE readArrays #-}
203{-# INLINE readArrays #-}
203readArrays :: (IsStream t, MonadCatch m, MonadIO m)
204readArrays :: (IsStream t, MonadCatch m, MonadIO m)
204 => FilePath -> t m (Array Word8)
205 => FilePath -> t m (Array Word8)
205−readArrays = readArraysOfUpto A.defaultChunkSize
206+readArrays = readArraysOf A.defaultChunkSize
206
207
207-------------------------------------------------------------------------------
208-------------------------------------------------------------------------------
208-- Read File to Stream
209-- Read File to Stream
@@ -219,7 +220,7 @@ readArrays = readArraysOfUpto A.defaultChunkSize
219--
220--
220{-# INLINE readByChunksUpto #-}
221{-# INLINE readByChunksUpto #-}
221readByChunksUpto :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
222readByChunksUpto :: (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
223-}
224-}
224
225
225-- TODO
226-- TODO
src/Streamly/FileSystem/Handle.hs+14−12
@@ -67,7 +67,7 @@ module Streamly.FileSystem.Handle
67 -- , readArrayUpto
67 -- , readArrayUpto
68 -- , readArrayOf
68 -- , readArrayOf
69
69
70− , readArraysOfUpto
70+ , readArraysOf
71 -- , readArraysOf
71 -- , readArraysOf
72 , readArrays
72 , readArrays
73
73
@@ -187,13 +187,13 @@ writeArray h Array{..} = withForeignPtr aStart $ \p -> hPutBuf h p aLen
187-- Stream of Arrays IO
187-- Stream of Arrays IO
188-------------------------------------------------------------------------------
188-------------------------------------------------------------------------------
189
189
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@.
191-- The maximum size of a single array is specified by @size@. The actual size
191-- The maximum size of a single array is specified by @size@. The actual size
192-- read may be less than or equal to @size@.
192-- read may be less than or equal to @size@.
193−{-# INLINABLE _readArraysOfUpto #-}
193+{-# INLINABLE _readArraysOf #-}
194−_readArraysOfUpto :: (IsStream t, MonadIO m)
194+_readArraysOf :: (IsStream t, MonadIO m)
195 => Int -> Handle -> t m (Array Word8)
195 => Int -> Handle -> t m (Array Word8)
196−_readArraysOfUpto size h = go
196+_readArraysOf size h = go
197 where
197 where
198 -- XXX use cons/nil instead
198 -- XXX use cons/nil instead
199 go = mkStream $ \_ yld _ stp -> do
199 go = mkStream $ \_ yld _ stp -> do
@@ -202,13 +202,14 @@ _readArraysOfUpto size h = go
202 then stp
202 then stp
203 else yld arr go
203 else yld arr go
204
204
205−-- | @readArraysOfUpto size handle@ reads a stream of arrays from the file
205+-- | @readArraysOf size handle@ reads a stream of arrays from the file
206-- handle @handle@. The maximum size of a single array is limited to @size@.
206-- 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@.
207--
208--
208-- @since 0.7.0
209-- @since 0.7.0
209−{-# INLINE_NORMAL readArraysOfUpto #-}
210+{-# INLINE_NORMAL readArraysOf #-}
210−readArraysOfUpto :: (IsStream t, MonadIO m) => Int -> Handle -> t m (Array Word8)
211+readArraysOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m (Array Word8)
211−readArraysOfUpto size h = D.fromStreamD (D.Stream step ())
212+readArraysOf size h = D.fromStreamD (D.Stream step ())
212 where
213 where
213 {-# INLINE_LATE step #-}
214 {-# INLINE_LATE step #-}
214 step _ _ = do
215 step _ _ = do
@@ -222,14 +223,15 @@ readArraysOfUpto size h = D.fromStreamD (D.Stream step ())
222--
223--
223-- | @readArrays handle@ reads a stream of arrays from the specified file
224-- | @readArrays handle@ reads a stream of arrays from the specified file
224-- handle. The maximum size of a single array is limited to
225-- handle. The maximum size of a single array is limited to
226+-- @defaultChunkSize@. The actual size read may be less than or equal to
225-- @defaultChunkSize@.
227-- @defaultChunkSize@.
226--
228--
227−-- > readArrays = readArraysOfUpto defaultChunkSize
229+-- > readArrays = readArraysOf defaultChunkSize
228--
230--
229-- @since 0.7.0
231-- @since 0.7.0
230{-# INLINE readArrays #-}
232{-# INLINE readArrays #-}
231readArrays :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
233readArrays :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
232−readArrays = readArraysOfUpto defaultChunkSize
234+readArrays = readArraysOf defaultChunkSize
233
235
234-------------------------------------------------------------------------------
236-------------------------------------------------------------------------------
235-- Read File to Stream
237-- Read File to Stream
@@ -245,7 +247,7 @@ readArrays = readArraysOfUpto defaultChunkSize
245-- @since 0.7.0
247-- @since 0.7.0
246{-# INLINE readByChunksUpto #-}
248{-# INLINE readByChunksUpto #-}
247readByChunksUpto :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
249readByChunksUpto :: (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
249
251
250-- TODO
252-- TODO
251-- Generate a stream of elements of the given type from a file 'Handle'.
253-- 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
154-- handle it blocks until some data becomes available. If data is available
154-- handle it blocks until some data becomes available. If data is available
155-- then it immediately returns that data without blocking. It reads a maximum
155-- then it immediately returns that data without blocking. It reads a maximum
156-- of up to the size requested.
156-- of up to the size requested.
157−{-# INLINABLE readArrayUpto #-}
157+{-# INLINABLE readArrayOf #-}
158−readArrayUpto :: Int -> Socket -> IO (Array Word8)
158+readArrayOf :: Int -> Socket -> IO (Array Word8)
159−readArrayUpto = readArrayUptoWith recvBuf
159+readArrayOf = readArrayUptoWith recvBuf
160
160
161-------------------------------------------------------------------------------
161-------------------------------------------------------------------------------
162-- Array IO (output)
162-- Array IO (output)
@@ -217,14 +217,14 @@ readArraysUptoWith f size h = go
217 then stp
217 then stp
218 else yld arr go
218 else yld arr go
219
219
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@.
221-- The maximum size of a single array is limited to @size@.
221-- The maximum size of a single array is limited to @size@.
222-- 'fromHandleArraysUpto' ignores the prevailing 'TextEncoding' and 'NewlineMode'
222-- 'fromHandleArraysUpto' ignores the prevailing 'TextEncoding' and 'NewlineMode'
223-- on the 'Handle'.
223-- on the 'Handle'.
224−{-# INLINABLE readArraysUpto #-}
224+{-# INLINABLE readArraysOf #-}
225−readArraysUpto :: (IsStream t, MonadIO m)
225+readArraysOf :: (IsStream t, MonadIO m)
226 => Int -> Socket -> t m (Array Word8)
226 => Int -> Socket -> t m (Array Word8)
227−readArraysUpto = readArraysUptoWith readArrayUpto
227+readArraysOf = readArraysUptoWith readArrayOf
228
228
229-- XXX read 'Array a' instead of Word8
229-- XXX read 'Array a' instead of Word8
230--
230--
@@ -236,7 +236,7 @@ readArraysUpto = readArraysUptoWith readArrayUpto
236-- @since 0.7.0
236-- @since 0.7.0
237{-# INLINE readArrays #-}
237{-# INLINE readArrays #-}
238readArrays :: (IsStream t, MonadIO m) => Socket -> t m (Array Word8)
238readArrays :: (IsStream t, MonadIO m) => Socket -> t m (Array Word8)
239−readArrays = readArraysUpto A.defaultChunkSize
239+readArrays = readArraysOf A.defaultChunkSize
240
240
241-------------------------------------------------------------------------------
241-------------------------------------------------------------------------------
242-- Read File to Stream
242-- Read File to Stream