Ghostty upstream drafts from #3 (for Jake to edit and file) #63

Open
opened 2026-10-02 12:13:43 +00:00 by jhgaylor · 0 comments
Owner

Found in S9 (spikes/s9-memory) and #3 (M9 step 1, branch m9a-memory). Two libghostty-vt memory findings to send to Ghostty. Not filed by an agent on purpose (same as #54): Ghostty's AI_POLICY.md wants AI-assisted reports reviewed and edited by a human, with the tool disclosed. Bugs go in as an "Issue Triage" discussion and asks go under "Feature Requests, Ideas". So re-check each against current Ghostty main, trim, rewrite in your own words, disclose Claude Code, and file.

Pinned: Ghostty 22d13172cde98a0a4dda05d3d6a3fcb0dd8ed018 (through libghostty-rs 8953a74), Zig 0.16.0, -Demit-lib-vt=true, x86_64-linux-gnu (glibc 2.43), linked statically into a Rust program (illogicald). Line numbers below are at that commit.

Repro material:

  • spikes/s9-memory/engine/: one libghostty terminal per state, RSS divided by K (S9 README §3).
  • spikes/s9-memory/bench.py: the whole daemon (S9 README §1, §2).
  • readelf -S -W <binary> | grep tbss.

Draft 1: drop the 256 KiB threadlocal signal stack from libghostty-vt (Issue Triage, with a one-line patch)

What we carry: vendor/libghostty-vt-sys/patches/0001-no-signal-stack.patch (applied at build time since #3):

--- a/src/lib_vt.zig
+++ b/src/lib_vt.zig
@@ -376,6 +376,11 @@
 pub const std_options: std.Options = opts: {
     var options: std.Options = .{};
 
+    // No threadlocal signal stack: as a library, static TLS is allocated
+    // (and zeroed) for every thread of the host process, and libghostty-vt
+    // starts no threads that would use it.
+    options.signal_stack_size = null;
+
     if (builtin.target.cpu.arch.isWasm()) {

Repro:

  1. Build libghostty-vt (any optimize mode) and link it into a C or Rust program.
  2. readelf -S -W prog | grep tbss shows .tbss of 0x402c0 (256 KiB + a bit).
  3. Start N threads that do nothing. RSS grows by about 260 KB per thread.
  4. With the patch, .tbss is 0x1b0. Five idle threads cost 122 KB instead of 1,337 KB (S9 engine bench, threads state with BARE=1).

Why: Zig std's Thread.maybeAttachSignalStack keeps its alternate signal stack in threadlocal var signal_stack: [std.options.signal_stack_size]u8, and the default size is 1 << 18. lib_vt.zig's std_options doesn't override it. In an executable that's fine. But in a library linked into a host, the threadlocal lands in the host's static TLS block, and glibc allocates and zeroes that block for every thread the host starts: Rust threads, tokio workers, all of them. libghostty-vt starts no threads of its own, so nothing ever uses the stack.

In illogical that was 1.3 MB per terminal pane (five threads each), plus 8 MB for the tokio pool. The patch alone took 500 idle panes from 1,614 MB to 994 MB of daemon RSS (S9). With all of #3's step 1, 500 idle panes are 887 MB (1.78 MB each, from 3.30); the details are in PLAN.md's M9 section.

Draft text:

libghostty-vt gives every thread of the host process a 256 KiB TLS block

When libghostty-vt is linked into another program, Zig std's threadlocal signal stack (std.options.signal_stack_size, 256 KiB by default) ends up in the program's static TLS. glibc allocates and zeroes static TLS for every thread the program creates, so each thread costs about 260 KB of RSS even though libghostty-vt never starts a thread or installs a handler that uses that stack.

Repro at 22d13172: build with -Demit-lib-vt=true, link the static library into a small program, and readelf -S it: .tbss is 0x402c0. Spawn 100 idle threads and compare RSS against the same program without libghostty-vt.

Setting options.signal_stack_size = null in lib_vt.zig's std_options brings .tbss down to 0x1b0 and the per-thread cost to about 24 KB. We've carried that patch in an embedder (a terminal multiplexer with several threads per terminal) without problems. Is there a reason to keep the signal stack for the library build? If not, we'd be happy to send the one-line change.

(Found and drafted with Claude Code; reviewed and edited by me.)

Check before filing: whether main still has the default (grep -n signal_stack_size src/lib_vt.zig and Zig std), and whether libghostty (the full lib, not just -vt) has the same problem.


Draft 2: ReleaseSafe builds make every pooled page resident (Feature Request or Issue Triage)

Numbers (S9 engine bench, KiB of RSS per terminal):

state ReleaseSafe ReleaseFast
empty, 80x24 or 200x50 1,602 67
alt screen entered, empty 3,184 183
full 200x50 screen, 16 colours 3,586 200

illogical builds libghostty-vt ReleaseSafe on purpose: it parses untrusted program output, so we keep Zig's safety checks (Jake decided in #3 to stay on ReleaseSafe). That leaves about 1.5 MB per screen of memory nobody uses: after #3, an idle pane costs about 1.8 MB of daemon RSS, and about 1.5 MB of that is this.

Where (our reading, verify on main):

  • src/terminal/PageList.zig:38: page_preheat = 4. Each screen's MemoryPool.init preheats the page pool with 4 standard pages (PagePool.initCapacity, line 339).
  • Zig 0.16 std/mem/Allocator.zig:301: allocBytesWithAlignment does @memset(byte_ptr[0..byte_count], undefined). In safe modes that writes 0xAA over the whole allocation, so the preheated pages (fresh, zeroed mmap memory that would otherwise stay unbacked) are all touched.
  • PageList.zig:716-722 (initPages) and 4442-4453: "In runtime safety modes we have to memset because the Zig allocator interface will always memset to 0xAA for undefined", then @memset(page_buf, 0) on every page taken from the pool.

So in a safe build every page in the pool is resident from the start, while in ReleaseFast only the rows that get written are.

Fix ideas (pick one to propose, after checking main):

  1. Get pool pages with rawAlloc (no undefined fill) from the page allocator, and rely on fresh mmap pages being zero in safe builds too, as release builds already do. destroyNodeExt already zeroes buffers before they go back to the pool. The safety fill buys little for memory that is zeroed straight away.
  2. Make the preheat count an option (or 1 for the C API), so an embedder with hundreds of mostly idle terminals pays for one page, not four.
  3. Keep the fill but skip it when the buffer is known to come fresh from the OS.

Draft text (feature request):

Safe builds of libghostty-vt use ~1.5 MB per empty screen that release builds don't

We embed libghostty-vt built ReleaseSafe (it parses untrusted output, so we want the safety checks) with hundreds of terminals per process. An empty 80x24 terminal is about 1.6 MB of RSS in ReleaseSafe and 67 KB in ReleaseFast; entering the alternate screen doubles it.

As far as we can tell, it's the preheated page pool: in safe modes, Zig's Allocator fills new allocations with 0xAA, and PageList.initPages then memsets each pooled page to 0 (the comment there explains why), so every preheated page is resident from the start. In release builds the mmapped pages stay untouched until rows are written.

Would you take a change that avoids touching pool pages in safe builds, for example allocating them with rawAlloc and relying on zeroed OS pages as release builds already do? Or one that makes page_preheat configurable for library users? Happy to measure either on main.

(Found and drafted with Claude Code; reviewed and edited by me.)

Check before filing: whether main changed page_preheat, initPages or the pool. A quick test with page_preheat = 1 in a ReleaseSafe build would show how much of the 1.5 MB is the preheat (we didn't run it).

Found in S9 (`spikes/s9-memory`) and #3 (M9 step 1, branch `m9a-memory`). Two libghostty-vt memory findings to send to Ghostty. **Not filed by an agent on purpose** (same as #54): Ghostty's AI_POLICY.md wants AI-assisted reports reviewed *and edited* by a human, with the tool disclosed. Bugs go in as an "Issue Triage" discussion and asks go under "Feature Requests, Ideas". So re-check each against current Ghostty main, trim, rewrite in your own words, disclose Claude Code, and file. **Pinned:** Ghostty `22d13172cde98a0a4dda05d3d6a3fcb0dd8ed018` (through libghostty-rs `8953a74`), Zig 0.16.0, `-Demit-lib-vt=true`, x86_64-linux-gnu (glibc 2.43), linked statically into a Rust program (illogicald). Line numbers below are at that commit. **Repro material:** - `spikes/s9-memory/engine/`: one libghostty terminal per state, RSS divided by K (S9 README §3). - `spikes/s9-memory/bench.py`: the whole daemon (S9 README §1, §2). - `readelf -S -W <binary> | grep tbss`. --- ## Draft 1: drop the 256 KiB threadlocal signal stack from libghostty-vt (Issue Triage, with a one-line patch) **What we carry:** `vendor/libghostty-vt-sys/patches/0001-no-signal-stack.patch` (applied at build time since #3): ```diff --- a/src/lib_vt.zig +++ b/src/lib_vt.zig @@ -376,6 +376,11 @@ pub const std_options: std.Options = opts: { var options: std.Options = .{}; + // No threadlocal signal stack: as a library, static TLS is allocated + // (and zeroed) for every thread of the host process, and libghostty-vt + // starts no threads that would use it. + options.signal_stack_size = null; + if (builtin.target.cpu.arch.isWasm()) { ``` **Repro:** 1. Build libghostty-vt (any optimize mode) and link it into a C or Rust program. 2. `readelf -S -W prog | grep tbss` shows `.tbss` of `0x402c0` (256 KiB + a bit). 3. Start N threads that do nothing. RSS grows by about 260 KB per thread. 4. With the patch, `.tbss` is `0x1b0`. Five idle threads cost 122 KB instead of 1,337 KB (S9 engine bench, `threads` state with `BARE=1`). **Why:** Zig std's `Thread.maybeAttachSignalStack` keeps its alternate signal stack in `threadlocal var signal_stack: [std.options.signal_stack_size]u8`, and the default size is `1 << 18`. `lib_vt.zig`'s `std_options` doesn't override it. In an executable that's fine. But in a library linked into a host, the threadlocal lands in the host's static TLS block, and glibc allocates and zeroes that block for **every** thread the host starts: Rust threads, tokio workers, all of them. libghostty-vt starts no threads of its own, so nothing ever uses the stack. In illogical that was 1.3 MB per terminal pane (five threads each), plus 8 MB for the tokio pool. The patch alone took 500 idle panes from 1,614 MB to 994 MB of daemon RSS (S9). With all of #3's step 1, 500 idle panes are 887 MB (1.78 MB each, from 3.30); the details are in PLAN.md's M9 section. **Draft text:** > **libghostty-vt gives every thread of the host process a 256 KiB TLS block** > > When libghostty-vt is linked into another program, Zig std's threadlocal signal stack (`std.options.signal_stack_size`, 256 KiB by default) ends up in the program's static TLS. glibc allocates and zeroes static TLS for every thread the program creates, so each thread costs about 260 KB of RSS even though libghostty-vt never starts a thread or installs a handler that uses that stack. > > Repro at 22d13172: build with `-Demit-lib-vt=true`, link the static library into a small program, and `readelf -S` it: `.tbss` is 0x402c0. Spawn 100 idle threads and compare RSS against the same program without libghostty-vt. > > Setting `options.signal_stack_size = null` in `lib_vt.zig`'s `std_options` brings `.tbss` down to 0x1b0 and the per-thread cost to about 24 KB. We've carried that patch in an embedder (a terminal multiplexer with several threads per terminal) without problems. Is there a reason to keep the signal stack for the library build? If not, we'd be happy to send the one-line change. > > (Found and drafted with Claude Code; reviewed and edited by me.) **Check before filing:** whether main still has the default (`grep -n signal_stack_size src/lib_vt.zig` and Zig std), and whether libghostty (the full lib, not just `-vt`) has the same problem. --- ## Draft 2: ReleaseSafe builds make every pooled page resident (Feature Request or Issue Triage) **Numbers (S9 engine bench, KiB of RSS per terminal):** | state | ReleaseSafe | ReleaseFast | |---|---|---| | empty, 80x24 or 200x50 | 1,602 | 67 | | alt screen entered, empty | 3,184 | 183 | | full 200x50 screen, 16 colours | 3,586 | 200 | illogical builds libghostty-vt ReleaseSafe on purpose: it parses untrusted program output, so we keep Zig's safety checks (Jake decided in #3 to stay on ReleaseSafe). That leaves about 1.5 MB per screen of memory nobody uses: after #3, an idle pane costs about 1.8 MB of daemon RSS, and about 1.5 MB of that is this. **Where (our reading, verify on main):** - `src/terminal/PageList.zig:38`: `page_preheat = 4`. Each screen's `MemoryPool.init` preheats the page pool with 4 standard pages (`PagePool.initCapacity`, line 339). - Zig 0.16 `std/mem/Allocator.zig:301`: `allocBytesWithAlignment` does `@memset(byte_ptr[0..byte_count], undefined)`. In safe modes that writes 0xAA over the whole allocation, so the preheated pages (fresh, zeroed mmap memory that would otherwise stay unbacked) are all touched. - `PageList.zig:716-722` (`initPages`) and `4442-4453`: *"In runtime safety modes we have to memset because the Zig allocator interface will always memset to 0xAA for undefined"*, then `@memset(page_buf, 0)` on every page taken from the pool. So in a safe build every page in the pool is resident from the start, while in ReleaseFast only the rows that get written are. **Fix ideas (pick one to propose, after checking main):** 1. Get pool pages with `rawAlloc` (no `undefined` fill) from the page allocator, and rely on fresh mmap pages being zero in safe builds too, as release builds already do. `destroyNodeExt` already zeroes buffers before they go back to the pool. The safety fill buys little for memory that is zeroed straight away. 2. Make the preheat count an option (or 1 for the C API), so an embedder with hundreds of mostly idle terminals pays for one page, not four. 3. Keep the fill but skip it when the buffer is known to come fresh from the OS. **Draft text (feature request):** > **Safe builds of libghostty-vt use ~1.5 MB per empty screen that release builds don't** > > We embed libghostty-vt built ReleaseSafe (it parses untrusted output, so we want the safety checks) with hundreds of terminals per process. An empty 80x24 terminal is about 1.6 MB of RSS in ReleaseSafe and 67 KB in ReleaseFast; entering the alternate screen doubles it. > > As far as we can tell, it's the preheated page pool: in safe modes, Zig's `Allocator` fills new allocations with 0xAA, and `PageList.initPages` then memsets each pooled page to 0 (the comment there explains why), so every preheated page is resident from the start. In release builds the mmapped pages stay untouched until rows are written. > > Would you take a change that avoids touching pool pages in safe builds, for example allocating them with `rawAlloc` and relying on zeroed OS pages as release builds already do? Or one that makes `page_preheat` configurable for library users? Happy to measure either on main. > > (Found and drafted with Claude Code; reviewed and edited by me.) **Check before filing:** whether main changed `page_preheat`, `initPages` or the pool. A quick test with `page_preheat = 1` in a ReleaseSafe build would show how much of the 1.5 MB is the preheat (we didn't run it).
Commenting is not possible because the repository is archived.
No milestone
No project
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jhgaylor/illogical#63
No description provided.