From 6a67961c79fc61883291789c3e92d0cf78b54fd7 Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Thu, 12 Oct 2023 11:15:09 +0200 Subject: [PATCH] quic: Avoid bytes for VariableLengthInteger Allocation of bytes objects due to parsing and usage of pack and the invocation of to_uint() showed significantly in profiles (3.3% sample matches). Switch to a more procedural approach to avoid the allocation overhead. --- analyzer/QUIC.spicy | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/analyzer/QUIC.spicy b/analyzer/QUIC.spicy index e7276f2..71a0452 100644 --- a/analyzer/QUIC.spicy +++ b/analyzer/QUIC.spicy @@ -141,16 +141,21 @@ type VariableLengthInteger = unit { # https://datatracker.ietf.org/doc/rfc9000/ # Section 16 and Appendix A # - first_byte: bytes &size=1 { - local uint8_val = uint8($$.to_uint(spicy::ByteOrder::Big)); - self.bytes_to_parse = 2**((0xC0 & uint8_val) >> 6); - # Re-pack without most significant two bits for later use. - self.first_byte = pack(0x3f & uint8_val, spicy::ByteOrder::Big); + byte0: uint8 { + self.bytes_to_parse = 2**((0xC0 & $$) >> 6); + self.result = $$ & 0x3F; } - remaining_bytes: bytes &size=self.bytes_to_parse - 1; - on %done { - self.result = (self.first_byte + self.remaining_bytes).to_uint(spicy::ByteOrder::Big); + byte1: uint8 if ( self.bytes_to_parse > 1 ) { + self.result = (self.result << 8) | $$; + } + + byte2: uint8 if ( self.bytes_to_parse > 2 ) { + self.result = (self.result << 8) | $$; + } + + byte3: uint8 if ( self.bytes_to_parse > 3 ) { + self.result = (self.result << 8) | $$; } };