From 0dedd1d1f1386e7b48757499fa8bfad89e9127f8 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Thu, 22 Jul 2021 18:18:29 +0700 Subject: [PATCH] Change `MessageReader` to use `with_capacity` Some of the collections inside `MessageReader` were created with `new()` instead of `with_capacity()` call. This patch fixes that or comments why it was not done in some places. --- src/link/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/link/mod.rs b/src/link/mod.rs index eeba2fe..db57956 100644 --- a/src/link/mod.rs +++ b/src/link/mod.rs @@ -20,6 +20,9 @@ const HEAD_UE_MESSAGE: u8 = 42; // Maximum allowed size of JSON message sent from ue-server. const MAX_UE_MESSAGE_LENGTH: usize = 25 * 1024 * 1024; +// We do not expect to receive more that this much messages at once from ue-server +const EXPECTED_LIMIT_TO_UE_MESSAGES: usize = 100; + custom_error! { pub ReadingStreamError InvalidHead{input: u8} = "Invalid byte used as a HEAD: {input}", MessageTooLong{length: usize} = "Message to receive is too long: {length}", @@ -63,8 +66,9 @@ impl MessageReader { reading_state: ReadingState::Head, read_bytes: 0, current_message_length: 0, + // Will be recreated with `with_capacity` in `push_byte()` current_message: Vec::new(), - read_messages: VecDeque::new(), + read_messages: VecDeque::with_capacity(EXPECTED_LIMIT_TO_UE_MESSAGES), next_received_bytes: 0, received_bytes: 0, }