From 8cccd541250ec7ced88c5414dad0509b757d43ed Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Mon, 4 Mar 2024 00:27:13 +0100 Subject: [PATCH] avoid triple lookup in `m.flushed` yamux table (#1045) --- libp2p/muxers/yamux/yamux.nim | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/libp2p/muxers/yamux/yamux.nim b/libp2p/muxers/yamux/yamux.nim index 2a3e1f03e..9cb293353 100644 --- a/libp2p/muxers/yamux/yamux.nim +++ b/libp2p/muxers/yamux/yamux.nim @@ -516,16 +516,20 @@ method handle*(m: Yamux) {.async.} = await newStream.open() asyncSpawn m.handleStream(newStream) elif header.streamId notin m.channels: - if header.streamId notin m.flushed: - raise newException(YamuxError, "Unknown stream ID: " & $header.streamId) - elif header.msgType == Data: - # Flush the data - m.flushed[header.streamId].dec(int(header.length)) - if m.flushed[header.streamId] < 0: - raise newException(YamuxError, "Peer exhausted the recvWindow after reset") - if header.length > 0: - var buffer = newSeqUninitialized[byte](header.length) - await m.connection.readExactly(addr buffer[0], int(header.length)) + # Flush the data + m.flushed.withValue(header.streamId, flushed): + if header.msgType == Data: + flushed[].dec(int(header.length)) + if flushed[] < 0: + raise newException(YamuxError, + "Peer exhausted the recvWindow after reset") + do: + raise newException(YamuxError, + "Unknown stream ID: " & $header.streamId) + if header.length > 0: + var buffer = newSeqUninitialized[byte](header.length) + await m.connection.readExactly( + addr buffer[0], int(header.length)) continue let channel =