From 933f243dfc245a8795690e2ac6e8ea59d9b084dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Mon, 21 Oct 2024 09:28:32 +0200 Subject: [PATCH] Use `Box<[u8]>` instead of `Vec` in `Serialized` --- crates/hyperqueue/src/common/serialization.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/hyperqueue/src/common/serialization.rs b/crates/hyperqueue/src/common/serialization.rs index dc4ae881b..afc18b5ea 100644 --- a/crates/hyperqueue/src/common/serialization.rs +++ b/crates/hyperqueue/src/common/serialization.rs @@ -29,7 +29,7 @@ impl SerializationConfig for TrailingAllowedConfig { #[derive(Serialize, Deserialize)] pub struct Serialized { #[serde(with = "serde_bytes")] - data: Vec, + data: Box<[u8]>, _phantom: PhantomData<(T, C)>, } @@ -55,8 +55,11 @@ impl Clone for Serialized { impl Serialized { pub fn new(value: &T) -> bincode::Result { + let result = C::config().serialize(value)?; + // Check that we're not reallocating needlessly in `into_boxed_slice` + debug_assert_eq!(result.capacity(), result.len()); Ok(Self { - data: C::config().serialize(value)?, + data: result.into_boxed_slice(), _phantom: Default::default(), }) }