Grid Mode v2: 5-Tier Architecture
Grid Mode v2 combines native VTE parsing with xterm.js WebGL rendering to achieve significant CPU reduction compared to standard xterm.js.Performance Comparison
The 5-Tier Architecture
Tier 2: ACK-Based Flow Control
The key innovation that prevents crashes during high-output scenarios (LLM streaming,cat large_file).
The Problem
Without flow control:- LLM streaming outputs bulk data rapidly
- Each line triggers DOM reflow
- Browser crashes or freezes
The Solution
Consumer-driven backpressure:Key Properties
| Property | Description |
|---|---|
| Consumer-driven | Frontend rendering completion triggers next emit |
| No data loss | Grid processes ALL data immediately, only emit is controlled |
| 10s fallback | Prevents deadlock if ACK is lost |
| O(1) memory | Uses boolean flags only, no frame queue |
Tier 3: Cell Format Conversion
Grid Mode v2 converts between two different cell representations:Native Cell Structure
xterm.js BufferLine Format
Bit-Packing Format
Tier 4: Direct Buffer Injection
Instead of usingterm.write(), we inject directly into xterm’s internal buffer:
Why Not term.write()?
Epoch-Based Size Synchronization
Prevents race conditions during resize:Architecture Benefits
| Operation | Standard xterm | Grid Mode v2 |
|---|---|---|
| VTE Parsing | JavaScript (slow) | Native Rust (fast) |
| Cell Conversion | N/A | Direct format conversion |
| Buffer Update | JS internal | Direct injection |
| WebGL Render | GPU-accelerated | GPU-accelerated |
| Overall | High CPU usage | Lower CPU usage |
Key Files
| Component | File | Lines |
|---|---|---|
| AtomicParser | atomic_parser.rs | ~764 |
| Cell Converter | cell_converter.rs | ~416 |
| Buffer Injector | grid-buffer-injector.ts | ~726 |
| Terminal Parser | alacritty_renderer.rs | ~599 |