on master help sign in
Rename Socket BufferOf APIs
commit4f70b0d4e67a0bed5796f0e908c7390d8085b11e authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateWed 30 Mar 2022, 20:25 (5 years ago) parent1d106e3
4 files changed+103−52
docs/Changelog.md+6 src/Streamly/Internal/Network/Socket.hs+78−39 src/Streamly/Network/Socket.hs+10−4 test/Streamly/Test/Network/Socket.hs+9−9
docs/Changelog.md+6
@@ -1,7 +1,13 @@
11# Changelog
22
33<!-- See rendered changelog at https://streamly.composewell.com -->
4+## Unreleased
45
6+* Following APIs have been renamed in Streamly.Network.Socket module:
7+ readWithBufferOf -> readWith
8+ readChunksWithBufferOf -> readChunksWith
9+ writeWithBufferOf -> writeWith
10+ writeChunksWithBufferOf -> writeChunksWith
511## 0.8.2 (Mar 2022)
612
713* Fix performance issues for GHC-9. These changes coupled with GHC changes
src/Streamly/Internal/Network/Socket.hs+78−39
@@ -24,7 +24,7 @@ module Streamly.Internal.Network.Socket
2424
2525 -- * Read from connection
2626 , read
27− , readWithBufferOf
27+ , readWith
2828 -- , readUtf8
2929 -- , readLines
3030 -- , readFrames
@@ -35,9 +35,9 @@ module Streamly.Internal.Network.Socket
3535 -- , readChunksUpto
3636 , readChunk
3737 , readChunks
38− , readChunksWithBufferOf
38+ , readChunksWith
3939
40− , toChunksWithBufferOf
40+ , toChunksWith
4141 , toChunks
4242 , toBytes
4343
@@ -46,19 +46,25 @@ module Streamly.Internal.Network.Socket
4646 -- , writeUtf8
4747 -- , writeUtf8ByLines
4848 -- , writeByFrames
49− , writeWithBufferOf
50− , writeMaybesWithBufferOf
49+ , writeWith
50+ , writeMaybesWith
5151
5252 , putChunks
53− , putBytesWithBufferOf
53+ , putBytesWith
5454 , putBytes
5555
5656 -- -- * Array Write
5757 , writeChunk
5858 , writeChunks
59− , writeChunksWithBufferOf
59+ , writeChunksWith
6060
6161 -- reading/writing datagrams
62+
63+ -- * Deprecated
64+ , readWithBufferOf
65+ , readChunksWithBufferOf
66+ , writeWithBufferOf
67+ , writeChunksWithBufferOf
6268 )
6369where
6470
@@ -337,15 +343,15 @@ _readChunksUptoWith f size h = go
337343 then stp
338344 else yld arr go
339345
340−-- | @toChunksWithBufferOf size h@ reads a stream of arrays from file handle @h@.
346+-- | @toChunksWith size h@ reads a stream of arrays from file handle @h@.
341347-- The maximum size of a single array is limited to @size@.
342348-- 'fromHandleArraysUpto' ignores the prevailing 'TextEncoding' and 'NewlineMode'
343349-- on the 'Handle'.
344−{-# INLINE_NORMAL toChunksWithBufferOf #-}
345−toChunksWithBufferOf :: (IsStream t, MonadIO m)
350+{-# INLINE_NORMAL toChunksWith #-}
351+toChunksWith :: (IsStream t, MonadIO m)
346352 => Int -> Socket -> t m (Array Word8)
347−-- toChunksWithBufferOf = _readChunksUptoWith readChunk
348−toChunksWithBufferOf size h = fromStreamD (D.Stream step ())
353+-- toChunksWith = _readChunksUptoWith readChunk
354+toChunksWith size h = fromStreamD (D.Stream step ())
349355 where
350356 {-# INLINE_LATE step #-}
351357 step _ _ = do
@@ -363,17 +369,17 @@ toChunksWithBufferOf size h = fromStreamD (D.Stream step ())
363369-- @since 0.7.0
364370{-# INLINE toChunks #-}
365371toChunks :: (IsStream t, MonadIO m) => Socket -> t m (Array Word8)
366−toChunks = toChunksWithBufferOf defaultChunkSize
372+toChunks = toChunksWith defaultChunkSize
367373
368374-- | Unfold the tuple @(bufsize, socket)@ into a stream of 'Word8' arrays.
369375-- Read requests to the socket are performed using a buffer of size @bufsize@.
370376-- The size of an array in the resulting stream is always less than or equal to
371377-- @bufsize@.
372378--
373−-- @since 0.7.0
374−{-# INLINE_NORMAL readChunksWithBufferOf #-}
375−readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
376−readChunksWithBufferOf = Unfold step return
379+-- @since 0.9.0
380+{-# INLINE_NORMAL readChunksWith #-}
381+readChunksWith :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
382+readChunksWith = Unfold step return
377383 where
378384 {-# INLINE_LATE step #-}
379385 step (size, h) = do
@@ -383,6 +389,14 @@ readChunksWithBufferOf = Unfold step return
383389 0 -> D.Stop
384390 _ -> D.Yield arr (size, h)
385391
392+-- | Same as 'readChunksWith'
393+--
394+-- @since 0.7.0
395+{-# DEPRECATED readChunksWithBufferOf "Please use 'readChunksWith' instead" #-}
396+{-# INLINE_NORMAL readChunksWithBufferOf #-}
397+readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Socket) (Array Word8)
398+readChunksWithBufferOf = readChunksWith
399+
386400-- | Unfolds a socket into a stream of 'Word8' arrays. Requests to the socket
387401-- are performed using a buffer of size
388402-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'. The
@@ -392,7 +406,7 @@ readChunksWithBufferOf = Unfold step return
392406-- @since 0.7.0
393407{-# INLINE readChunks #-}
394408readChunks :: MonadIO m => Unfold m Socket (Array Word8)
395−readChunks = UF.supplyFirst defaultChunkSize readChunksWithBufferOf
409+readChunks = UF.supplyFirst defaultChunkSize readChunksWith
396410
397411-------------------------------------------------------------------------------
398412-- Read File to Stream
@@ -403,13 +417,13 @@ readChunks = UF.supplyFirst defaultChunkSize readChunksWithBufferOf
403417-- also control the read throughput in mbps or IOPS.
404418
405419{-
406−-- | @readWithBufferOf bufsize handle@ reads a byte stream from a file
420+-- | @readWith bufsize handle@ reads a byte stream from a file
407421-- handle, reads are performed in chunks of up to @bufsize@. The stream ends
408422-- as soon as EOF is encountered.
409423--
410−{-# INLINE readWithBufferOf #-}
411−readWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
412−readWithBufferOf chunkSize h = A.flattenArrays $ readChunksUpto chunkSize h
424+{-# INLINE readWith #-}
425+readWith :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
426+readWith chunkSize h = A.flattenArrays $ readChunksUpto chunkSize h
413427-}
414428
415429-- TODO
@@ -427,10 +441,18 @@ toBytes = AS.concat . toChunks
427441-- | Unfolds the tuple @(bufsize, socket)@ into a byte stream, read requests
428442-- to the socket are performed using buffers of @bufsize@.
429443--
444+-- @since 0.9.0
445+{-# INLINE readWith #-}
446+readWith :: MonadIO m => Unfold m (Int, Socket) Word8
447+readWith = UF.many A.read readChunksWith
448+
449+-- | Same as 'readWith'
450+--
430451-- @since 0.7.0
452+{-# DEPRECATED readWithBufferOf "Please use 'readWith' instead" #-}
431453{-# INLINE readWithBufferOf #-}
432454readWithBufferOf :: MonadIO m => Unfold m (Int, Socket) Word8
433−readWithBufferOf = UF.many A.read readChunksWithBufferOf
455+readWithBufferOf = readWith
434456
435457-- | Unfolds a 'Socket' into a byte stream. IO requests to the socket are
436458-- performed in sizes of
@@ -439,7 +461,7 @@ readWithBufferOf = UF.many A.read readChunksWithBufferOf
439461-- @since 0.7.0
440462{-# INLINE read #-}
441463read :: MonadIO m => Unfold m Socket Word8
442−read = UF.supplyFirst defaultChunkSize readWithBufferOf
464+read = UF.supplyFirst defaultChunkSize readWith
443465
444466-------------------------------------------------------------------------------
445467-- Writing
@@ -461,17 +483,26 @@ putChunks h = S.mapM_ (liftIO . writeChunk h)
461483writeChunks :: (MonadIO m, Storable a) => Socket -> Fold m (Array a) ()
462484writeChunks h = FL.drainBy (liftIO . writeChunk h)
463485
464−-- | @writeChunksWithBufferOf bufsize socket@ writes a stream of arrays to
486+-- | @writeChunksWith bufsize socket@ writes a stream of arrays to
465487-- @socket@ after coalescing the adjacent arrays in chunks of @bufsize@.
466488-- Multiple arrays are coalesed as long as the total size remains below the
467489-- specified size. It never splits an array, if a single array is bigger than
468490-- the specified size it emitted as it is.
469491--
470−-- @since 0.8.0
492+-- @since 0.9.0
493+{-# INLINE writeChunksWith #-}
494+writeChunksWith :: (MonadIO m, Storable a)
495+ => Int -> Socket -> Fold m (Array a) ()
496+writeChunksWith n h = lpackArraysChunksOf n (writeChunks h)
497+
498+-- | Same as 'writeChunksWith'
499+--
500+-- @since 0.7.0
501+{-# DEPRECATED writeChunksWithBufferOf "Please use 'writeChunksWith' instead" #-}
471502{-# INLINE writeChunksWithBufferOf #-}
472503writeChunksWithBufferOf :: (MonadIO m, Storable a)
473504 => Int -> Socket -> Fold m (Array a) ()
474−writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
505+writeChunksWithBufferOf = writeChunksWith
475506
476507-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
477508--
@@ -485,33 +516,41 @@ writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
485516-- be written to the IO device as soon as we collect the specified number of
486517-- input elements.
487518--
488−-- @since 0.7.0
489−{-# INLINE putBytesWithBufferOf #-}
490−putBytesWithBufferOf :: MonadIO m => Int -> Socket -> SerialT m Word8 -> m ()
491−putBytesWithBufferOf n h m = putChunks h $ AS.arraysOf n m
519+-- @since 0.9.0
520+{-# INLINE putBytesWith #-}
521+putBytesWith :: MonadIO m => Int -> Socket -> SerialT m Word8 -> m ()
522+putBytesWith n h m = putChunks h $ AS.arraysOf n m
492523
493524-- | Write a byte stream to a socket. Accumulates the input in chunks of
494525-- specified number of bytes before writing.
495526--
527+-- @since 0.9.0
528+{-# INLINE writeWith #-}
529+writeWith :: MonadIO m => Int -> Socket -> Fold m Word8 ()
530+writeWith n h = FL.chunksOf n (A.writeNUnsafe n) (writeChunks h)
531+
532+-- | Same as 'writeWith'
533+--
496534-- @since 0.7.0
535+{-# DEPRECATED writeWithBufferOf "Please use 'writeWith' instead" #-}
497536{-# INLINE writeWithBufferOf #-}
498537writeWithBufferOf :: MonadIO m => Int -> Socket -> Fold m Word8 ()
499−writeWithBufferOf n h = FL.chunksOf n (A.writeNUnsafe n) (writeChunks h)
538+writeWithBufferOf = writeWith
500539
501540-- | Write a stream of 'Maybe' values. Keep buffering the 'Just' values in an
502541-- array. Write the array to the 'Handle' as soon as a 'Nothing' is encountered
503542-- or the buffer size exceeds the specified limit.
504543--
505544-- /Pre-release/
506−{-# INLINE writeMaybesWithBufferOf #-}
507−writeMaybesWithBufferOf :: (MonadIO m )
545+{-# INLINE writeMaybesWith #-}
546+writeMaybesWith :: (MonadIO m )
508547 => Int -> Socket -> Fold m (Maybe Word8) ()
509−writeMaybesWithBufferOf n h =
548+writeMaybesWith n h =
510549 let writeNJusts = FL.lmap fromJust $ A.writeN n
511550 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
512551 in FL.many writeOnNothing (writeChunks h)
513552
514−-- > write = 'writeWithBufferOf' defaultChunkSize
553+-- > write = 'writeWith' defaultChunkSize
515554--
516555-- | Write a byte stream to a file handle. Combines the bytes in chunks of size
517556-- up to 'defaultChunkSize' before writing. Note that the write behavior
@@ -520,19 +559,19 @@ writeMaybesWithBufferOf n h =
520559-- @since 0.7.0
521560{-# INLINE putBytes #-}
522561putBytes :: MonadIO m => Socket -> SerialT m Word8 -> m ()
523−putBytes = putBytesWithBufferOf defaultChunkSize
562+putBytes = putBytesWith defaultChunkSize
524563
525564-- | Write a byte stream to a socket. Accumulates the input in chunks of
526565-- up to 'defaultChunkSize' bytes before writing.
527566--
528567-- @
529−-- write = 'writeWithBufferOf' 'defaultChunkSize'
568+-- write = 'writeWith' 'defaultChunkSize'
530569-- @
531570--
532571-- @since 0.7.0
533572{-# INLINE write #-}
534573write :: MonadIO m => Socket -> Fold m Word8 ()
535−write = writeWithBufferOf defaultChunkSize
574+write = writeWith defaultChunkSize
536575
537576{-
538577{-# INLINE write #-}
src/Streamly/Network/Socket.hs+10−4
@@ -101,20 +101,26 @@ module Streamly.Network.Socket
101101
102102 -- * Read
103103 , read
104− , readWithBufferOf
104+ , readWith
105105 , readChunks
106− , readChunksWithBufferOf
106+ , readChunksWith
107107 , readChunk
108108
109109 -- * Write
110110 , write
111− , writeWithBufferOf
111+ , writeWith
112112 , writeChunks
113− , writeChunksWithBufferOf
113+ , writeChunksWith
114114 , writeChunk
115115
116116 -- * Exceptions
117117 , forSocketM
118+
119+ -- * Deprecated
120+ , readWithBufferOf
121+ , readChunksWithBufferOf
122+ , writeWithBufferOf
123+ , writeChunksWithBufferOf
118124 )
119125where
120126
test/Streamly/Test/Network/Socket.hs+9−9
@@ -42,7 +42,7 @@ testDataSource = concat $ replicate 1000 testData
4242------------------------------------------------------------------------------
4343handlerChunksWithBuffer :: Socket -> IO ()
4444handlerChunksWithBuffer sk =
45− Stream.unfold Socket.readChunksWithBufferOf (100, sk)
45+ Stream.unfold Socket.readChunksWith (100, sk)
4646 & Stream.fold (Socket.writeChunks sk)
4747 & discard
4848
@@ -54,8 +54,8 @@ handlerChunks sk =
5454
5555handlerwithbuffer :: Socket -> IO ()
5656handlerwithbuffer sk =
57− Stream.unfold Socket.readWithBufferOf (100, sk)
58− & Stream.fold (Socket.writeWithBufferOf 100 sk)
57+ Stream.unfold Socket.readWith (100, sk)
58+ & Stream.fold (Socket.writeWith 100 sk)
5959 & discard
6060
6161handlerRW :: Socket -> IO ()
@@ -108,8 +108,8 @@ execute port size handler = do
108108 & Stream.finally (killThread tid)
109109 return lst
110110
111−validateWithBufferOf :: Property
112−validateWithBufferOf = monadicIO $ do
111+validateWith :: Property
112+validateWith = monadicIO $ do
113113 res <- run $ do
114114 ls2 <- execute basePort 45000 handlerwithbuffer
115115 Stream.eqBy (==) (Stream.fromList testDataSource) ls2
@@ -130,8 +130,8 @@ validateChunks = monadicIO $ do
130130 Stream.eqBy (==) (Stream.fromList testDataSource) ls2
131131 assert res
132132
133−validateChunksWithBufferOf :: Property
134−validateChunksWithBufferOf = monadicIO $ do
133+validateChunksWith :: Property
134+validateChunksWith = monadicIO $ do
135135 res <- run $ do
136136 ls2 <- execute (basePort + 3) 45000 handlerChunksWithBuffer
137137 Stream.eqBy (==) (Stream.fromList testDataSource) ls2
@@ -146,6 +146,6 @@ main = hspec $ do
146146 describe moduleName $ do
147147 describe "Read/Write" $ do
148148 prop "read/write" validateRW
149− prop "readWithBufferOf/writeWithBufferOf" validateWithBufferOf
149+ prop "readWith/writeWith" validateWith
150150 prop "readChunks/writeChunks" validateChunks
151− prop "readChunksWithBufferOf" validateChunksWithBufferOf
151+ prop "readChunksWith" validateChunksWith