From 5a4e743f192fb151915fcbe8789922fa401ecf48 Mon Sep 17 00:00:00 2001 From: abysssol Date: Tue, 30 Jul 2024 13:02:41 -0400 Subject: [PATCH] rewrite `Display::fmt()` to prevent many allocations --- ofborg/src/maintainers.rs | 40 ++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/ofborg/src/maintainers.rs b/ofborg/src/maintainers.rs index e5b17c79..881e2d73 100644 --- a/ofborg/src/maintainers.rs +++ b/ofborg/src/maintainers.rs @@ -104,23 +104,29 @@ impl ImpactedMaintainers { impl std::fmt::Display for ImpactedMaintainers { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - let d = self - .0 - .iter() - .map(|(maintainer, packages)| { - format!( - "{}: {}", - maintainer.0, - packages - .iter() - .map(|pkg| pkg.0.clone()) - .collect::>() - .join(", ") - ) - }) - .collect::>() - .join("\n"); - write!(f, "{}", d) + let mut is_first = true; + for (Maintainer(maintainer), packages) in &self.0 { + if is_first { + is_first = false; + } else { + f.write_str("\n")?; + } + + f.write_fmt(format_args!("{maintainer}"))?; + + let (first, rest) = { + let mut packages = packages.iter(); + (packages.next(), packages) + }; + if let Some(Package(package)) = first { + f.write_fmt(format_args!(": {package}"))?; + + for Package(package) in rest { + f.write_fmt(format_args!(", {package}"))?; + } + } + } + Ok(()) } }