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/fs.zig | 82 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 47 insertions(+), 35 deletions(-) (limited to 'src/fs.zig') diff --git a/src/fs.zig b/src/fs.zig index 061d4225..032ab9c0 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -1087,17 +1087,17 @@ pub const platform_has_fs = !pardes.isolated and switch (pardes.platform) { const find_max_hits = 512; /// What a Find or Grep walk passed over, said under its hits: directories -/// it might not open, files read only in part, and caps it stopped at. +/// it might not open, and caps it stopped at. pub const WalkNotes = struct { skipped: usize = 0, - /// Files longer than grep_max_bytes, searched in their first part only. - partial: usize = 0, hits_cut: bool = false, + /// A pane search's matching lines it stopped at, its rows filling + /// look.search_text_output_bytes; 0 if none. + rows_cut: usize = 0, /// The cap on entries walked it stopped at, 0 if none. walk_cut: usize = 0, pub const max_hits = find_max_hits; - pub const max_file_bytes = grep_max_bytes; pub const max_files = grep_max_files; pub const max_depth = find_max_depth; @@ -1105,7 +1105,7 @@ pub const WalkNotes = struct { pub fn write(n: WalkNotes, out: []u8) usize { var w = std.Io.Writer.fixed(out); if (n.hits_cut) w.print("cut at {d} hits\n", .{max_hits}) catch {}; - if (n.partial > 0) w.print("{d} file{s} read only in part (first {d} KiB)\n", .{ n.partial, if (n.partial == 1) "" else "s", max_file_bytes / 1024 }) catch {}; + if (n.rows_cut > 0) w.print("cut at {d} lines\n", .{n.rows_cut}) catch {}; if (n.walk_cut > 0) w.print("walk cut at {d} entries\n", .{n.walk_cut}) catch {}; if (n.skipped > 0) w.print("{d} director{s} skipped: permission denied\n", .{ n.skipped, if (n.skipped == 1) "y" else "ies" }) catch {}; return w.buffered().len; @@ -1124,7 +1124,8 @@ pub const WalkNotes = struct { }; const find_max_depth = 16; const find_max_steps = 100_000; -pub const search_max_output_bytes = pardes.MAX_PANES * find_max_hits * (4096 + 320); +/// One root's Find or Grep rows at most: its hits, each a path and a line. +pub const search_root_output_bytes = find_max_hits * (4096 + 320); const find_skip = [_][]const u8{ ".git", ".jj", "target", "node_modules", @@ -1194,9 +1195,10 @@ const grep_max_files = 20_000; const GrepResult = struct { bytes: usize, hits: usize }; -fn grepText(path: []const u8, text: []const u8, pat: []const u8, out: []u8, budget: usize) GrepResult { +/// `text` starts at line `first_line` + 1 of its file. +fn grepText(path: []const u8, text: []const u8, pat: []const u8, out: []u8, budget: usize, first_line: usize) GrepResult { var result: GrepResult = .{ .bytes = 0, .hits = 0 }; - var line: usize = 0; + var line: usize = first_line; var it = std.mem.splitScalar(u8, text, '\n'); while (it.next()) |raw| { line += 1; @@ -1220,7 +1222,7 @@ pub fn grep(arena: std.mem.Allocator, gpa: std.mem.Allocator, dir: []const u8, b if (!platform_has_fs) { for (sources) |s| { if (hits >= find_max_hits or written == out.len) break; - const result = grepText(s.path, s.contents, pat, out[written..], find_max_hits - hits); + const result = grepText(s.path, s.contents, pat, out[written..], find_max_hits - hits, 0); hits += result.hits; written += result.bytes; } @@ -1276,30 +1278,39 @@ pub fn grep(arena: std.mem.Allocator, gpa: std.mem.Allocator, dir: []const u8, b const path_z = std.fmt.bufPrintSentinel(&pathbuf, "{s}", .{path}, 0) catch return error.PathTooLong; const fd = libc.open(path_z, .{ .ACCMODE = .RDONLY, .CLOEXEC = true, .NONBLOCK = true }); if (fd < 0) continue; + defer _ = libc.close(fd); + // The whole file, a buffer at a time: each searched through its last + // whole line, the rest carried into the next. + const shown = lsp.rel(home, path); var len: usize = 0; - var readable = true; - while (len < buf.len) { - const n = libc.read(fd, buf[len..].ptr, buf.len - len); - if (n < 0) { - if (libc.errno(n) == .INTR) continue; - readable = false; - break; + var lines_before: usize = 0; + var first = true; + file: while (true) { + var eof = false; + while (len < buf.len) { + const n = libc.read(fd, buf[len..].ptr, buf.len - len); + if (n < 0) { + if (libc.errno(n) == .INTR) continue; + break :file; + } + if (n == 0) { + eof = true; + break; + } + len += @intCast(n); } - if (n == 0) break; - len += @intCast(n); + if (first and std.mem.indexOfScalar(u8, buf[0..@min(len, 1024)], 0) != null) break; + first = false; + // A line longer than the buffer is searched in buffer-sized parts. + const end = if (eof) len else if (std.mem.lastIndexOfScalar(u8, buf[0..len], '\n')) |nl| nl + 1 else len; + const result = grepText(shown, buf[0..end], pat, out[written..], find_max_hits - hits, lines_before); + hits += result.hits; + written += result.bytes; + if (eof or hits >= find_max_hits or written == out.len) break; + lines_before += std.mem.count(u8, buf[0..end], "\n"); + std.mem.copyForwards(u8, buf[0 .. len - end], buf[end..len]); + len -= end; } - // Read to its cap with more behind it: searched in part, and said. - var more: [1]u8 = undefined; - const longer = readable and len == buf.len and libc.read(fd, &more, 1) > 0; - _ = libc.close(fd); - if (!readable) continue; - const text = buf[0..len]; - if (std.mem.indexOfScalar(u8, text[0..@min(len, 1024)], 0) != null) continue; - if (longer) notes.partial += 1; - const shown = lsp.rel(home, path); - const result = grepText(shown, text, pat, out[written..], find_max_hits - hits); - hits += result.hits; - written += result.bytes; if (hits >= find_max_hits) notes.hits_cut = true; } return written; @@ -1366,14 +1377,14 @@ test "a directory the walk may not open is skipped and counted: Grep and Find ke try std.testing.expectEqual(@as(usize, 1), notes.skipped); } -test "Grep says when a cap hid what it might have found: a file read in part, the hits cut" { +test "Grep reads a long file to its end, and says when its hits were cut" { if (!platform_has_fs) return; const gpa = std.testing.allocator; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var base_buf: [std.fs.max_path_bytes]u8 = undefined; const dir = base_buf[0..try tmp.dir.realPath(std.testing.io, &base_buf)]; - // The match past the first 256 KiB of a long file. + // A match past the first buffer of a long file, on its own line number. const long = try gpa.alloc(u8, grep_max_bytes + 64); defer gpa.free(long); @memset(long, 'x'); @@ -1386,8 +1397,9 @@ test "Grep says when a cap hid what it might have found: a file read in part, th defer gpa.free(out); var notes: WalkNotes = .{}; var n = try grep(arena.allocator(), gpa, dir, dir, "needle", out, ¬es); - try std.testing.expectEqual(@as(usize, 0), n); - try std.testing.expectEqual(@as(usize, 1), notes.partial); + var want_buf: [64]u8 = undefined; + const want = try std.fmt.bufPrint(&want_buf, "long.txt:{d}:", .{std.mem.count(u8, long, "\n")}); + try std.testing.expect(std.mem.startsWith(u8, out[0..n], want)); // More hits than the cap. var many: std.ArrayList(u8) = .empty; defer many.deinit(gpa); @@ -1398,7 +1410,7 @@ test "Grep says when a cap hid what it might have found: a file read in part, th try std.testing.expect(notes.hits_cut); var said: [256]u8 = undefined; const lines = said[0..notes.write(&said)]; - try std.testing.expectEqualStrings("cut at 512 hits\n1 file read only in part (first 256 KiB)\n", lines); + try std.testing.expectEqualStrings("cut at 512 hits\n", lines); try std.testing.expect(WalkNotes.isNote("cut at 512 hits") and WalkNotes.isNote("2 directories skipped: permission denied") and !WalkNotes.isNote("src/2 directories.txt")); } -- cgit v1.3