Skip to content

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

Description

@obchain

Problem

Two allocation patterns in src/ui/tabs/overview.rs:

1. connections_title — two allocations per frame for static content

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");        // possible realloc
}
// …then:
Span::styled(format!(" {base}"),)              // alloc

The four possible combinations of (show_historic, grouped) each produce a distinct static string. A single match covers all four without allocating:

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,)   // zero alloc: &'static str → Cow::Borrowed

The leading space is embedded in each static string, so the format!(" {base}") call is also eliminated. Saves 2+ heap allocations per frame.

2. Per-row connector — connector.to_string() in the expanded-group render loop

let connector: &'static str = if is_last { "  └─ " } else { "  ├─ " };
// …
Span::styled(connector.to_string(), theme::fg(theme::muted()))  // alloc per row

connector is already &'static str. Span::styled accepts T: Into<Cow<'static, str>>, which &'static str satisfies directly — to_string() is unnecessary.

Span::styled(connector, theme::fg(theme::muted()))  // zero alloc

Saves 1 heap allocation per expanded connection per frame.

Impact

  • connections_title: −2 to −3 allocations every frame regardless of what is visible.
  • Connector: −1 allocation per visible expanded-group connection per frame.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions