szn

Bug 225 — isImageReferenced performs O(total_cells × num_slots) scanning — linear search per sixel placement

225. isImageReferenced performs O(total_cells × num_slots) scanning — linear search per sixel placement

File: src/screen.zig:307–340 Severity: LOW (performance) Status: ✅ FIXED — added sixel_refcounts: [64]usize to Screen. Every cell-write path (writeChar, eraseLine, eraseDisplay, insertLines, deleteLines, insertChars, deleteChars, scrollUp, scrollDown, clearScreen, resetHard) now decrements the appropriate refcount before overwriting cells. isImageReferenced reduced from O(cells) to O(1) array lookup.

isImageReferenced is called up to three times per sixel placement (slot-selection steps 1, 2, and 3), and each call scans every cell in the main grid, main history, alt grid, and alt history. For a grid of 80×24 with 2000 history lines, that is ~160,000 cell comparisons per call. With step 3 looping over 64 slots, the worst case is 64 × 160K = ~10 million cell comparisons per sixel image.

Fix: Maintain a reference count per sixel image slot. Increment when a sixel marker cell is written (addSixelImage and any copy/scroll that moves such cells), decrement when a sixel cell is overwritten/erased. Replace isImageReferenced with a simple refcount > 0 check.


NEW BUGS (2026-07-24 deep static code review audit)