summaryrefslogtreecommitdiff
path: root/docs/ghostty-page-size.md
blob: 6e65f36a1aac061401da20b1a840e063f8353ab1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# ghostty: a terminal's state depends on the OS page size

A report for upstream (https://github.com/ghostty-org/ghostty), against
the commit pardes pins (83fb95624b04, 1.4.0, `build.zig.zon`). pardes
patches its fetched copy at build time (build/patches.zig, `ghostty_page`
and `ghostty_mem`) until it is fixed there, if upstream wants it fixed.

## What happens

A page's memory is rounded up to the OS page (`src/terminal/page.zig`,
`Page.layout`):

```zig
const total_size = alignForward(usize, meta_end, std.heap.page_size_min);
```

`page_size_min` is 4 KiB on x86-64 and aarch64 Linux, FreeBSD and x86-64
macOS, and 16 KiB on aarch64 macOS (Zig's `std/heap.zig`). The rounding is
not only slack: `Capacity.adjust` fills the rounded size with rows
(`availableBytesForGrid` is `total_size` less the metadata), and a style
table that grows is sized to fill it too. A standard page is 401,408 bytes
on Linux and 409,600 on an Apple silicon Mac. The scrollback limit
(`max_scrollback_bytes`) counts these bytes and prunes a whole page at a
time, so the same bytes fed to two terminals of the same size keep
different history on the two machines, and the snapshot of either
records its own capacity hints.

pardes meets it because a client of a session runs its own emulator on
the pty's bytes: a Mac client of a Linux session must end with the
session's terminal, row for row.

## Measured

A driver fed eight streams through pardes's own wrapper
(`core/term/terminal.zig`), at 120x40, in reads of 4,096 and 65,536 bytes,
resized to 61x43 at a third and back at two thirds; then the hash of
pardes's per-unit digest (the active screen's text, the history's extent,
the view, the cursor) and of ghostty's full snapshot. Linux x86-64 (SIMD
on and off) and FreeBSD 15.1 amd64 gave the same lines; the Mac
(Darwin 25.6, arm64):

| stream | digest | snapshot | history rows, Linux / Mac |
|---|---|---|---|
| shell, 90 KB | equal | differs (a style capacity hint, 388 / 397, and its record's CRC) | 0 / 0 |
| TUI, 31 KB | equal | equal | 0 / 0 |
| source text, 3.1 MB | differs | differs | 25,323 / 25,264 |
| build logs, 1.6 MB | differs | differs | 15,531 / 15,495 |
| heavy colour and links, 10 MB | differs | differs | 14,201 / 13,024 |
| a colour a cell, a link a word, stacked marks, 9 MB | differs | differs | 4,296 / 3,716 |
| kitty images, raw RGBA, 8.4 MB | equal | equal | 0 / 0 |

The active screen's text, the cursor and the modes are the same on both;
what differs is how much history is kept and, on the last stream, which
links were dropped for lack of room in a page (19,002 `start_hyperlink
OutOfSpace` on Linux, 19,750 on the Mac). The smallest streams that show
it: the first 601,027 bytes of the source text (the first prune) for the
history, the first 11,690 bytes of the shell stream (its page's style
table grows) for the snapshot alone.

The same driver built for Linux with `std.options.page_size_min = 16 KiB`
gives the Mac's lines exactly, digest and snapshot, on every stream: the
page size is the whole cause.

## The fix

Round a page's size to 4 KiB on every target, whatever the OS page is:

```diff
--- a/src/terminal/page.zig
+++ b/src/terminal/page.zig
@@ Page.layout
-        const total_size = alignForward(usize, meta_end, std.heap.page_size_min);
+        const total_size = alignForward(usize, meta_end, 4 << 10);
@@ Page.init
-        assert(l.total_size % std.heap.page_size_min == 0);
+        assert(l.total_size % (4 << 10) == 0);
--- a/src/terminal/mem.zig
+++ b/src/terminal/mem.zig
@@ decommit, recommit
-    assert(memory.len % std.heap.page_size_min == 0);
+    assert(memory.len % (4 << 10) == 0);
```

Why the Mac is the one that changes: on Linux and FreeBSD, where
`page_size_min` is 4 KiB, the three lines are what they were, so their
terminals are byte for byte what they were (the same history, the same
cost); only an Apple silicon Mac's pages shrink to Linux's sizes.

Why a Mac can: the length of a page's memory need not be a whole number of
OS pages. A page's memory is `mmap`ed (`Page.init`, `AllocPosix`) or a
pool item allocated alone by the page allocator (`UntouchedPool`, ghostty's
tagged allocator on Darwin, which rounds the mapping up to whole pages and
unmaps it the same way); the pointer stays 16 KiB aligned, as its type
says; only the length is shorter than the mapping. `madvise` (`decommit`,
`recommit`: `FREE_REUSABLE`, `FREE_REUSE`) acts on whole pages, and the
last page of a mapping is that page's alone, the slack included. What
asked for whole OS pages were three assertions, which now ask for 4 KiB:
in a safe build they failed at once on a Mac; in a fast one they were
promises to the optimiser. Nothing else in ghostty's terminal, its page
pool, its compression or its snapshot loader reads the rounding.

With it, the Mac gives Linux's lines on every stream (and with the PNG fix
of docs/zstbi-fp-contract.md, the PNG stream too).

## What it costs

Nothing on Linux and FreeBSD. On an Apple silicon Mac a page's mapping
keeps up to 12 KiB it does not use (8 KiB for a standard page: 401,408
bytes in 409,600), never touched, so never resident; the scrollback's
16 MiB budget counts the 4 KiB-rounded size, so a Mac now keeps Linux's
history (above: 25,323 rows of source text, 14,201 of heavy colour, where
it kept 25,264 and 13,024).

Measured with the Edit: on Linux and FreeBSD the cross-check driver's
eighteen lines are the lines it gave before, page counts and page bytes
included; pardes-perf's per-operation counts are within 0.03% of the
base's (its terminal cells +0.007%); on the Mac a ReleaseSafe build of
the driver runs all eight streams, the full scrollbacks' pruning and
page reuse included, with no assertion, and gives Linux's lines.

## Upstream today

Not read beyond what is on this machine (no network). The pin, 83fb956
(1.4.0), has the rounding at `page.zig:1770`. The newest other copy here,
`~/05-genizah/ghostty` at its `main` (e8e7fea1, 2026-06-16, 1.3.2-dev), is
OLDER than the pin and has the same line (`page.zig:1753`). So moving the
pin is no fix as far as can be seen from here; whether upstream changed
it after 1.4.0 is not known.