mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-08-05 15:18:40 +00:00
This adds opt-in continuation tracking to `terminal.Stream` that allows any caller to call `writeContinuation` in order to get the minimum bytes necessary from a grounded parser state to the identical state. This enables reliable stream restart across serialization states, which could be used for local restart, networked terminals, etc. For me, this is used for multiplexers. :) **LLM usage:** I wrote the continuation tracker myself, used a mix of 5.6+Fable to review it for me, applied their feedback directly. Only place with predominantly AI code are tests, which I reviewed. Commit and PR message written myself. ## Implementation The implementation of this was really carefully done to avoid any negative performance impact particularly when continuation tracking is _off_. The way this work is simple: 1. ESC is the only char that leaves the ground state and most ESC sequences are short. So if we're in a non-ground state, we do a backwards vectorized search to find the last `ESC` in the input slice. If one doesn't exist, we assume we found it previously and store the whole slice (rare, since ESC sequences are usually short like I said). 2. If we're in the ground state that means we only have a potential incomplete UTF-8 codepoint, so we find the lead UTF-8 byte. 3. When writing, we normalize the suffix to drop things like BEL commands that would've already been handled to avoid double-calling. ## Performance No real impact. Via `ghostty-bench +terminal-stream`. Corpus | Main | PR w/ Tracking Off | PR w/ Tracking On -- | -- | -- | -- Plain ASCII (256 MiB) | 175.4 ms | 175.8 ms | 175.5 ms UTF-8 (32 MiB) | 268.4 ms | 270.0 ms | 269.9 ms 5% invalid UTF-8 (32 MiB) | 316.4 ms | 317.8 ms | 320.2 ms CSI-heavy (32 MiB) | 145.0 ms | 146.3 ms | 145.9 ms OSC (32 MiB) | 1621.0 ms | 1627.5 ms | 1638.3 ms Kitty APC (128 MiB) | 95.5 ms | 96.9 ms | 96.3 ms Mixed traffic (32 MiB) | 172.4 ms | 172.0 ms | 172.3 ms Giant APC (128 MiB) | 38.3 ms | 38.3 ms | 40.8 ms