Skip to content

perf(ui/overview): use static strings in title and connector spans#412

Open
obchain wants to merge 1 commit into
domcyrus:mainfrom
obchain:perf/overview-static-strings
Open

perf(ui/overview): use static strings in title and connector spans#412
obchain wants to merge 1 commit into
domcyrus:mainfrom
obchain:perf/overview-static-strings

Conversation

@obchain

@obchain obchain commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Closes #411

What

Two allocation patterns eliminated in src/ui/tabs/overview.rs. All 78 lib tests pass unchanged.

1. connections_title — match on booleans instead of building a String

// before: 2–3 allocations per frame
let mut base = if ui_state.show_historic {
    "Active + Historic Connections".to_string()  // alloc
} else {
    "Active Connections".to_string()             // alloc
};
if grouped {
    base.push_str(" · Grouped by Process");       // may realloc
}
Span::styled(format!(" {base}"),)             // alloc

// after: 0 allocations per frame
let base: &'static str = match (ui_state.show_historic, grouped) {
    (false, false) => " Active Connections",
    (false, true)  => " Active Connections · Grouped by Process",
    (true,  false) => " Active + Historic Connections",
    (true,  true)  => " Active + Historic Connections · Grouped by Process",
};
Span::styled(base,)  // &'static str → Cow::Borrowed, no heap

The leading space is baked into each static string, eliminating the format!(" {base}") call entirely. Saves 2–3 allocations every frame.

2. Connector span in expanded-group rows

// before: 1 alloc per expanded connection per frame
Span::styled(connector.to_string(), theme::fg(theme::muted()))

// after: 0 allocs
Span::styled(connector, theme::fg(theme::muted()))

connector is &'static str (either " └─ " or " ├─ "). Span::styled accepts T: Into<Cow<'static, str>>, which &'static str satisfies directly. Saves 1 allocation per visible expanded-group connection per frame.

Verification

cargo build --workspace            # clean
cargo test --workspace --lib       # 78 passed, 0 failed
cargo fmt --check                  # clean
cargo clippy --all-targets -- -D warnings  # clean

connections_title built a mutable String from one of two static bases,
conditionally extended it with push_str, then wrapped it in format!(" {base}")
for the Span — two or three allocations per frame for content that is fully
determined by two boolean flags.

Replace with a match on (show_historic, grouped) that returns a &'static str
with the space already embedded; Span::styled accepts &'static str directly
via Into<Cow<'static, str>>, so the format!() call is also eliminated.

In the expanded-group render loop, connector is already a &'static str
("  └─ " / "  ├─ "). The to_string() call that converted it to an owned
String before passing to Span::styled was unnecessary — removed.

Closes domcyrus#411
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(ui/overview): eliminate String allocs in connections_title and per-row connector span

1 participant