on master help sign in
enable 2x+ performance improvement for 10 MB+ messages by removing intermediate byte list in envelope parser
commit74ecaac619ad1165a4517d5c0f4e530493bda130 authorAgentM <agentm@themactionfaction.com> dateMon 11 Jan 2021, 23:53 (6 years ago) parent28ff1d0
2 files changed+16−10
unifiedsplit
cabal.project+1−1 src/Network/RPC/Curryer/Server.hs+15−9
cabal.project+1−1
@@ -3,4 +3,4 @@ packages: .
33source-repository-package
44 type: git
55 location: https://github.com/composewell/streamly.git
6− tag: ac3af8749194f1788704dda8667d0b3807075cc2
6+ tag: 4e48b52e8709390a47e99c107ebed2d1c9420076
src/Network/RPC/Curryer/Server.hs+15−9
@@ -1,4 +1,4 @@
1−{-# LANGUAGE DerivingVia, DeriveGeneric, RankNTypes, ScopedTypeVariables, MultiParamTypeClasses, OverloadedStrings, GeneralizedNewtypeDeriving, CPP, ExistentialQuantification, StandaloneDeriving, GADTs #-}
1+{-# LANGUAGE DerivingVia, DeriveGeneric, RankNTypes, ScopedTypeVariables, MultiParamTypeClasses, OverloadedStrings, GeneralizedNewtypeDeriving, CPP, ExistentialQuantification, StandaloneDeriving, GADTs, RecordWildCards #-}
22{-# OPTIONS_GHC -fno-warn-orphans #-}
33{- HLINT ignore "Use lambda-case" -}
44module Network.RPC.Curryer.Server where
@@ -35,13 +35,13 @@ import Data.Hashable
3535import System.Timeout
3636import qualified Network.ByteOrder as BO
3737
38+
3839-- for toArrayS conversion
39−{-import qualified Data.ByteString.Internal as BSI
40−import qualified Streamly.Internal.Data.Array.Storable.Foreign.Types as SA
41−import Foreign.ForeignPtr (plusForeignPtr)
40+import qualified Data.ByteString.Internal as BSI
41+import qualified Streamly.Internal.Data.Array.Storable.Foreign.Types as Arr
4242import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
43−import GHC.Ptr (plusPtr)
44−-}
43+import GHC.Ptr (minusPtr)
44+
4545-- define CURRYER_SHOW_BYTES 1
4646#if CURRYER_SHOW_BYTES == 1
4747import Debug.Trace
@@ -159,8 +159,7 @@ localHostAddr = (127,0,0,1)
159159-- Each message is length-prefixed by a 32-bit unsigned length.
160160envelopeP :: Parser IO Word8 Envelope
161161envelopeP = do
162− let s = FL.toList
163− msgTypeP = (P.satisfy (== 0) *>
162+ let msgTypeP = (P.satisfy (== 0) *>
164163 (RequestMessage . fromIntegral <$> word32P)) `P.alt`
165164 (P.satisfy (== 1) $> ResponseMessage) `P.alt`
166165 (P.satisfy (== 2) $> TimeoutResponseMessage) `P.alt`
@@ -171,7 +170,7 @@ envelopeP = do
171170 if c == 0 then
172171 pure BS.empty
173172 else
174− BS.pack <$> P.takeEQ c s
173+ fromArray <$> P.takeEQ c (Arr.writeN c)
175174 Envelope <$> fingerprintP <*> msgTypeP <*> uuidP <*> lenPrefixedByteStringP
176175
177176encodeEnvelope :: Envelope -> BS.ByteString
@@ -334,3 +333,10 @@ sendEnvelope envelope sockLock = do
334333fingerprint :: Typeable a => a -> Fingerprint
335334fingerprint = typeRepFingerprint . typeOf
336335
336+fromArray :: Arr.Array Word8 -> BSI.ByteString
337+fromArray Arr.Array {..}
338+ | aLen == 0 = mempty
339+ | otherwise = BSI.PS aStart 0 aLen
340+ where
341+ aStartPtr = unsafeForeignPtrToPtr aStart
342+ aLen = aEnd `minusPtr` aStartPtr