I Built a Pure-Zsh Prompt Engine That Renders in 0.6ms (Without Rust or C++)
1. The Core Performance Invariant: Zero Subshell Forks
In shell scripting, subshells are expensive:
# This forks a new process:
current_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
# This forks another process:
formatted=$(echo "$current_branch" | tr '[:lower:]' '[:upper:]')
If your prompt executes 4 to 8 subshells every time you press Enter, prompt rendering latency quickly jumps to 15-50 ms. On systems with endpoint security scanners (e.g., macOS Endpoint Security or Windows Defender on WSL), process execution overhead can be even higher.
In zline, the synchronous render path has a strict rule enforced by automated CI audits: 0 subshells allowed. Everything on the critical render path relies strictly on native Zsh builtins and parameter expansion:
- Instead of
sedortr: Native parameter substring replacement${var//search/replace} - Instead of
basenameordirname: Path modifiers${var:t}and${var:h} - Instead of
wc -l: Array length expansions${#array}and math evaluation$(( ... )) - Instead of
cut: Splitting expansion${(s/:/)var} - Instead of
join: Joining expansion${(j: :)items}
2. Two-Phase Git Status Detection
Git repositories are the primary bottleneck for shell prompts. Running git status synchronously in a 500k-file repository can freeze your prompt for seconds. zline uses a two-phase architecture:
Phase 1: Direct File System Read (0.07 ms)
Before touching the git binary, zline checks .git/HEAD directly using pure Zsh file descriptors:
_zline_git_read_head() {
emulate -L zsh
local dir="$1"
local git_path="${dir}/.git"
# Handle .git worktrees / submodules
if [[ -f "$git_path" ]]; then
local line
read -r line < "$git_path" 2>/dev/null
if [[ "$line" == "gitdir: "* ]]; then
local gd="${line#gitdir: }"
[[ "$gd" != /* ]] && gd="${dir}/${gd}"
git_path="$gd"
fi
fi
local head_file="${git_path}/HEAD"
[[ -r "$head_file" ]] || return 1
local head
read -r head < "$head_file" 2>/dev/null
if [[ "$head" == "ref: refs/heads/"* ]]; then
REPLY="${head#ref: refs/heads/}"
elif [[ -n "$head" ]]; then
REPLY="${head[1,7]}" # Detached commit SHA
fi
# Check merge / rebase / cherry-pick states
if [[ -f "${git_path}/MERGE_HEAD" ]]; then
REPLY="${REPLY}|MERGING"
elif [[ -d "${git_path}/rebase-merge" ]]; then
REPLY="${REPLY}|REBASE"
fi
}
Reading and parsing .git/HEAD in pure Zsh executes in 0.072 ms. Your branch name, detached state,
Comments
No comments yet. Start the discussion.