From 2172cbb8efc91f553348903615cfc2a122d21462 Mon Sep 17 00:00:00 2001 From: Julian Eager Date: Wed, 8 Nov 2023 22:06:22 +0800 Subject: [PATCH 1/2] tighten varint encoding --- src/runes/varint.rs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/runes/varint.rs b/src/runes/varint.rs index debef86e44..e15ff5810c 100644 --- a/src/runes/varint.rs +++ b/src/runes/varint.rs @@ -11,21 +11,12 @@ pub fn encode_to_vec(mut n: u128, v: &mut Vec) { let mut out = [0; 19]; let mut i = 18; - loop { - let mut byte = n.to_le_bytes()[0] & 0b0111_1111; - - if i < 18 { - byte |= 0b1000_0000; - } - - out[i] = byte; - - if n < 0b1000_0000 { - break; - } + out[i] = n.to_le_bytes()[0] & 0x7f; + while n >= 0x80 { n = n / 128 - 1; i -= 1; + out[i] = n.to_le_bytes()[0] & 0x7f | 0x80; } v.extend_from_slice(&out[i..]); From a01dfd79740ed7b8eeecfc23a264ab360bf36ebd Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Wed, 8 Nov 2023 16:27:22 -0800 Subject: [PATCH 2/2] Use binary literals --- src/runes/varint.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/runes/varint.rs b/src/runes/varint.rs index e15ff5810c..2f088cd222 100644 --- a/src/runes/varint.rs +++ b/src/runes/varint.rs @@ -11,12 +11,12 @@ pub fn encode_to_vec(mut n: u128, v: &mut Vec) { let mut out = [0; 19]; let mut i = 18; - out[i] = n.to_le_bytes()[0] & 0x7f; + out[i] = n.to_le_bytes()[0] & 0b0111_1111; - while n >= 0x80 { + while n > 0b0111_1111 { n = n / 128 - 1; i -= 1; - out[i] = n.to_le_bytes()[0] & 0x7f | 0x80; + out[i] = n.to_le_bytes()[0] | 0b1000_0000; } v.extend_from_slice(&out[i..]);