on master help sign in
use bracket instead of finally for better perf

finally in fromSocket seems to perform worse than using bracket in "withSocket" style.

commitf767cf5f5d0c62b9e177e4ad16c9ebee91962592 authorHarendra Kumar <harendra.kumar@gmail.com> dateSun 2 Jun 2019, 05:14 (7 years ago) parentb604937
1 file changed+27−12
src/Streamly/Network/Socket.hs+27−12
src/Streamly/Network/Socket.hs+27−12
@@ -32,9 +32,11 @@
3232
3333module Streamly.Network.Socket
3434 (
35+ -- * Use a socket
3536 withSocket
37+ , withSocketS
38+
3639 -- * Read from connection
37− , fromSocket
3840 , read
3941 -- , readUtf8
4042 -- , readLines
@@ -94,16 +96,35 @@ import qualified Streamly.Mem.Array as A
9496import qualified Streamly.Mem.Array.Types as A hiding (flattenArrays)
9597import qualified Streamly.Prelude as S
9698
97−-- | @'withSocket' socket act@ runs the computation @act@ passing the socket
98−-- handle to it. The handle will be closed on exit from 'withSocket', whether
99−-- by normal termination or by raising an exception. If closing the handle
100−-- raises an exception, then this exception will be raised by 'withSocket'
101−-- rather than any exception raised by 'act'.
99+-- | @'withSocket' socket act@ runs the monadic computation @act@ passing the
100+-- socket handle to it. The handle will be closed on exit from 'withSocket',
101+-- whether by normal termination or by raising an exception. If closing the
102+-- handle raises an exception, then this exception will be raised by
103+-- 'withSocket' rather than any exception raised by 'act'.
104+--
105+-- @since 0.7.0
106+{-# INLINE withSocket #-}
102107withSocket :: (MonadCatch m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
103108withSocket sk f = do
104109 f sk `onException` liftIO (Net.close sk)
105110 liftIO (Net.close sk)
106111
112+-- XXX bracket performs 2x better than 'finally'. That's perhaps because of
113+-- better fusion of "A.flattenArrays . readArrays"?
114+--
115+-- withSocketS :: (IsStream t, MonadCatch m, MonadIO m) => Socket -> t m Word8
116+-- withSocketS sk = A.flattenArrays $
117+-- S.finally (liftIO (Net.close sk)) (readArrays sk)
118+
119+-- | Like 'withSocket' but runs a streaming computation instead of a monadic
120+-- computation.
121+--
122+-- @since 0.7.0
123+{-# INLINE withSocketS #-}
124+withSocketS :: (IsStream t, MonadCatch m, MonadIO m)
125+ => Socket -> (Socket -> t m a) -> t m a
126+withSocketS sk = S.bracket (return sk) (liftIO . Net.close)
127+
107128-------------------------------------------------------------------------------
108129-- Array IO (Input)
109130-------------------------------------------------------------------------------
@@ -246,12 +267,6 @@ readByChunksUpto chunkSize h = A.flattenArrays $ readArraysUpto chunkSize h
246267read :: (IsStream t, MonadIO m) => Socket -> t m Word8
247268read = A.flattenArrays . readArrays
248269
249−-- | Read a stream of Word8 from a socket, closing the socket when done.
250−{-# INLINE fromSocket #-}
251−fromSocket :: (MonadCatch m, MonadIO m) => Socket -> SerialT m Word8
252−fromSocket sk = A.flattenArrays $
253− S.finally (liftIO (Net.close sk)) (readArrays sk)
254−
255270-------------------------------------------------------------------------------
256271-- Writing
257272-------------------------------------------------------------------------------