Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

perf: batch ps lookup into a single call on save#566

Open
thewatts wants to merge 1 commit into
tmux-plugins:mastertmux-plugins/tmux-resurrect:masterfrom
thewatts:faster-savethewatts/tmux-resurrect:faster-saveCopy head branch name to clipboard
Open

perf: batch ps lookup into a single call on save#566
thewatts wants to merge 1 commit into
tmux-plugins:mastertmux-plugins/tmux-resurrect:masterfrom
thewatts:faster-savethewatts/tmux-resurrect:faster-saveCopy head branch name to clipboard

Conversation

@thewatts

Copy link
Copy Markdown

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.

## 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.