Skip to content

Commit

Permalink
Avoid copying payload into an empty queue
Browse files Browse the repository at this point in the history
Avoid copying the SDU payload into the queue if it was empty.
  • Loading branch information
karknu committed Mar 6, 2025
1 parent 43acc60 commit 4d6a898
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions network-mux/src/Network/Mux/Ingress.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Network.Mux.Ingress
) where

import Data.Array
import Data.ByteString.Builder.Internal (lazyByteStringThreshold)
import Data.ByteString.Builder.Internal (lazyByteStringInsert, lazyByteStringThreshold)
import Data.ByteString.Lazy qualified as BL
import Data.List (nub)

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

lookupMiniProtocol :: MiniProtocolDispatch m
Expand Down

0 comments on commit 4d6a898

Please sign in to comment.