perf: batch ps lookup into a single call on save#566
Open
thewatts wants to merge 1 commit into
Open
perf: batch ps lookup into a single call on save#566thewatts wants to merge 1 commit into
thewatts wants to merge 1 commit into
Conversation
## Motivation Save performance degrades as session complexity grows — on a session with 86 panes and 78 windows, a save takes ~20 seconds. The bottleneck is that `pane_full_command` calls the save command strategy script once per pane. With the default `ps` strategy, each call runs `ps -ao "ppid,args"` to dump the entire system process table, then greps it for a single PPID. This is repeated once per pane. Additionally, `dump_panes` piped into a while loop, running the loop body in a subshell and preventing precomputed data from being shared across iterations. ## Changes **Batch the `ps` lookup into a single upfront call:** Added `_load_ps_table` which runs `ps -ao "ppid,args"` exactly once at the start of `save_all`, parsing the output into a bash associative array keyed by PPID. `pane_full_command` now does an O(1) in-memory hash lookup per pane instead of spawning a new process. Non-`ps` strategies (pgrep, linux_procfs, gdb) are unaffected and fall through to the original per-pane subprocess path. **Use process substitution in `dump_panes`:** Changed `dump_panes_raw | while ...` to `while ... done < <(dump_panes_raw)` so the loop runs in the current shell rather than a subshell, giving it access to the `_ps_table` associative array. ## Results Benchmarked on a session with 86 panes across 78 windows: | | Wall time | |---|---| | Before | ~20.4s (avg of 4 runs) | | After | ~0.70s (avg of 5 runs) | ~29× faster. The improvement scales with pane count — the more panes, the greater the benefit. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Save performance degrades as session complexity grows — on a session with 86 panes and 78 windows, a save takes ~20 seconds.
The bottleneck is that
pane_full_commandcalls the save command strategy script once per pane. With the defaultpsstrategy, each call runsps -ao "ppid,args"to dump the entire system process table, then greps it for a single PPID. This is repeated once per pane.Additionally,
dump_panespiped into a while loop, running the loop body in a subshell and preventing precomputed data from being shared across iterations.Changes
Batch the
pslookup into a single upfront call:Added
_load_ps_tablewhich runsps -ao "ppid,args"exactly once at the start ofsave_all, parsing the output into a bash associative array keyed by PPID.pane_full_commandnow does an O(1) in-memory hash lookup per pane instead of spawning a new process. Non-psstrategies (pgrep, linux_procfs, gdb) are unaffected and fall through to the original per-pane subprocess path.Use process substitution in
dump_panes:Changed
dump_panes_raw | while ...towhile ... done < <(dump_panes_raw)so the loop runs in the current shell rather than a subshell, giving it access to the_ps_tableassociative array.Results
Benchmarked on a session with 86 panes across 78 windows:
~29× faster. The improvement scales with pane count — the more panes, the greater the benefit.