on master help sign in
use bracket instead of finally for better perf
f767cf5Harendra Kumar
src/Streamly/Network/Socket.hs+27−12
@@ -32,9 +32,11 @@
32
32
33module Streamly.Network.Socket
33module Streamly.Network.Socket
34 (
34 (
35+ -- * Use a socket
35 withSocket
36 withSocket
37+ , withSocketS
38+
36 -- * Read from connection
39 -- * Read from connection
37− , fromSocket
38 , read
40 , read
39 -- , readUtf8
41 -- , readUtf8
40 -- , readLines
42 -- , readLines
@@ -94,16 +96,35 @@ import qualified Streamly.Mem.Array as A
94import qualified Streamly.Mem.Array.Types as A hiding (flattenArrays)
96import qualified Streamly.Mem.Array.Types as A hiding (flattenArrays)
95import qualified Streamly.Prelude as S
97import qualified Streamly.Prelude as S
96
98
97−-- | @'withSocket' socket act@ runs the computation @act@ passing the socket
99+-- | @'withSocket' socket act@ runs the monadic computation @act@ passing the
98−-- handle to it. The handle will be closed on exit from 'withSocket', whether
100+-- socket handle to it. The handle will be closed on exit from 'withSocket',
99−-- by normal termination or by raising an exception. If closing the handle
101+-- whether by normal termination or by raising an exception. If closing the
100−-- raises an exception, then this exception will be raised by 'withSocket'
102+-- handle raises an exception, then this exception will be raised by
101−-- rather than any exception raised by 'act'.
103+-- 'withSocket' rather than any exception raised by 'act'.
104+--
105+-- @since 0.7.0
106+{-# INLINE withSocket #-}
102withSocket :: (MonadCatch m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
107withSocket :: (MonadCatch m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
103withSocket sk f = do
108withSocket sk f = do
104 f sk `onException` liftIO (Net.close sk)
109 f sk `onException` liftIO (Net.close sk)
105 liftIO (Net.close sk)
110 liftIO (Net.close sk)
106
111
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+
107-------------------------------------------------------------------------------
128-------------------------------------------------------------------------------
108-- Array IO (Input)
129-- Array IO (Input)
109-------------------------------------------------------------------------------
130-------------------------------------------------------------------------------
@@ -246,12 +267,6 @@ readByChunksUpto chunkSize h = A.flattenArrays $ readArraysUpto chunkSize h
246read :: (IsStream t, MonadIO m) => Socket -> t m Word8
267read :: (IsStream t, MonadIO m) => Socket -> t m Word8
247read = A.flattenArrays . readArrays
268read = A.flattenArrays . readArrays
248
269
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−
255-------------------------------------------------------------------------------
270-------------------------------------------------------------------------------
256-- Writing
271-- Writing
257-------------------------------------------------------------------------------
272-------------------------------------------------------------------------------