From f8b3deb973fefeacf720d055b8e319425d7b846a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 20:15:17 -0300 Subject: normal: C and Alt-C walk the lines on from the row they last tried: the bytes soak's one hang (s0-2, seed 0 at 30 ms: the session's own C at row 41,775 of a flood, normal.multiSelAction) was the copy arm trying row after row while a line is too short for the column, each try two offsetAt (lineStartOffset from the text's start) and two positionAt (lineAtOffset from the text's start): quadratic in the rows skipped, so a C or Alt-C on a column no near line reaches froze the editor on a long file of short lines; FIRST the test (50,000 lines, only the first and the last reach column 3: C, then Alt-C back, each bounded by the instructions this thread runs, perf_event_open's instructions:u as pardes-perf's gate counts them): on the base C took 306,247,585,476 instructions; then the fix: LineWalk, a line's start carried from try to try, walked forward or back a line at a time, and its cell the column's offset or null where the line ends before it or a grapheme spans it (graphemeStart of the clamped column equal to the column, the old test); C now 194 million in Debug, about 3,900 a line; Alt-s's per-line walks from the start are bounded by max_selections passes as Text.setRanges's own conversion is, and left; tui core-test 865/1022 (157 skipped), shell-test 1062/1065, term-test 18/18, pane-test 183/184, kitty-test 45/45, behaviour-test 275/275 and --replay 275/275, process-test 0 of 144 failed, gui unit-test 1660/1664 (BaselineHarnessMismatch its one failure); the frame and perf gate vs tree2 PASS: 77/77 frames, perfab +0.106% (threshold 0.4%), perfops 121 counts within 0.1% (largest +0.0306%) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/normal.zig | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 6 deletions(-) (limited to 'core/normal.zig') diff --git a/core/normal.zig b/core/normal.zig index ae6c2d18..982ab656 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -789,8 +789,12 @@ fn multiSelAction(tab: u16, t: *Text, text: []const u8, role: modal.Normal.Role, for (rs[0..n], 0..) |r, ri| { const is_pri = ri == got.pri; // head-exclusive: back the leading end off onto its own cell - const hp = modal.positionAt(text, if (r.anchor < r.head) modal.prevGrapheme(text, r.head) else r.head); - const ap = modal.positionAt(text, if (r.anchor < r.head) r.anchor else modal.prevGrapheme(text, r.anchor)); + const h_off = if (r.anchor < r.head) modal.prevGrapheme(text, r.head) else r.head; + const a_off = if (r.anchor < r.head) r.anchor else modal.prevGrapheme(text, r.anchor); + const hp = modal.positionAt(text, h_off); + const ap = modal.positionAt(text, a_off); + var al: LineWalk = .of(text, a_off, ap.row); + var hl: LineWalk = .of(text, h_off, hp.row); const height = @max(hp.row, ap.row) - @min(hp.row, ap.row) + 1; if (m >= Text.max_selections) break; if (is_pri) pri = m; @@ -803,12 +807,12 @@ fn multiSelAction(tab: u16, t: *Text, text: []const u8, role: modal.Normal.Role, const arow = if (below) ap.row + d else ap.row -| d; const hrow = if (below) hp.row + d else hp.row -| d; if (arow >= nlines or hrow >= nlines) break; - const a2 = modal.offsetAt(text, .{ .row = arow, .col = ap.col }); - const h2 = modal.offsetAt(text, .{ .row = hrow, .col = hp.col }); // a line too short to reach the column is skipped, not clamped - if (modal.positionAt(text, a2).col == ap.col and modal.positionAt(text, h2).col == hp.col) { + const a2 = al.cell(text, arow, ap.col); + const h2 = hl.cell(text, hrow, hp.col); + if (a2 != null and h2 != null) { if (is_pri) pri = m; - out[m] = modal.moveSelectionCursor(text, .{ .anchor = a2, .head = a2 }, h2, true); + out[m] = modal.moveSelectionCursor(text, .{ .anchor = a2.?, .head = a2.? }, h2.?, true); m += 1; made += 1; } @@ -818,6 +822,28 @@ fn multiSelAction(tab: u16, t: *Text, text: []const u8, role: modal.Normal.Role, t.setRanges(tab, text, out[0..m], &.{}, pri, expl); } +/// A line's start, walked on from the row C or Alt-C last tried: found +/// from the text's start, each try was a pass over all before it, and a +/// column no near line reaches hung the editor on a long text. +const LineWalk = struct { + row: usize, + start: usize, + + fn of(text: []const u8, off: usize, row: usize) LineWalk { + return .{ .row = row, .start = if (std.mem.lastIndexOfScalar(u8, text[0..off], '\n')) |nl| nl + 1 else 0 }; + } + + /// Column `col` of `row`, or null where the line ends before it or a + /// grapheme there spans it. + fn cell(w: *LineWalk, text: []const u8, row: usize, col: usize) ?usize { + while (w.row < row) : (w.row += 1) w.start = std.mem.indexOfScalarPos(u8, text, w.start, '\n').? + 1; + while (w.row > row) : (w.row -= 1) w.start = if (std.mem.lastIndexOfScalar(u8, text[0 .. w.start - 1], '\n')) |nl| nl + 1 else 0; + const e = std.mem.indexOfScalarPos(u8, text, w.start, '\n') orelse text.len; + const g = modal.graphemeStart(text[w.start..e], @min(col, e - w.start)); + return if (g == col) w.start + g else null; + } +}; + fn maxLine(text: []const u8) usize { const nl = modal.cursorLineCount(text); return if (text.len == 0 or text[text.len - 1] == '\n') nl -| 2 else nl - 1; @@ -1547,6 +1573,62 @@ test "one cursor holds no room for other ranges; a second range makes it and one try std.testing.expectEqual(@as(usize, 0), pane.body.sels.len); } +test "C and Alt-C on a long text of short lines walk its lines once, not each try from its start" { + // Work, not time: the instructions this thread runs, as pardes-perf's + // gate counts them. Each try walking from the text's start made C here + // cost billions (the bytes soak's hang, at row 41,775 of a flood). + if (@import("builtin").os.tag != .linux) return error.SkipZigTest; + const linux = std.os.linux; + var attr: linux.perf_event_attr = .{ + .type = .HARDWARE, + .config = @intFromEnum(linux.PERF.COUNT.HW.INSTRUCTIONS), + .flags = .{ .exclude_kernel = true, .exclude_hv = true }, + }; + const opened = linux.perf_event_open(&attr, 0, -1, -1, linux.PERF.FLAG.FD_CLOEXEC); + if (linux.errno(opened) != .SUCCESS) return error.SkipZigTest; // no counter here (perf_event_paranoid 3) + const fd: linux.fd_t = @intCast(opened); + defer _ = linux.close(fd); + const Count = struct { + fn now(f: linux.fd_t) u64 { + var n: u64 = 0; + _ = linux.read(f, std.mem.asBytes(&n), @sizeOf(u64)); + return n; + } + }; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 12 }); + defer p.deinit(); + // only the first line and the last reach column 3 + const lines = 50_000; + const text = try gpa.alloc(u8, 2 * 5 + (lines - 2) * 2); + defer gpa.free(text); + @memcpy(text[0..5], "abcd\n"); + for (0..lines - 2) |k| @memcpy(text[5 + 2 * k ..][0..2], "x\n"); + @memcpy(text[text.len - 5 ..], "abcd\n"); + const pane = try p.setTestFile(text); + const body = &pane.body; + // C walked once costs 194 million here in Debug, about 4,000 a line; + // walked from the start at every try it cost 306 billion + const bound = 10_000 * lines; + for ("lll") |cp| _ = handleNormal(p.editor(), body, .{ .cp = cp }); + var rs: [Text.max_selections]modal.Selection = undefined; + for ([_]Key{ .{ .cp = 'C' }, .{ .cp = 'C', .alt = true } }) |key| { + const t0 = Count.now(fd); + _ = handleNormal(p.editor(), body, key); + const cost = Count.now(fd) - t0; + const got = body.ranges(text, 0, &rs); + try std.testing.expectEqual(2, got.n); + try std.testing.expectEqual(3, @min(rs[0].anchor, rs[0].head)); + try std.testing.expectEqual(text.len - 2, @min(rs[1].anchor, rs[1].head)); + std.testing.expect(cost < bound) catch |err| { + std.debug.print("{u} (alt {}): {d} instructions, the bound {d}\n", .{ key.cp, key.alt, cost, bound }); + return err; + }; + // the new cursor alone, for the twin to walk back from + _ = handleNormal(p.editor(), body, .{ .cp = ',' }); + } +} + test "cursors that land on one place are one: a key typed there is typed once, as helix merges them" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 12 }); -- cgit v1.3