From 0618bc5a138cd1919f4ff7ebd6e05b6a2e89350c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Massot?= Date: Fri, 9 Oct 2026 15:57:30 +0200 Subject: [PATCH] perf: one-byte fast path for VInt::serialize Lengths and counts written while serializing documents are almost always below 128. Writing them through a 10-byte buffer and a variable-size write_all showed up as memcpy calls (~3% of the bulk load CPU). --- common/src/vint.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/common/src/vint.rs b/common/src/vint.rs index 22eaa267c..221e924df 100644 --- a/common/src/vint.rs +++ b/common/src/vint.rs @@ -191,7 +191,12 @@ impl VInt { } impl BinarySerializable for VInt { + #[inline] fn serialize(&self, writer: &mut W) -> io::Result<()> { + // Most VInts are lengths and counts below 128: one byte, no variable-size copy. + if self.0 < 128 { + return writer.write_all(&[self.0 as u8 | STOP_BIT]); + } let mut buffer = [0u8; 10]; let num_bytes = self.serialize_into(&mut buffer); writer.write_all(&buffer[0..num_bytes])