From 25ead28f90b9aebc9b8c4d7e5c5276a6990e0715 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 27 Sep 2026 19:03:54 +0200 Subject: [PATCH] Trim launcher overhead for terminals and web apps (#13423) omarchy-launch-webapp asked xdg-settings for the default browser with BROWSER still set to omarchy-launch-browser, which sends it down a slower path to the same answer: ~105ms instead of ~30ms on every web app launch. Unset it, as omarchy-launch-browser already does. omarchy-cmd-terminal-cwd found the focused terminal's shell with pgrep, which scans all of /proc on every new terminal. Read the terminal's child lists from /proc instead, keeping the newest child as before. Web app wrapper overhead drops from ~111ms to ~34ms, and a new terminal appears in ~77ms instead of ~90ms. Co-authored-by: Claude Opus 5.5 --- bin/omarchy-cmd-terminal-cwd | 11 +++++++- bin/omarchy-launch-webapp | 4 ++- test/shell.d/terminal-cwd-test.sh | 45 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 test/shell.d/terminal-cwd-test.sh diff --git a/bin/omarchy-cmd-terminal-cwd b/bin/omarchy-cmd-terminal-cwd index afb7d622..f44f1636 100755 --- a/bin/omarchy-cmd-terminal-cwd +++ b/bin/omarchy-cmd-terminal-cwd @@ -11,7 +11,16 @@ if [[ -S $kitty_socket ]]; then cwd=$(kitten @ --to "unix:$kitty_socket" ls --match "state:focused" 2>/dev/null | jq -r '.[].tabs[].windows[].cwd // empty') else - shell_pid=$(pgrep -P "$terminal_pid" | tail -n1) + # The newest child, as pgrep -P | tail -n1 found it, read from the kernel's + # child lists: pgrep scans all of /proc on every new terminal. + shell_pid="" + for children_file in /proc/"$terminal_pid"/task/*/children; do + [[ -r $children_file ]] || continue + read -ra children <"$children_file" || true + for child in "${children[@]}"; do + (( child > ${shell_pid:-0} )) && shell_pid=$child + done + done if [[ -n $shell_pid ]]; then cwd=$(readlink -f "/proc/$shell_pid/cwd" 2>/dev/null) diff --git a/bin/omarchy-launch-webapp b/bin/omarchy-launch-webapp index 6ee0f84a..ff4d0c6e 100755 --- a/bin/omarchy-launch-webapp +++ b/bin/omarchy-launch-webapp @@ -3,7 +3,9 @@ # omarchy:summary=Launch a URL as a web app in the default supported browser # omarchy:args= -browser=$(xdg-settings get default-web-browser) +# The session sets BROWSER to omarchy-launch-browser, which sends xdg-settings +# down a slower path (~100ms against ~30ms) to the same answer. +browser=$(env -u BROWSER xdg-settings get default-web-browser) case $browser in google-chrome* | brave* | microsoft-edge* | opera* | vivaldi* | helium*) ;; diff --git a/test/shell.d/terminal-cwd-test.sh b/test/shell.d/terminal-cwd-test.sh new file mode 100644 index 00000000..4584f698 --- /dev/null +++ b/test/shell.d/terminal-cwd-test.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +test_tmp=$(mktemp -d) +stub_bin="$test_tmp/bin" +work_dir="$test_tmp/project dir" +mkdir -p "$stub_bin" "$work_dir" + +# A stand-in terminal whose newest child is a shell sitting in work_dir. The +# trailing ":" keeps bash from exec'ing sleep, so the child stays a shell. +bash -c '(cd "$1" && bash -c "sleep 30; :") & wait' _ "$work_dir" & +terminal_pid=$! + +# Children first: killing a parent reparents its children out of reach. +kill_tree() { + local child + for child in $(cat /proc/"$1"/task/*/children 2>/dev/null); do + kill_tree "$child" + done + kill "$1" 2>/dev/null || true +} +trap 'kill_tree "$terminal_pid"; wait "$terminal_pid" 2>/dev/null || true; rm -rf "$test_tmp"' EXIT + +for _ in $(seq 50); do + [[ -n $(cat /proc/"$terminal_pid"/task/*/children 2>/dev/null) ]] && break + sleep 0.05 +done + +cat >"$stub_bin/hyprctl" <<'SH' +#!/bin/bash +[[ -n ${ACTIVE_PID:-} ]] && printf 'Window 1 -> terminal:\n\tpid: %s\n' "$ACTIVE_PID" +exit 0 +SH +chmod +x "$stub_bin/hyprctl" + +cwd=$(ACTIVE_PID="$terminal_pid" PATH="$stub_bin:$PATH" XDG_RUNTIME_DIR="$test_tmp" "$ROOT/bin/omarchy-cmd-terminal-cwd") +[[ $cwd == "$work_dir" ]] || fail "a new terminal opens in the focused terminal's shell directory" "got: $cwd" +pass "a new terminal opens in the focused terminal's shell directory" + +output=$(PATH="$stub_bin:$PATH" XDG_RUNTIME_DIR="$test_tmp" HOME="$test_tmp" "$ROOT/bin/omarchy-cmd-terminal-cwd" 2>&1) +[[ $output == "$test_tmp" ]] || fail "with no focused terminal the new one opens in HOME" "got: $output" +pass "with no focused terminal the new one opens in HOME, quietly"