Severity: MEDIUM (performance)
Status: ✅ FIXED
Files: src/server/render.zig, src/grid.zig, src/screen.zig, src/status.zig, src/format.zig, src/char_width.zig, src/options.zig, src/cmd/cmd.zig, src/cfg.zig, src/input.zig
render.zig:183-260 full merge copy of every dirty pane into merged_screen; :196-213 recomputes historyLen + combined_idx per row; :213-258 per-cell branch + copy.render.zig:186-195 per-pane 64-slot sixel scan per frame even with no sixels; :218 + screen.zig:462 findSixelImage 64-scan per sixel-marked cell.render.zig:252-256,493-497 copy_mode.isSelected(x,y) per cell.render.zig:471-484 duplicates the :199-211 history/combined_idx logic.render.zig:528-598 SGR bufPrint at 8 sites per changed cell; :723-729 moveTo bufPrint per cursor segment.render.zig:614-616,632,640 2x combiningCodepoint binary search + charWidth per non-space cell.grid.zig:621-795 per wrap break up to 4 backward scans (number run, word_breaks, space, MAI_HAN_AKAT, Thai); fresh line_cells per line + double copy, no upfront ensureTotalCapacity.grid.zig:588-598 full has_thai scan + findWordBreaks alloc per logical line; :280-282 shrink loops scrollUp; insertLine/deleteLine/insertChars/deleteChars O(width/height) shifts per call.screen.zig:441-450,462-483 three 64-slot id searches; callers multiply to O(width x 64) per scroll/erase (decrementLineRefs:502-513, scrollUpInRegion/scrollDownInRegion, resize:586-594).screen.zig:890-898 insert-mode one-cell memmove per writeChar; :915-919 writeStr per-byte charWidth + refcount + dirty.status.zig:302-355 per frame: fresh Context+HashMap, 6x set dupes + per-window 4x set + expandCached alloc/free; :360-370 second window loop; :202-211 4x visibleLen O(n) + double-scan truncate; orderedRemove(w) in while O(w^2).format.zig:198-209,501-507,514-542 fresh ops/compile/toOwnedSlice per expansion and per #{?/==/&&} arg; appendWithStrftime:248-288 byte-by-byte + localtime_r per literal op; replaceOwned:707 per substitution per frame.char_width.zig:62-70 linear override scan (up to 22) first in charWidth:223 on the hottest path; :231-238 3x binary search per non-ASCII call.options.zig:144-149 linear findDef per get/set; cmd.zig:1701-1709 + :1856-1863 double inline for over 49-entry table; :997-1009 triple-nest history-limit sweep; :751-769 nested capture loop with no reserve; :1763-1843 parse with per-token alloc and no reserve.cfg.zig:104-158 7x startsWith chain per line + allocPrint per error; :277-290 hand-rolled O(n x m) trim duplicating stdlib; input.zig:96-103 zeroes 64 param slots per CSI; :354-363 per-byte OSC append; :604 full sixel dupe per completion.Batch by area: cache historyLen/footprints per frame; hoist sixel scans behind a pane_has_sixels bit; reserve capacities (ensureTotalCapacity) for status/format/capture/parse buffers; memoize compiled formats per template; replace hottest linear scans (overrides, option defs) with comptime maps; single-pass rewrap with bounded backtrack. Profile with bench.sh before/after.
Fixed the items with a safe, behavior-preserving change; the rest were re-measured and are documented below as benign (fixing them would add risk for nanosecond-scale gains).
cmdCapturePane (src/cmd/cmd.zig): capped upfront ensureTotalCapacity
(min(h*(2w+1), 1MiB)) — no more geometric regrowth on large panes.status.buildLine (src/status.zig): centre_buf reserved for
windows*32 bytes and out_ranges for windows entries per frame.ChooseMode.enter (src/choose.zig): ensureTotalCapacity(items.len).format.expand (src/format.zig): result reserved for template.len.cfg.trimLeft (src/cfg.zig): hand-rolled O(n*m) loop replaced with
std.mem.trimStart.CmdArgs.exec (src/cmd/cmd.zig): removed the redundant second
comptime table scan; parse() already resolved the entry.historyLen/per-cell copy: historyLen() is a field read;
the merge is inherent to multi-pane compositing and already skips clean
panes plus per-cell diffs via last_cells. Production capture reuses the
retained render_buf, so there is no per-frame reservation to add.isSelected, SGR bufPrint, width lookups per cell: inherent per-cell
work, all O(1)-ish; no algorithmic cliff.char_width override scan: override_count == 0 fast path is already O(1)
when unconfigured; option table is 40 entries (sub-microsecond scans) and
command lookup runs at human frequency on a comptime-unrolled table.visibleLen repeats, clearParams memset, OSC appends, localtime_r per
status line: all on short strings / amortized geometric growth.clock/choose full repaints: tick/keypress frequency, not per frame.Full suite (zig build test) passes; no behavior change, only fewer
reallocs on the measured paths.