From 6436a5cebed11ee3d36a73c4f96c0a2814706375 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Mon, 28 Apr 2025 10:11:13 +0200 Subject: [PATCH] websocket: limit allocation for small sizes Fixes: 16f74c68aaa9 ("websocket: use max window bits of 15") We do not need to allocate 8kbytes for a small message --- rust/src/websocket/websocket.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rust/src/websocket/websocket.rs b/rust/src/websocket/websocket.rs index f86101743d..c1b6bbfa53 100644 --- a/rust/src/websocket/websocket.rs +++ b/rust/src/websocket/websocket.rs @@ -239,7 +239,12 @@ impl WebSocketState { buf.compress = false; // cf RFC 7692 section-7.2.2 tx.pdu.payload.extend_from_slice(&[0, 0, 0xFF, 0xFF]); - let mut v = Vec::with_capacity(WEBSOCKET_DECOMPRESS_BUF_SIZE); + let mut v = Vec::with_capacity(std::cmp::min( + WEBSOCKET_DECOMPRESS_BUF_SIZE, + // Do not allocate 8kbytes for a small size. + // Numbers here may be optimized. + 256 + 16 * tx.pdu.payload.len(), + )); if let Some(dec) = dec { let expect = dec.total_in() + tx.pdu.payload.len() as u64; let start = dec.total_in();