diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..a9f42d6a --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-07-27 - [Avoid String allocations in eq checks] +**Learning:** In Rust, `str::to_lowercase()` eagerly allocates a new `String`. This is bad for simple checks against fixed ASCII strings like `"true"` or `"yes"`. +**Action:** Use `.eq_ignore_ascii_case()` directly on the borrowed string slice to avoid allocation. diff --git a/crates/forktty-ui-gtk/src/socket_cli/hooks/event.rs b/crates/forktty-ui-gtk/src/socket_cli/hooks/event.rs index 24915361..2bca7f2d 100644 --- a/crates/forktty-ui-gtk/src/socket_cli/hooks/event.rs +++ b/crates/forktty-ui-gtk/src/socket_cli/hooks/event.rs @@ -255,9 +255,11 @@ pub(in crate::socket_cli) fn hook_debug(context: &CliContext, message: &str) { } pub(in crate::socket_cli) fn is_truthy_env(key: &str) -> bool { - trimmed_env(key) - .map(|value| matches!(value.to_lowercase().as_str(), "1" | "true" | "yes")) - .unwrap_or(false) + // ⚡ Bolt optimization: Avoid eager `String` allocation from `.to_lowercase()` + // by using `eq_ignore_ascii_case` on the borrowed slice. Also simplify `map/unwrap_or` to `is_some_and`. + trimmed_env(key).is_some_and(|value| { + value == "1" || value.eq_ignore_ascii_case("true") || value.eq_ignore_ascii_case("yes") + }) } /// Hook event ordering must survive wall-clock steps (NTP, manual `date`):