Ghostty upstream drafts from #3 (for Jake to edit and file) #63
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Found in S9 (
spikes/s9-memory) and #3 (M9 step 1, branchm9a-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-rs8953a74), 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):Repro:
readelf -S -W prog | grep tbssshows.tbssof0x402c0(256 KiB + a bit)..tbssis0x1b0. Five idle threads cost 122 KB instead of 1,337 KB (S9 engine bench,threadsstate withBARE=1).Why: Zig std's
Thread.maybeAttachSignalStackkeeps its alternate signal stack inthreadlocal var signal_stack: [std.options.signal_stack_size]u8, and the default size is1 << 18.lib_vt.zig'sstd_optionsdoesn'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:
Check before filing: whether main still has the default (
grep -n signal_stack_size src/lib_vt.zigand 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):
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'sMemoryPool.initpreheats the page pool with 4 standard pages (PagePool.initCapacity, line 339).std/mem/Allocator.zig:301:allocBytesWithAlignmentdoes@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) and4442-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):
rawAlloc(noundefinedfill) from the page allocator, and rely on fresh mmap pages being zero in safe builds too, as release builds already do.destroyNodeExtalready zeroes buffers before they go back to the pool. The safety fill buys little for memory that is zeroed straight away.Draft text (feature request):
Check before filing: whether main changed
page_preheat,initPagesor the pool. A quick test withpage_preheat = 1in a ReleaseSafe build would show how much of the 1.5 MB is the preheat (we didn't run it).