Skip to content

renderer: release GPU resources for hidden surfaces (macOS) - #14017

Merged
mitchellh merged 1 commit into
mainfrom
gpu-resources
Aug 25, 2026
Merged

mitchellh merged 1 commit into
mainfrom
gpu-resources

Conversation

@mitchellh

@mitchellh mitchellh commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Ref #12034

This commit releases many GPU resources when a surface becomes invisible and rebuilds it on the next draw. I don't say "all" because there are still some things we can improve on (Kitty images).

We previously held onto all GPU resources for the lifetime of the surface regardless of its visibility state. This is 3x (for triple-buffering): screen render targets, uniform/cell/custom shader buffers, font textures, and more.

Measured on macOS (Metal):

Measurement (1 visible + 20 hidden tabs) Before After
Tracked GPU allocations (steady state) 384.6 MiB 18.3 MiB
MTLDevice.currentAllocatedSize 393.3 MiB 19.7 MiB
footprint IOSurface (dirty) 309 MB 15 MB
Swap chain rebuild on unhide (42 tab switches) n/a avg 0.43 ms, max 0.55 ms

As you can see, importantly, swap chain rebuild is fast: 0.43ms average. That means that the rebuild is imperceptible and happens well within a frame draw time.

This is macOS only, but most of the work was in the generic renderer. GTK only needs to call releaseGpuResources when it becomes invisible to get the same benefits. I didn't have my VM handy to test this yet so I didn't include it.

Ref #12034

This commit releases many GPU resources when a surface becomes invisible and
rebuilds it on the next draw. I don't say "all" because there are still
some things we can improve on (Kitty images).

We previously held onto all GPU resources for the lifetime of the surface
regardless of its visibility state. This is 3x (for triple-buffering): 
screen render targets, uniform/cell/custom shader buffers, font textures,
and more.

Measured on macOS (Metal):

| Measurement (1 visible + 20 hidden tabs)    | Before    | After   |
|---------------------------------------------|-----------|---------|
| Tracked GPU allocations (steady state)      | 384.6 MiB | 18.3 MiB |
| `MTLDevice.currentAllocatedSize`            | 393.3 MiB | 19.7 MiB |
| `footprint` IOSurface (dirty)               | 309 MB    | 15 MB   |
| Swap chain rebuild on unhide (42 switches)  | n/a       | avg 0.43 ms, max 0.55 ms |

As you can see, importantly, swap chain rebuild is fast: 0.43ms average.
That means that the rebuild is imperceptible and happens well within
a frame draw time.

This is macOS only, but most of the work was in the generic renderer.
GTK only needs to call `releaseGpuResources` when it becomes invisible
to get the same benefits. I didn't have my VM handy to test this yet so
I didn't include it.
@mitchellh mitchellh added this to the 1.4.0 milestone Aug 25, 2026
@mitchellh
mitchellh requested a review from a team as a code owner August 25, 2026 17:46
@ghostty-bot ghostty-bot Bot added the renderer label Aug 25, 2026
@mitchellh
mitchellh merged commit 683d8db into main Aug 25, 2026
205 checks passed
@mitchellh
mitchellh deleted the gpu-resources branch August 25, 2026 18:08
wesm pushed a commit to kenn-io/ghosthub that referenced this pull request Sep 1, 2026
- Reduce long-lived Ghosthub memory retention while pinned to libghostty 1.3.1.
- Release Metal and IOSurface resources while terminal surfaces are hidden, without detaching tmux or SSH clients ([Ghostty #14017](ghostty-org/ghostty#14017)).
- Track terminal page ownership explicitly and return free-listed page memory to the OS instead of retaining the process high-water footprint ([Ghostty #13241](ghostty-org/ghostty#13241), [Ghostty #13245](ghostty-org/ghostty#13245)).
- Build on the ten upstream lifetime fixes already merged in #220; record the exact upstream commits and bump the bootstrap schema so stale artifacts rebuild.
- Keep the v1.3.1 adaptation limited to hidden-surface semaphore and display-link APIs; the page-list source changes match upstream.
- Allow local debug builds to be inspected with macOS memory tools; packaged release entitlements remain unchanged.
- The patched libghostty passes its focused PageList tests, Ghosthub’s full Swift and Python suites, formatting checks, and the app build.

Co-authored-by: CL Kao <clkao@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant