From 8474b3a7c16cdac242f362144d100931f149ad6f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 23:36:49 -0300 Subject: Text.setRanges counts each range's rows on from the last one's, one pass over the text to the last range where a count from the top for each was a pass a range (walk UI, line E, for item 3's live s: "every key in a prompt draws its live effect in the same frame"): RUN (pardes-perf --search, ReleaseFast, a 1.08 MB file, `%s` then a pattern that finds 1024 ranges near the top): 442-451 million instructions, 25-27 ms a key and its frame; THE CAUSE: Text.zig setRanges put each range's two ends with modal.positionAt, whose row is lineAtOffset's count of every newline from the text's start (1024 ranges, two ends each, a pass each); THE FIX: the ranges are sorted and merged by then, so a Walked counter takes each end's row on from the last (positionAt's answer, from the top again for an end before the last); now 30-38 million, 1.6-2.3 ms, the no-match key (`qq`, a pass over the text) 50.8 million, 2.2-2.4 ms, `/` 1.5-2.1 million (9.7 million its first key); TEST FIRST: Text.zig "setRanges puts each range's ends where positionAt says, counting rows on from the last"; core-test 1019/1186 (167 skipped), shell-test 1233/1236, pane-test 182/184, behaviour-test 274/277, process-test A's six (the failures A's, as on item 1) Co-Authored-By: Claude Opus 5.5 --- core/Text.zig | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'core/Text.zig') diff --git a/core/Text.zig b/core/Text.zig index 37feb081..3cf65b74 100644 --- a/core/Text.zig +++ b/core/Text.zig @@ -315,11 +315,18 @@ pub fn setRanges(t: *Text, tab: u16, text: []const u8, in: []const modal.Selecti if (n > 1 and !t.reserveSels()) return; // no room: the primary alone t.sticky_col = st[pri]; var w: usize = 0; + // The ranges are in the text's order now: each end's row counted on + // from the last one's, one pass to the last range, where a count from + // the top for each was a pass a range (an `s` over a big text's 1024 + // ranges, 445 million instructions a key). + var walked: Walked = .{}; for (r[0..n], 0..) |x, idx| { if (idx == pri) continue; const c = rangeCells(text, x); - const cc = modal.positionAt(text, c.cur); - const ac = modal.positionAt(text, c.anc); + const first = walked.at(text, @min(c.cur, c.anc)); + const last = walked.at(text, @max(c.cur, c.anc)); + const cc = if (c.cur <= c.anc) first else last; + const ac = if (c.cur <= c.anc) last else first; t.sels[w] = .{ .row = @as(i32, @intCast(cc.row)), .col = @intCast(cc.col), @@ -332,6 +339,24 @@ pub fn setRanges(t: *Text, tab: u16, text: []const u8, in: []const modal.Selecti t.nsel = @intCast(w); } +/// Offsets' positions in a text, asked in order: the rows counted on from +/// the last asked (modal.positionAt's answer); one asked before it, from +/// the top. +const Walked = struct { + off: usize = 0, + row: usize = 0, + + fn at(w: *Walked, text: []const u8, off: usize) modal.Cursor { + const bounded = @min(off, text.len); + if (bounded < w.off) return modal.positionAt(text, bounded); + w.row += std.mem.count(u8, text[w.off..bounded], "\n"); + w.off = bounded; + const s = if (std.mem.lastIndexOfScalar(u8, text[0..bounded], '\n')) |nl| nl + 1 else 0; + const e = std.mem.indexOfScalarPos(u8, text, s, '\n') orelse text.len; + return .{ .row = w.row, .col = modal.graphemeStart(text[s..e], @min(bounded - s, e - s)) }; + } +}; + /// helix `scroll` without cursor sync (Ctrl-f/b, PgUp/PgDn, zj/zk): shift /// the view, then snap a fallen-out cursor to the near scrolloff edge, col 0 pub fn scrollViewMove(t: *Text, tab: u16, text: []const u8, range: modal.Selection, delta: i32) void { @@ -698,3 +723,29 @@ pub fn step(t: *Text, gpa: std.mem.Allocator, current: ?EditBuffer, back: bool) t.vsel = back_to.vsel; return back_to; } + +test "setRanges puts each range's ends where positionAt says, counting rows on from the last" { + const pardes = @import("root.zig"); + const p = try pardes.Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const text = "ab\ncd ef\n\ngh ij kl\nmn\n"; + const t = &(try p.setTestFile(text)).body; + const in = [_]modal.Selection{ + .{ .anchor = 3, .head = 5 }, // cd + .{ .anchor = 8, .head = 6 }, // ef, backwards + .{ .anchor = 11, .head = 13 }, // gh + .{ .anchor = 14, .head = 16 }, // ij + .{ .anchor = 20, .head = 22 }, // mn + }; + t.setRanges(4, text, &in, &.{}, 0, true); + try std.testing.expectEqual(@as(u16, 4), t.nsel); + for (in[1..], t.sels[0..4]) |r, s| { + const c = rangeCells(text, r); + const cc = modal.positionAt(text, c.cur); + const ac = modal.positionAt(text, c.anc); + try std.testing.expectEqual(@as(i32, @intCast(cc.row)), s.row); + try std.testing.expectEqual(@as(i32, @intCast(cc.col)), s.col); + try std.testing.expectEqual(@as(i32, @intCast(ac.row)), s.arow); + try std.testing.expectEqual(@as(i32, @intCast(ac.col)), s.acol); + } +} -- cgit v1.3