From 93388e366c85ce151febeeb7e80c9c28a57ddf61 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 21 Aug 2025 13:58:19 +0200 Subject: [PATCH] radicle-term: Preallocate in Line::spaced() Signed-off-by: Matthias Beyer --- crates/radicle-term/src/element.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/radicle-term/src/element.rs b/crates/radicle-term/src/element.rs index 0f8f2515..feee19a9 100644 --- a/crates/radicle-term/src/element.rs +++ b/crates/radicle-term/src/element.rs @@ -183,13 +183,23 @@ impl Line { } /// Return a line with a single space between the given labels. + // TODO: Make this impl trivial once [`Iterator::intersperse`] is stable. pub fn spaced(items: impl IntoIterator) -> Self { - let mut line = Self::default(); - for item in items.into_iter() { - // Don't create spaces around empty labels. - if item.is_blank() { - continue; - } + let iter = items.into_iter(); + + let mut line = Self { + items: Vec::with_capacity({ + let (min, max) = iter.size_hint(); + let likely = max.unwrap_or(min); + + // Technically (likely + (likely - 1)), but we push the last space before + // we pop it again, so we need that additional space anyways. + likely * 2 + }), + }; + + // Don't create spaces around empty labels. + for item in iter.filter(|i| !i.is_blank()) { line.push(item); line.push(Label::space()); }