From 47c0edc3f6ded8bbf960aa0cee8eae77907b84ad Mon Sep 17 00:00:00 2001 From: Link Mauve Date: Tue, 9 Jun 2026 00:08:52 +0200 Subject: [PATCH] xmpp-parsers: Optimize to_hex() a bit Just one correctly-sized String allocation, instead of one String per byte, a Vec, and then a final String. skip-changelog: Internal change. --- parsers/src/hashes.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/parsers/src/hashes.rs b/parsers/src/hashes.rs index f116a481..6f6592b6 100644 --- a/parsers/src/hashes.rs +++ b/parsers/src/hashes.rs @@ -6,6 +6,7 @@ use alloc::borrow::Cow; use core::{ + fmt::Write, num::ParseIntError, ops::{Deref, DerefMut}, str::FromStr, @@ -181,11 +182,11 @@ impl Hash { /// Formats this hash into hexadecimal. pub fn to_hex(&self) -> String { - self.hash - .iter() - .map(|byte| format!("{:02x}", byte)) - .collect::>() - .join("") + let mut hex = String::with_capacity(self.hash.len() * 2); + for byte in &self.hash { + write!(&mut hex, "{:02x}", byte).unwrap(); + } + hex } /// Formats this hash into colon-separated hexadecimal.