diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..d2a1ba7d --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2023-10-27 - [Avoid redundant clone during iterator evaluation in recursive structure] +**Learning:** When searching recursively in a tree-like structure (e.g. `PaneTree` using `PaneNode`), passing string or collection ownership via `.clone()` to an iterator predicate like `.any()` leads to unnecessary allocations. The condition may fail in multiple children, resulting in an O(N) allocation overhead just to verify a negative match. +**Action:** When passing parameters that eventually need to be owned inside nested traversal functions, pass them as references through the call stack and invoke `.clone()` only at the exact point of true insertion/ownership. diff --git a/crates/forktty-core/src/model.rs b/crates/forktty-core/src/model.rs index c86541a3..8b5761d9 100644 --- a/crates/forktty-core/src/model.rs +++ b/crates/forktty-core/src/model.rs @@ -906,7 +906,7 @@ impl WorkspaceModel { .workspaces .get_mut(&workspace_id) .expect("workspace verified above"); - if !push_tab_to_leaf(&mut workspace.pane_tree, near_surface_id, new_id.clone()) { + if !push_tab_to_leaf(&mut workspace.pane_tree, near_surface_id, &new_id) { return None; } workspace.focused_surface_id = new_id.clone(); diff --git a/crates/forktty-core/src/model/pane_tree.rs b/crates/forktty-core/src/model/pane_tree.rs index 2a3f7fc5..f3a256d2 100644 --- a/crates/forktty-core/src/model/pane_tree.rs +++ b/crates/forktty-core/src/model/pane_tree.rs @@ -646,12 +646,12 @@ pub(super) fn set_leaf_active_for_surface(node: &mut PaneNode, surface_id: &str) pub(super) fn push_tab_to_leaf( node: &mut PaneNode, near_surface_id: &str, - new_tab_id: SurfaceId, + new_tab_id: &SurfaceId, ) -> bool { match node { PaneNode::Leaf { tabs, active } => { if tabs.iter().any(|id| id == near_surface_id) { - tabs.push(new_tab_id); + tabs.push(new_tab_id.clone()); *active = tabs.len() - 1; true } else { @@ -660,6 +660,6 @@ pub(super) fn push_tab_to_leaf( } PaneNode::Split { children, .. } => children .iter_mut() - .any(|child| push_tab_to_leaf(child, near_surface_id, new_tab_id.clone())), + .any(|child| push_tab_to_leaf(child, near_surface_id, new_tab_id)), } } diff --git a/crates/forktty-ui-gtk/src/gtk_app/terminal_input.rs b/crates/forktty-ui-gtk/src/gtk_app/terminal_input.rs index 35435d3b..3211b614 100644 --- a/crates/forktty-ui-gtk/src/gtk_app/terminal_input.rs +++ b/crates/forktty-ui-gtk/src/gtk_app/terminal_input.rs @@ -140,14 +140,11 @@ pub(super) fn translate_gtk_key( return Some(TerminalInput::Bytes(text.as_bytes().to_vec())); } } - if let Some(ch) = key.to_unicode() { - GhosttyKeySpec { - key: GhosttyKey::Char(ch), - ctrl, - alt, - } - } else { - return None; + let ch = key.to_unicode()?; + GhosttyKeySpec { + key: GhosttyKey::Char(ch), + ctrl, + alt, } } }; diff --git a/crates/forktty-ui-gtk/src/socket_cli.rs b/crates/forktty-ui-gtk/src/socket_cli.rs index 85ec0380..8bf17042 100644 --- a/crates/forktty-ui-gtk/src/socket_cli.rs +++ b/crates/forktty-ui-gtk/src/socket_cli.rs @@ -81,6 +81,8 @@ use help::{ use hooks::event::*; #[cfg(test)] use hooks::handle_hooks; +#[cfg(feature = "gtk-ghostty")] +#[allow(unused_imports)] pub(crate) use hooks::hook_setup_reminder_message; #[cfg(test)] use hooks::install::*; diff --git a/crates/forktty-ui-gtk/src/telemetry.rs b/crates/forktty-ui-gtk/src/telemetry.rs index 69ccb559..c53c8bf7 100644 --- a/crates/forktty-ui-gtk/src/telemetry.rs +++ b/crates/forktty-ui-gtk/src/telemetry.rs @@ -15,6 +15,7 @@ struct TelemetryStamp { last_ping_date: String, } +#[allow(dead_code)] pub fn maybe_start_anonymous_ping(config: &forktty_core::AppConfig) { maybe_start_anonymous_ping_with(config, telemetry_stamp_path(), now_unix_seconds(), true); } @@ -50,12 +51,14 @@ fn maybe_start_anonymous_ping_with( }); } +#[allow(dead_code)] fn telemetry_stamp_path() -> Option { dirs::state_dir() .or_else(dirs::data_local_dir) .map(|base| base.join("forktty").join(TELEMETRY_STAMP_FILE)) } +#[allow(dead_code)] fn now_unix_seconds() -> i64 { SystemTime::now() .duration_since(UNIX_EPOCH)