From f7116e75a8392f24637a4c08a5e0600ded21fecb Mon Sep 17 00:00:00 2001 From: obchain Date: Thu, 18 Jun 2026 16:22:11 +0530 Subject: [PATCH] perf(ui/connection_table): borrow in Location, Service, Application cells Three cells in the per-connection-row render path allocated String values from data that can be borrowed directly. Location column: country_display() already returns &str borrowed from the GeoIpInfo. The to_string() call and NONE_PLACEHOLDER.to_string() fallback both allocated just to pass to Cell::from. With conn annotated as &'a Connection, Cell::from(&str) can hold the borrow for the row lifetime. Service column: service_text cloned the Option service_name only to pass the result as &str to truncate_with_ellipsis. Changed the return type to Cow<'a, str>: Cow::Borrowed for the service-name and placeholder cases (zero allocation), Cow::Owned only for the port-number branch where an owned String is unavoidable. Application column (compact branch): sort_key() returns &'static str. Computing budget first and passing sort_key() directly to truncate_with_ellipsis removes the intermediate .to_string() that converted a static string to a heap String just to pass it as &str. All 78 lib tests pass unchanged. Closes #407 --- src/ui/connection_table.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/ui/connection_table.rs b/src/ui/connection_table.rs index 6e9c7f5..51f5f7d 100644 --- a/src/ui/connection_table.rs +++ b/src/ui/connection_table.rs @@ -16,6 +16,8 @@ //! ellipsis only happens as a last resort at very narrow widths, after //! column hiding has already done its job. +use std::borrow::Cow; + use ratatui::layout::Constraint; use ratatui::style::{Color, Modifier, Style}; use ratatui::text::{Line, Span}; @@ -197,13 +199,14 @@ fn process_text(conn: &Connection) -> String { } /// Untruncated Service cell text: service name or port number. -fn service_text(conn: &Connection, ui_state: &UIState) -> String { +fn service_text<'a>(conn: &'a Connection, ui_state: &UIState) -> Cow<'a, str> { if ui_state.show_port_numbers { - conn.remote_addr.port().to_string() + Cow::Owned(conn.remote_addr.port().to_string()) } else { - conn.service_name - .clone() - .unwrap_or_else(|| NONE_PLACEHOLDER.to_string()) + match conn.service_name.as_deref() { + Some(name) => Cow::Borrowed(name), + None => Cow::Borrowed(NONE_PLACEHOLDER), + } } } @@ -336,7 +339,7 @@ fn staleness_style(conn: &Connection) -> (Option