From a481de9feea08de7b5b82c2471de70abd70d7462 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 19 Nov 2026 23:07:14 -0300 Subject: build: one options module a module set, `pardes_options`, and `build_info` beside it (the start design's step 2, pardes-build-options change 2): `core.module` makes the set's options from the build's `ShellConfig` and the set's `Core` (the platform, now a `Core` field in place of a module handed in; the grammar set; the terminal; pardes-isolate's `isolated`; the addons compiled in and the pdf one with them, `builtin_addons` and `builtin_pdf`; tracy; the workspace tag; embedded sources; the library's `lib`), one module shared by the set's five as `pardes_config` was, and `build_info` (version, commit, change, built, deps) made once in `assemble` and given to every set; the three one-value modules made in both values (`term_options`, `pardes_isolation`, `builtin_addons`), `shellOptions`, the six `Parts` fields that held them, `Parts.tracy` and `Parts.embed_sources` (the `ShellConfig` in `Parts` holds both) and the assembly's two options (`opts`, `gui_opts`) go; the library's `pardes-syntax` and the editor (the library's and hxlib's) get `pardes_options` with no terminal, the editor's with `lib` as before; the sources' imports follow, one identifier a line (abi/wire.zig and core/Output.zig import both, the hash's and the Build pane's lines reading the version, commit and dependencies from `build_info`); test programs see it as before, through the core module's import table (pane-test, behaviour-test); `pardes version`'s semantics line did not move on any product (38b5ae573b965550; d41689a1f97605b8 with -Dtree-sitter=minimal); build.zig and build/ 2,992 -> 2,960 lines (3,005 at the base); every shape configures and builds as on the base, pardes-isolate and -Dtree-sitter=disabled failing with the base's same errors (pardes-isolate reaching them through its own options module); tui core-test 848/991 (143 skipped), shell-test 1027/1030, pane-test 183/184, behaviour-test 275/275 (and -- --replay), term-test 15/15, kitty-test 45/45, lib-test (11/11 in the consumer), hxlib 538 cases 0 mismatches, process-test 2 of 144 failed (lspcomplete and lspcompletemoved, the base's), gui unit-test 1625/1629 (the perf check's BaselineHarnessMismatch, the base's); the test names of every suite equal the base's (ledger); the frame and perf gate (gpu/bo-s2 against tree1) PASS: 77/77 frames byte-identical in Debug, the alternated window corpus2x -0.104% (pictures identical in all 10 runs), the 97 per-operation counts within 0.1% (largest image-frame +0.0345%; against the coordinator's pardes-merge22 gate m22-g, 90 within 5 ppm, deep-sel 8-10 ppm, image-frame up to 85 ppm, its known noise); load 5-25 on 16 cores Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- docs/next-editor.md | 2 +- docs/next-terminal.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'docs') diff --git a/docs/next-editor.md b/docs/next-editor.md index 120b4c09..e7168256 100644 --- a/docs/next-editor.md +++ b/docs/next-editor.md @@ -7,7 +7,7 @@ Decided 2026-11-17 on the coordinator's leans (survey: `.scratch/sketch/reports/editor-lib.md`); built the same day. One source of truth: the library runs the functions pardes runs, compiled from the -same files with `pardes_config.lib` set; nothing is copied for it. +same files with `pardes_options.lib` set; nothing is copied for it. ## The calls, as built diff --git a/docs/next-terminal.md b/docs/next-terminal.md index 26996897..c77ccd33 100644 --- a/docs/next-terminal.md +++ b/docs/next-terminal.md @@ -184,9 +184,9 @@ What differs from the plan above, and why: - **The ops are a compile-time constant, not handed over at start.** `Term.ops` (core/Term.zig:18) is `&@import("term").ops` when the build's - `term_options.terminal` says the core has a terminal, null otherwise. + `pardes_options.terminal` says the core has a terminal, null otherwise. Every core module but core-test's is given `term`; core-test's - (`core_lean` in build.zig) is not, and its `term_options` say so. A + (`core_lean` in build.zig) is not, and its `pardes_options` say so. A run-time `Pardes.Options.terminal` would have put the table in every test's options and every host's start for no gain: no build ever has two terminals, and a null known at compile time drops the dead code. -- cgit v1.3 From dcf6d58f8aacefd9a66817ef4589f68ce2d42e8a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 19 Nov 2026 21:56:34 -0300 Subject: build: ghostty's terminal page sized in 4 KiB steps on every target (build/patches.zig `ghostty_page` and `ghostty_mem`, docs/ghostty-page-size.md), so the same pty bytes and resizes give the same emulator on Linux, FreeBSD and an Apple silicon Mac, as a client that replays its session's bytes needs, with Linux and FreeBSD unchanged: ghostty rounds a page to the OS page (src/terminal/page.zig:1770 at the pinned 1.4.0; 4 KiB here and on FreeBSD, 16 KiB on aarch64 macOS) and fills the rounding with rows and grown style room, so the 16 MiB scrollback kept other pages on the Mac: its digest differed from Linux's on every stream that filled the scrollback (source text, build logs, heavy colour, the torture stream: 59 to 1,177 history rows fewer) and its snapshot on the shell stream too (a style capacity hint, 388 against 397); now the layout rounds to 4 KiB and the three assertions that a page's length is a whole number of OS pages (page.zig:247, mem.zig:154 and :346) ask for 4 KiB, which on Linux and FreeBSD is what they were; a Mac maps whole 16 KiB pages under the shorter length (up to 12 KiB a page unused and never touched; 8 KiB for a standard page) and madvise and munmap act on its whole pages; the cross-check driver built from this tree (with the next change) gives on Linux and FreeBSD the lines it gave before the Edit, page counts and bytes included, and on the Mac the same lines, from a ReleaseFast and a ReleaseSafe build through all eight streams with no assertion (.scratch/sketch/reports/pardes-term-xcheck.md, "The reverse Edit"); in place of an earlier form of this change that rounded every target to 16 KiB and cost Linux +0.49% on pardes-perf's at-limit terminal cells and up to 13% of its history; the genizah's ghostty (main e8e7fea1, 2026-06-16) is older than the pin and rounds the same way, so no pin moves it; on both changes: tui term-test 15/15, kitty-test 45/45, core-test 848/991 (143 skipped), shell-test 1027/1030, image-harness and kitty-harness pass; gui unit-test 1625/1629 (4 skipped; the perf check's BaselineHarnessMismatch, the base's), image-harness and kitty-harness pass; the macOS library builds for Linux and aarch64-macos, the FreeBSD tui builds (load 11 to 37); the frame and perf gate (gpu/xcheck-c3, against tree1): 77/77 frames byte-identical, unit-test as the base, perfab corpus2x -0.076% (noise 0.091%), perfops 97 of 97 within 0.1% (terminal on all +0.0069%, the largest +0.0285%) (load 11 to 24) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- build/patches.zig | 32 +++++++++++- docs/ghostty-page-size.md | 128 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 docs/ghostty-page-size.md (limited to 'docs') diff --git a/build/patches.zig b/build/patches.zig index d5ac7372..f58fb926 100644 --- a/build/patches.zig +++ b/build/patches.zig @@ -245,11 +245,41 @@ const ghostty_kitty = [_]Edit{ }, }; +/// ghostty's terminal page (src/terminal/page.zig): a page's size is +/// rounded to 4 KiB on every target, not to the OS page (4 KiB on Linux +/// and FreeBSD, 16 KiB on an Apple aarch64 Mac). The same bytes must give +/// the same emulator on every machine a client runs on, and the rounding +/// decides how many rows a page holds and how much room its style and link +/// tables grow to, so how many history rows the 16 MiB scrollback keeps: +/// a Mac had fewer than Linux once it was full, which the digest saw +/// (docs/ghostty-page-size.md). On Linux and FreeBSD this changes nothing; +/// a Mac maps whole 16 KiB pages under a length that is not one, up to +/// 12 KiB a page left unused, and ghostty's assertions that a page's +/// length is a whole number of OS pages (page.zig, mem.zig) ask for 4 KiB. +const ghostty_page = [_]Edit{ + .{ + .old = " const total_size = alignForward(usize, meta_end, std.heap.page_size_min);\n", + .new = " const total_size = alignForward(usize, meta_end, 4 << 10); // pardes (build/patches.zig)\n", + }, + .{ + .old = " assert(l.total_size % std.heap.page_size_min == 0);\n", + .new = " assert(l.total_size % (4 << 10) == 0); // pardes (build/patches.zig)\n", + }, +}; +const ghostty_mem = [_]Edit{.{ + .old = " assert(memory.len % std.heap.page_size_min == 0);\n", + .new = " assert(memory.len % (4 << 10) == 0); // pardes (build/patches.zig): a page's size\n", + .count = 2, +}}; + pub fn ghosttyKitty(b: *std.Build, patch: *std.Build.Step.Compile, dep: *std.Build.Dependency, vt: *std.Build.Module) void { const files = b.addWriteFiles(); // The rest of src as it is; fonts are the app's, not the library's. - _ = files.addCopyDirectory(dep.path("src"), "", .{ .exclude_extensions = &.{ "graphics_image.zig", ".ttf", ".otf" } }); + // ("terminal/page.zig" leaves terminal/snapshot/page.zig in.) + _ = files.addCopyDirectory(dep.path("src"), "", .{ .exclude_extensions = &.{ "graphics_image.zig", "terminal/page.zig", "terminal/mem.zig", ".ttf", ".otf" } }); _ = files.addCopyFile(apply(b, patch, dep, "src/terminal/kitty/graphics_image.zig", &ghostty_kitty, "ghostty_kitty.zig"), "terminal/kitty/graphics_image.zig"); + _ = files.addCopyFile(apply(b, patch, dep, "src/terminal/page.zig", &ghostty_page, null), "terminal/page.zig"); + _ = files.addCopyFile(apply(b, patch, dep, "src/terminal/mem.zig", &ghostty_mem, null), "terminal/mem.zig"); vt.root_source_file = files.getDirectory().path(b, "lib_vt.zig"); } diff --git a/docs/ghostty-page-size.md b/docs/ghostty-page-size.md new file mode 100644 index 00000000..6e65f36a --- /dev/null +++ b/docs/ghostty-page-size.md @@ -0,0 +1,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. -- cgit v1.3 From 406f34120e3013c1900abfadb7a812dfd54ba89e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 19 Nov 2026 22:31:10 -0300 Subject: build: no fused multiply-add in zstbi's resize (build/patches.zig `zstbi_cut`: `#pragma STDC FP_CONTRACT OFF`, docs/zstbi-fp-contract.md), so a kitty PNG over 1280 pixels downscales to the same pixels on an Apple silicon Mac as on Linux and FreeBSD, as a client that replays its session's bytes needs: clang contracts `a * b + c` where the CPU has a fused multiply-add, aarch64 always does (the Mac binary had 98 fmadd/fmla in stbir__resize_arbitrary and stbir__resample_horizontal_downsample; the x86-64 ones have none), a fused one rounds once, and the two downscaled pictures of the cross-check's PNG stream came out other pixels on the Mac (the snapshot differed; images are not in the digest) while the one under the bound and raw RGBA did not; with the pragma the Mac's are Linux's; the proof, the cross-check driver built from this tree (and the change before) with no patch of its own (only its step added to a scratch copy): its 18 lines (eight streams by two read sizes, two at 7 bytes: digest and full snapshot after two resizes) the same on Linux x86-64, FreeBSD 15.1 amd64 and the Mac (Darwin 25.6 arm64) (.scratch/sketch/reports/pardes-term-xcheck.md, "Landed"); no zstbi or stb upstream is on this machine to compare; tui term-test 15/15, kitty-test 45/45, core-test 848/991 (143 skipped), shell-test 1027/1030, image-harness and kitty-harness pass; gui unit-test 1625/1629 (4 skipped; the perf check's BaselineHarnessMismatch, the base's), image-harness and kitty-harness pass; the macOS library builds for Linux and aarch64-macos, the FreeBSD tui builds (load 19 to 47); the frame and perf gate (gpu/xcheck-c3, against tree1, with the change before): 77/77 frames byte-identical, perfab corpus2x -0.076%, perfops 97 of 97 within 0.1% Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- build/patches.zig | 8 +++++- docs/zstbi-fp-contract.md | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 docs/zstbi-fp-contract.md (limited to 'docs') diff --git a/build/patches.zig b/build/patches.zig index f58fb926..3d956d26 100644 --- a/build/patches.zig +++ b/build/patches.zig @@ -175,8 +175,14 @@ pub fn vaxis(b: *std.Build, patch: *std.Build.Step.Compile, vaxis_dep: *std.Buil /// zstbi with stb_image and its writer cut out: of it only its resize is /// left, for a terminal PNG's downscale (build/core.zig, "One PNG /// decoder"). zstbi.deinit still resets the reader's and the writer's flip -/// flags, and Image.deinit frees the resize's output. +/// flags, and Image.deinit frees the resize's output. And no fused +/// multiply-add in the resize: clang contracts `a * b + c` where the CPU +/// has one (aarch64 always does), which rounds once, so a PNG downscaled +/// on an Apple silicon Mac came out pixels apart from Linux's, and the same +/// bytes must give the same terminal on every machine a client runs on +/// (docs/zstbi-fp-contract.md). const zstbi_cut = [_]Edit{ + .{ .old = "#include \n", .new = "#include \n#pragma STDC FP_CONTRACT OFF\n" }, .{ .old = "#define STB_IMAGE_IMPLEMENTATION\n#include \"stb_image.h\"\n", .new = "void stbi_set_flip_vertically_on_load(int flag) { (void)flag; }\nvoid stbi_image_free(void *p) { zstbiFreePtr(p); }\n" }, .{ .old = "#define STB_IMAGE_WRITE_IMPLEMENTATION\n#include \"stb_image_write.h\"\n", .new = "void stbi_flip_vertically_on_write(int flag) { (void)flag; }\n" }, }; diff --git a/docs/zstbi-fp-contract.md b/docs/zstbi-fp-contract.md new file mode 100644 index 00000000..95c067fd --- /dev/null +++ b/docs/zstbi-fp-contract.md @@ -0,0 +1,66 @@ +# stb_image_resize: a downscale's pixels depend on the CPU's fused multiply-add + +A report for upstream (https://github.com/zig-gamedev/zstbi, which vendors +nothings/stb's `stb_image_resize.h` v0.97), against the commit pardes pins +(6b9bea1f, `build.zig.zon`). pardes patches its fetched copy at build time +(build/patches.zig, `zstbi_cut`) until it is fixed there, if upstream wants +it fixed. + +## What happens + +pardes downscales a kitty graphics PNG over 1280 pixels with zstbi's +`stbir_resize_uint8` (core/term/TerminalImages.zig, `decodePng`). The +resize is plain C on floats, with no SIMD of its own, and clang compiles C +with `-ffp-contract=on`: where the target has a fused multiply-add, +`a * b + c` becomes one instruction that rounds once instead of twice. +aarch64 always has one; the x86-64 targets pardes builds for here do not +use one. The aarch64 macOS binary has 66 `fmadd`/`fmla` in +`stbir__resize_arbitrary` and 32 in `stbir__resample_horizontal_downsample`; +the x86-64 Linux and FreeBSD binaries have no `vfmadd` anywhere. So the same +PNG downscales to pixels a step apart on an Apple silicon Mac. + +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, and a join compares the snapshot (which holds each image's +pixels) of the two. + +## Measured + +Three PNGs sent by direct data (`f=100`): 1601x1203 (downscaled to +1280x961), 700x2001 (to 447x1280) and 333x257 (not downscaled). On Linux +x86-64 (SIMD on and off) and FreeBSD 15.1 amd64 the three images' pixels +were the same; on the Mac (Darwin 25.6, arm64) the two downscaled ones +differed and the third was the same, as were two 1024x768 images sent as +raw RGBA (`f=32`). pardes's per-unit digest does not cover images, so only +the snapshot differed. + +## The fix + +Contraction off for the whole translation unit, at the top of zstbi's C file: + +```diff +--- a/src/zstbi.c ++++ b/src/zstbi.c +@@ -1,3 +1,4 @@ + #include ++#pragma STDC FP_CONTRACT OFF +``` + +(`-ffp-contract=off` in zstbi's build flags is the same.) With it the Mac +binary has no fused multiply-add in `stbir_*`, and the Mac's pixels are +Linux's: the three images, and the snapshot (with the page fix of +docs/ghostty-page-size.md, every stream's). + +## What it costs + +A multiply and an add where there was one instruction, in a resize made +once per image sent (a 1601x1203 picture's), on aarch64 only: not +measured, as nothing else of pardes runs it. + +## Upstream today + +Not read beyond what is on this machine (no network): no copy of zstbi or +stb is in `~/05-genizah`. The three zstbi copies in `zig-pkg/` (the pin and +two others) all vendor `stb_image_resize.h` v0.97 with no pragma. stb's own +successor, `stb_image_resize2.h`, has SIMD paths of its own; moving to it +would be another question of the same kind, not a fix of this one. -- cgit v1.3