Triple Buffering
Three buffers, atomic swaps. The parser and GPU never block each other. Zero tearing.
Questions this answers
- What is triple buffering in a terminal emulator?
- How to prevent screen tearing in terminal applications
- Terminal rendering stalls when output is faster than display refresh
- Lock-free rendering pipeline for terminal emulators
How it works
Chau7 maintains three screen buffers: one being written by the parser, one being read by the Metal renderer, and one completed buffer waiting to be displayed. The parser writes new terminal state into the write buffer at PTY speed without any locks or synchronization. When a frame is complete, an atomic pointer swap makes the write buffer available to the renderer and gives the parser a fresh buffer to fill.
The Metal renderer picks up the latest completed buffer at display refresh time (60Hz or 120Hz depending on the display). Because there are three buffers in rotation, the renderer always has a consistent, complete frame to draw and the parser always has a free buffer to write into. Neither thread ever blocks waiting for the other.
The atomic swap uses a single compare-and-swap instruction with acquire-release memory ordering. There are no mutexes, no condition variables, and no possibility of priority inversion. The entire synchronization cost is one atomic instruction per frame, typically under 10 nanoseconds.
Why it matters
Without triple buffering, a terminal must either lock the screen buffer (causing the parser to stall while the GPU reads) or accept tearing artifacts. Chau7 maintains three buffers and rotates them with atomic swaps: the parser always has a buffer to write, the GPU always has a buffer to read, and they never contend. Smooth output at any speed.
Frequently asked questions
Does triple buffering increase memory usage significantly?
Each buffer holds the visible terminal grid, typically 80x24 to 240x80 cells. At roughly 32 bytes per cell, three buffers consume under 2 MB even for a large terminal. The memory cost is negligible compared to the performance benefit.
How does triple buffering interact with ProMotion 120Hz displays?
Triple buffering is refresh-rate independent. On a 120Hz ProMotion display, the renderer picks up the latest buffer 120 times per second. The parser continues writing at PTY speed regardless. The extra buffer ensures the renderer always has a complete frame even at twice the swap rate.