Skip to content

Commit bb34c24

Browse files
committed
Avoid copying payload into an empty queue
Avoid copying the SDU payload into the queue if it was empty.
1 parent 1a6546f commit bb34c24

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

network-mux/src/Network/Mux/Ingress.hs

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module Network.Mux.Ingress
1212
) where
1313

1414
import Data.Array
15-
import Data.ByteString.Builder.Internal (lazyByteStringThreshold)
15+
import Data.ByteString.Builder.Internal (lazyByteStringInsert, lazyByteStringThreshold)
1616
import Data.ByteString.Lazy qualified as BL
1717
import Data.List (nub)
1818

@@ -119,7 +119,13 @@ demuxer ptcls bearer =
119119
(len, buf) <- readTVar q
120120
let len' = len + BL.length (msBlob sdu)
121121
if len' <= fromIntegral qMax
122-
then writeTVar q $ (len', buf <> (lazyByteStringThreshold 64 $ msBlob sdu))
122+
then do
123+
let buf' = if len == 0
124+
then -- Don't copy the payload if the queue was empty
125+
lazyByteStringInsert $ msBlob sdu
126+
else -- Copy payloads smaller than 128 bytes
127+
buf <> (lazyByteStringThreshold 128 $ msBlob sdu)
128+
writeTVar q $ (len', buf')
123129
else throwSTM $ IngressQueueOverRun (msNum sdu) (msDir sdu)
124130

125131
lookupMiniProtocol :: MiniProtocolDispatch m

0 commit comments

Comments
 (0)