From 74a9e9f0f87b27d51d7ee551897ec04aa5c2592f Mon Sep 17 00:00:00 2001 From: William Yang Date: Wed, 11 Feb 2026 14:59:03 +0100 Subject: [PATCH] perf: faster vbi decode when strict-protocol-compliance is on. avoid alloc when decode vbi. Old code try to encode it with allocation. --- src/mqtt_serde/base_data.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/mqtt_serde/base_data.rs b/src/mqtt_serde/base_data.rs index a8cd6fd0..ee0b8eff 100644 --- a/src/mqtt_serde/base_data.rs +++ b/src/mqtt_serde/base_data.rs @@ -95,12 +95,11 @@ impl VariableByteInteger { } // MQTT 5.0 Protocol Compliance: Check for non-minimal VBI encoding + // Optimized check: A VBI is non-minimal if it has more than one byte and + // the last byte (where MSB=0) is zero (e.g., [0x80, 0x00]). #[cfg(feature = "strict-protocol-compliance")] { - // A VBI encoding is non-minimal if it could be represented with fewer bytes - // This happens when there are unnecessary continuation bytes - let minimal_encoded = VariableByteInteger::encode(value as u32); - if minimal_encoded.len() != i { + if i > 1 && buffer[i - 1] == 0 { return Err(ParseError::ParseError( "Variable Byte Integer encoding is not minimal".to_string(), ));