minor optimization: reserve buffer once length is known when reading websockets

This commit is contained in:
Mohammad Rezaei 2022-12-21 16:28:09 -05:00 committed by Yuki Okushi
parent 6fdda45ca3
commit bf30243996
1 changed files with 5 additions and 0 deletions

View File

@ -1,3 +1,4 @@
use std::cmp::min;
use std::convert::TryFrom; use std::convert::TryFrom;
use bytes::{Buf, BufMut, BytesMut}; use bytes::{Buf, BufMut, BytesMut};
@ -96,6 +97,10 @@ impl Parser {
// not enough data // not enough data
if src.len() < idx + length { if src.len() < idx + length {
let min_length = min(length, max_size);
if src.capacity() < idx + min_length {
src.reserve(idx + min_length - src.capacity());
}
return Ok(None); return Ok(None);
} }