Skip to content

Commit

Permalink
rewrite Display::fmt() to prevent many allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
abysssol authored and cole-h committed Aug 8, 2024
1 parent 29bae01 commit 5a4e743
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions ofborg/src/maintainers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<String>>()
.join(", ")
)
})
.collect::<Vec<String>>()
.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(())
}
}

Expand Down

0 comments on commit 5a4e743

Please sign in to comment.