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).
This commit is contained in:
François Massot
2026-10-09 15:57:30 +02:00
parent d3afe68cc6
commit 0618bc5a13
+5
View File
@@ -191,7 +191,12 @@ impl VInt {
}
impl BinarySerializable for VInt {
#[inline]
fn serialize<W: Write + ?Sized>(&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])