From 0073d05329335dbb0eb85f5c3044e42805d144e5 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 23 Oct 2026 17:10:58 -0300 Subject: Callstack lists every caller and pages a tree too big for one query; memory bounds and resident-size fixes Callstack walks while the server answers, counts on the message row past a second, puts test callers last at each level, and ends a tree past one query's memory (memory.limits.arena.lsp_query, a 256 MiB region taken and given back per query) in a row whose Callstack shows the next page. Grep reads whole files. New hard limits: undo text per file, image decode pixels, the LSP snapshot of other open files, one paste. Safe builds no longer fill worst-case buffers with 0xAA (memory.unfilled), big regions are kept off transparent huge pages, and malloc_trim runs after big results. Also: Look's git show pages into a +Diff, and dropped marks in File. Co-Authored-By: Claude Opus 5.5 --- src/File.zig | 141 +++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 109 insertions(+), 32 deletions(-) (limited to 'src/File.zig') diff --git a/src/File.zig b/src/File.zig index 59b04e3f..7e05ac9f 100644 --- a/src/File.zig +++ b/src/File.zig @@ -43,14 +43,14 @@ pub const Snapshot = struct { saved_len: ?usize = null, fn deinit(s: Snapshot, gpa: std.mem.Allocator) void { - gpa.free(s.content); + pardes.memory.freeUnfilled(gpa, s.content); } }; /// The pane's text, place and name now, for an undo or redo step. fn snapshotOf(gpa: std.mem.Allocator, pane: *const Pane, f: *const State) ?Snapshot { return .{ - .content = gpa.dupe(u8, f.content) catch return null, + .content = pardes.memory.dupeUnfilled(gpa, f.content) catch return null, .cur_row = pane.body.cur_row, .cur_col = pane.body.cur_col, .vsel = pane.body.vsel, @@ -105,6 +105,8 @@ pub const History = struct { undo_len: usize = 0, redo: [limits.undo_max]Snapshot = undefined, redo_len: usize = 0, + /// The text `undo` and `redo` hold, kept near limits.undo_bytes. + bytes: usize = 0, /// The names the file had, each with the rename generation it ended /// (Snapshot.gen), and the generation now. @@ -115,6 +117,7 @@ pub const History = struct { const history = try gpa.create(History); history.undo_len = 0; history.redo_len = 0; + history.bytes = 0; history.names = .empty; history.gen = 0; return history; @@ -165,6 +168,9 @@ pub const State = struct { /// edit drops them (setContentSpan); shifting them through the splice, /// as pane.zig's shiftOne moves dot, would keep them. pager_spans: []pardes.pager.Span = &.{}, + /// The rows and spans an edit dropped, with the text they were for: an + /// edit (an undo) back to that very text takes them again. + dropped_marks: ?DroppedMarks = null, /// Seconds of the last content change, for the filesystem's stat; zero until edited. mtime: u32 = 0, /// Non-null for a generated output buffer rather than an on-disk file. @@ -457,10 +463,6 @@ test "an invalid byte in a grapheme is one cell, and never reaches vaxis's width try std.testing.expectEqual(@as(usize, 4), fitEnd(text, 0, 10)); } -pub fn byteDisplayWidth(byte: u8) usize { - return if (byte == '\t') config.tab_now else 1; -} - pub fn displayWidth(text: []const u8) usize { var width: usize = 0; var at: usize = 0; @@ -787,18 +789,9 @@ pub fn lineIndex(gpa: std.mem.Allocator, f: *State) ![]const usize { pub const Span = struct { head: usize, tail: usize }; fn editedLineStarts(gpa: std.mem.Allocator, starts: []const usize, old: []const u8, new: []const u8, known: ?Span) ![]usize { - const n = @min(old.len, new.len); - var head: usize = 0; - var tail: usize = 0; - if (known) |span| { - head = span.head; - tail = span.tail; - } else { - while (head + 64 <= n and std.mem.eql(u8, old[head..][0..64], new[head..][0..64])) head += 64; - while (head < n and old[head] == new[head]) head += 1; - while (tail + 64 <= n - head and std.mem.eql(u8, old[old.len - tail - 64 ..][0..64], new[new.len - tail - 64 ..][0..64])) tail += 64; - while (tail < n - head and old[old.len - tail - 1] == new[new.len - tail - 1]) tail += 1; - } + const span = known orelse spanOf(old, new); + const head = span.head; + const tail = span.tail; // A start s follows the newline at s-1: kept while that newline is in // the common head, shifted while it is in the common tail. const kept = std.sort.upperBound(usize, starts, head, struct { @@ -1123,6 +1116,7 @@ pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void { if (file.highlights.len > 0) p.tree_sitter_gpa.free(file.highlights); p.gpa.free(file.pager_spans); locations.freeRows(p.gpa, file.location_rows); + if (file.dropped_marks) |m| m.deinit(p.gpa); if (file.context_declarations.len > 0) p.tree_sitter_gpa.free(file.context_declarations); if (file.node_tree) |tree| syntax.freeTree(tree); for (file.node_history.items) |step| p.gpa.free(step.ranges); @@ -1140,6 +1134,18 @@ fn reportEdit(p: *Pardes, f: *State, new: []const u8) void { pardes.ctlfs.events.noteReplace(p, id, false, f.content, new); } +pub const DroppedMarks = struct { + len: usize, + hash: u64, + rows: []locations.Row, + spans: []pardes.pager.Span, + + fn deinit(m: DroppedMarks, gpa: std.mem.Allocator) void { + locations.freeRows(gpa, m.rows); + gpa.free(m.spans); + } +}; + pub fn setContent(p: *Pardes, f: *State, new: []u8) void { setContentSpan(p, f, new, null); } @@ -1160,10 +1166,16 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { moved[i] = .{ .cursor = spliced(@min(at, f.content.len), f.content.len, new.len, span.?), .scroll = spliced(lineStart(p.gpa, tf, tf.scroll), f.content.len, new.len, span.?) }; } const old_starts = f.line_starts.ptr; - locations.freeRows(p.gpa, f.location_rows); - f.location_rows = &.{}; - p.gpa.free(f.pager_spans); - f.pager_spans = &.{}; + if (f.location_rows.len > 0 or f.pager_spans.len > 0) { + if (f.dropped_marks) |m| m.deinit(p.gpa); + f.dropped_marks = .{ .len = f.content.len, .hash = std.hash.Wyhash.hash(0, f.content), .rows = f.location_rows, .spans = f.pager_spans }; + f.location_rows = &.{}; + f.pager_spans = &.{}; + } else if (f.dropped_marks) |m| if (m.len == new.len and m.hash == std.hash.Wyhash.hash(0, new)) { + f.location_rows = m.rows; + f.pager_spans = m.spans; + f.dropped_marks = null; + }; for (p.panes) |slot| if (slot) |pane| { if (pane.file) |*file| if (file == f) { for (0..pane.sel.len) |button| { @@ -1182,7 +1194,9 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { f.saved_hash = std.hash.Wyhash.hash(0, f.content); f.saved_len = f.content.len; } - p.gpa.free(f.content); + // Raw: `free` overwrites a safe build's freed memory, a whole file's + // worth per keystroke. + if (f.content.len > 0) p.gpa.rawFree(f.content, .of(u8), @returnAddress()); f.content = new; f.revision +%= 1; if (f.saved_hash) |h| if ((f.saved_len orelse new.len) == new.len and std.hash.Wyhash.hash(0, new) == h) { @@ -1241,14 +1255,43 @@ fn spliced(at: usize, old_len: usize, new_len: usize, span: Span) usize { } /// What `old` and `new` agree on at either end. -fn spanOf(old: []const u8, new: []const u8) Span { +/// What `new` kept of `old` at either end, compared 32 bytes at a time +/// (a byte at a time was most of a `d` in a 7 MB file). +pub fn spanOf(old: []const u8, new: []const u8) Span { + const block = 32; const n = @min(old.len, new.len); - const head = std.mem.indexOfDiff(u8, old[0..n], new[0..n]) orelse n; + var head: usize = 0; + while (head + block <= n and std.mem.eql(u8, old[head..][0..block], new[head..][0..block])) head += block; + while (head < n and old[head] == new[head]) head += 1; var tail: usize = 0; + while (tail + block <= n - head and std.mem.eql(u8, old[old.len - tail - block ..][0..block], new[new.len - tail - block ..][0..block])) tail += block; while (tail < n - head and old[old.len - 1 - tail] == new[new.len - 1 - tail]) tail += 1; return .{ .head = head, .tail = tail }; } +test "spanOf by blocks finds the byte loop's ends" { + var prng: std.Random.DefaultPrng = .init(5); + const random = prng.random(); + var a: [160]u8 = undefined; + var b: [160]u8 = undefined; + for (0..4000) |_| { + const la = random.uintLessThan(usize, a.len); + const lb = random.uintLessThan(usize, b.len); + for (a[0..la]) |*c| c.* = "ab"[random.uintLessThan(usize, 2)]; + const keep = @min(la, lb); + @memcpy(b[0..keep], a[0..keep]); + for (b[keep..lb]) |*c| c.* = "ab"[random.uintLessThan(usize, 2)]; + if (lb > 0 and random.boolean()) b[random.uintLessThan(usize, lb)] ^= 1; + const old = a[0..la]; + const new = b[0..lb]; + const n = @min(old.len, new.len); + const head = std.mem.indexOfDiff(u8, old[0..n], new[0..n]) orelse n; + var tail: usize = 0; + while (tail < n - head and old[old.len - 1 - tail] == new[new.len - 1 - tail]) tail += 1; + try std.testing.expectEqual(Span{ .head = head, .tail = tail }, spanOf(old, new)); + } +} + fn positionIn(gpa: std.mem.Allocator, f: *State, at: usize) struct { row: usize, col: usize } { const index = lineIndex(gpa, f) catch { const c = modal.positionAt(f.content, at); @@ -1492,14 +1535,18 @@ pub fn restoreSnap(pane: *Pane, f: *State, snap: Snapshot) void { pane.ensureCursorVisible(); } -fn pushHistory(gpa: std.mem.Allocator, slots: []Snapshot, len: *usize, snap: Snapshot) void { - if (len.* == slots.len) { +/// Bounded in steps and in bytes: the oldest go first, though the one +/// pushed always stays. +fn pushHistory(gpa: std.mem.Allocator, history: *History, slots: []Snapshot, len: *usize, snap: Snapshot) void { + while (len.* > 0 and (len.* == slots.len or history.bytes + snap.content.len > limits.undo_bytes)) { + history.bytes -= slots[0].content.len; slots[0].deinit(gpa); - std.mem.copyForwards(Snapshot, slots[0 .. slots.len - 1], slots[1..]); + std.mem.copyForwards(Snapshot, slots[0 .. len.* - 1], slots[1..len.*]); len.* -= 1; } slots[len.*] = snap; len.* += 1; + history.bytes += snap.content.len; } pub fn pushUndo(p: *Pardes, pane: *Pane) void { @@ -1510,9 +1557,12 @@ pub fn pushUndo(p: *Pardes, pane: *Pane) void { if (std.mem.eql(u8, last.content, f.content) and last.gen == history.gen) return; } const snap = snapshotOf(p.gpa, pane, f) orelse return; - pushHistory(p.gpa, &history.undo, &history.undo_len, snap); - for (history.redo[0..history.redo_len]) |item| item.deinit(p.gpa); + for (history.redo[0..history.redo_len]) |item| { + history.bytes -= item.content.len; + item.deinit(p.gpa); + } history.redo_len = 0; + pushHistory(p.gpa, history, &history.undo, &history.undo_len, snap); } pub fn undo(p: *Pardes, pane: *Pane) void { @@ -1520,9 +1570,10 @@ pub fn undo(p: *Pardes, pane: *Pane) void { const history = f.history; if (history.undo_len == 0) return; const current = snapshotOf(p.gpa, pane, f) orelse return; - pushHistory(p.gpa, &history.redo, &history.redo_len, current); + pushHistory(p.gpa, history, &history.redo, &history.redo_len, current); history.undo_len -= 1; const previous = history.undo[history.undo_len]; + history.bytes -= previous.content.len; // the file's text now setContent(p, f, previous.content); restoreName(p, pane, f, previous); restoreSnap(pane, f, previous); @@ -1533,9 +1584,10 @@ pub fn redo(p: *Pardes, pane: *Pane) void { const history = f.history; if (history.redo_len == 0) return; const current = snapshotOf(p.gpa, pane, f) orelse return; - pushHistory(p.gpa, &history.undo, &history.undo_len, current); + pushHistory(p.gpa, history, &history.undo, &history.undo_len, current); history.redo_len -= 1; const next = history.redo[history.redo_len]; + history.bytes -= next.content.len; // the file's text now setContent(p, f, next.content); restoreName(p, pane, f, next); restoreSnap(pane, f, next); @@ -2266,3 +2318,28 @@ pub fn paintWordSelection( } } } + +test "undo keeps its steps within a byte bound, the newest always" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const pane = try p.setTestFile(""); + const f = &pane.file.?; + const step = limits.undo_bytes / 4 + 1; + const text = try gpa.alloc(u8, step); + defer gpa.free(text); + for (0..8) |i| { + @memset(text, 'a' + @as(u8, @intCast(i))); + pushUndo(p, pane); + setContent(p, f, try gpa.dupe(u8, text)); + } + pushUndo(p, pane); + const history = f.history; + try std.testing.expect(history.undo_len > 0 and history.undo_len < 8); + try std.testing.expect(history.bytes <= limits.undo_bytes + step); + var sum: usize = 0; + for (history.undo[0..history.undo_len]) |snap| sum += snap.content.len; + try std.testing.expectEqual(sum, history.bytes); + undo(p, pane); + try std.testing.expectEqualStrings(text[0..1], f.content[0..1]); +} -- cgit v1.3