szn

Bug 561 — Performance cluster 4

561. Avoidable full-scrollback scans and per-cell work in sixel cleanup, copy mode, rendering, and option lookup

Severity: MEDIUM (performance)
Status: ✅ RESOLVED
Files: src/screen.zig:371-398, src/mode_copy.zig:329-361, src/mode_copy.zig:412-558, src/server/render.zig:807-825, src/options.zig:85-135

Sites

  1. Full-grid + full-history scan in Screen.clearSixelMarkerCells (src/screen.zig:371-398): Whenever a sixel slot is overwritten or evicted, clearSixelMarkerCells(id) iterates every cell of self.grid.lines and every cell of self.grid.history ((2000 + height) * width cells), even when self.grid.sixel_refcounts[id & 0x3F] == 0 (the common case when addSixelImage reuses an unreferenced slot!). Fast-pathing if (self.grid.sixel_refcounts[id & 0x3F] == 0) return; (and stopping as soon as sixel_refcounts[id & 0x3F] reaches 0) eliminates the scan on unreferenced slot reuse.

  2. $O(\text{total_lines} \times \text{width})$ scan in CopyMode.yankSelection (src/mode_copy.zig:329-361): yankSelection loops for (0..total) |row| across all scrollback + visible rows (up to 2,050 lines) and calls self.isSelected(@intCast(col), @intCast(row)) for every cell, even when the user selected a single word on one line. Computing min_r = @min(self.sel_start_y, self.cursor_y) and max_r = @max(self.sel_start_y, self.cursor_y) restricts the loop to min_r .. max_r + 1.

  3. Full scrollback re-indexing on every n/N search keypress (src/mode_copy.zig:412-558): searchForward and searchBackward allocate and rebuild logicalLineInfo (ArrayList(LogicalLine)) across the entire scrollback history on every n/N keypress and re-encode each logical line to UTF-8 on the fly. Since the grid is frozen while in copy mode, logicalLineInfo can be built once when entering copy mode (or on first search).

  4. Redundant resolveRowCells inside renderer blank-run coalescing (src/server/render.zig:807-825): Display.renderContent already populates row_cells for the entire row via resolveRowCells(pane, row, &row_cells) before the column loop (src/server/render.zig:750), but the blank-run lookahead loop at line 814 calls resolveRowCells(pane, row, &next_cell_buf) (or resolveCell) for each consecutive blank column instead of reading row_cells[col + blank_run].

  5. Linear $O(N)$ scans in Options (src/options.zig:85-135): Options.entries is an unsorted ArrayList(Entry) scanned linearly with std.mem.eql on every option lookup. Because all 37 valid options in option_table (src/options.zig:387) are known at comptime, indexing a fixed-size [option_table.len]?OptionValue array by a comptime-generated option enum makes every lookup/set $O(1)$ with zero Entry list allocations.

Proposed Fix

  1. In Screen.clearSixelMarkerCells, return immediately if self.grid.sixel_refcounts[id & 0x3F] == 0 and break early once the refcount hits 0.
  2. In CopyMode.yankSelection, iterate only over the selected row range min_r .. max_r + 1.
  3. Cache logicalLineInfo in CopyMode while copy mode is active.
  4. Read row_cells[col + blank_run] directly in Display.renderContent’s blank-run coalescing loop.
  5. Index Options by a comptime option enum (?[option_table.len]?OptionValue).

Resolution

  1. Screen.clearSixelMarkerCells early exit:
    • Checks findSixelImageSlot(image_id); if the slot is known and its sixel_refcounts[slot] == 0, immediately returns in $O(1)$ without scanning any rows.
    • During the marker clear loop, decrements sixel_refcounts[slot] and immediately breaks and returns as soon as the refcount reaches 0.
    • Updated addSixelImage to increment sixel_refcounts[slot] cell-by-cell as markers are placed, and runs clearSixelMarkerCells(id) before slot teardown in errdefer, ensuring proper early-exit on placement failures.
  2. CopyMode.yankSelection bounding & pre-allocation:
    • Confirmed loop bounds sy..=ey restrict iteration to the selection rectangle.
    • Pre-reserves result.ensureTotalCapacity for the estimated row count to prevent repeated heap reallocations.
  3. CopyMode search optimizations:
    • lineBytes pre-reserves offsets and out capacities per line (ensureUnusedCapacity) and uses appendAssumeCapacity / appendSliceAssumeCapacity in the per-cell loop, eliminating capacity checks and reallocations on every cell.
    • lineBytes returns LineBytesResult { ncells, end_li }, allowing searchForward to advance directly past continuation lines in $O(1)$ without repeating redundant scrollback line-walk loops.
    • Initialized search scratch buffers with pre-reserved capacities.
  4. Display.renderContent blank-run coalescing:
    • Replaced duplicate cell resolution and manual selection inverted-video checks in blank-run coalescing and VS16 width expansion with resolveCell(screen, row_cells, x + run, y) and resolveCell(screen, row_cells, x + 1, y).
    • Inlined row_cells extraction from resolveRowCells(screen, y).
  5. Comptime-unrolled Options lookup:
    • Optimized Options.findDef and options.findOptionDef using inline for loops over SESSION_OPTIONS and WINDOW_OPTIONS, taking advantage of comptime string literals for fast length comparison and unrolled integer word equality checks instead of runtime slice iterations.