From 8ac6db5662dd2db0f082e1651835f1db36e235f8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 11:57:35 -0300 Subject: Search converge 4/6: the disk engine, Grep and Find's listing on a worker (rg when there, else a sorted walk), generation-numbered batches, a debounce, a 2000-line cap and More, and the declarations parse off the loop --- src/Names.zig | 85 +++-------------------------------------------------------- 1 file changed, 3 insertions(+), 82 deletions(-) (limited to 'src/Names.zig') diff --git a/src/Names.zig b/src/Names.zig index 48447f56..0c8ba075 100644 --- a/src/Names.zig +++ b/src/Names.zig @@ -1,5 +1,5 @@ //! Names a query pane picks from by a fuzzy query: the files under a -//! directory (walked a slice a frame), the recent files, the open panes, +//! directory (listed by the disk engine, Stream.zig), the recent files, the open panes, //! a file's declarations. Each keystroke scores them again with a small //! fzf-style scorer (subsequence, Smith-Waterman with affine gaps, bonuses //! at the starts of words and path segments), a frame's budget at a time, @@ -12,7 +12,6 @@ const std = @import("std"); const pardes = @import("pardes.zig"); const panes = @import("panes.zig"); const config = @import("config.zig"); -const filesystem = @import("fs.zig"); const Pardes = pardes.Pardes; /// The candidates' rows, back to back, no newlines. @@ -31,9 +30,6 @@ pending: std.ArrayList(u32) = .empty, pending_at: usize = 0, query_buf: [max_query]u8 = undefined, query_len: u8 = 0, -/// The files walk, while it lasts (Find in a query pane until the disk -/// engine has it). -walk: ?Walk = null, /// Bumped whenever `hits` change: the rows are made again only then. gen: u32 = 0, @@ -59,30 +55,10 @@ pub const max_query = 64; /// A candidate is scored over its first this many bytes. const max_text = 512; const max_cands = 200_000; -/// Entries a walk reads a frame: ~1-3 ms warm. -const walk_slice = 2000; -const walk_max_depth = 16; /// A keystroke's scoring, and a frame's: what is left waits a frame. const score_budget_ns = 4 * std.time.ns_per_ms; -const Walk = struct { - root: std.Io.Dir, - walker: std.Io.Dir.SelectiveWalker, - done: bool = false, - skipped: u32 = 0, - - fn deinit(w: *Walk) void { - const io = std.Io.Threaded.global_single_threaded.io(); - // The walker closes a directory only as it leaves it: the ones it - // is still in (all but the root, which is ours) are closed here. - if (w.walker.stack.items.len > 1) for (w.walker.stack.items[1..]) |*item| item.iter.reader.dir.close(io); - w.walker.deinit(); - w.root.close(io); - } -}; - pub fn deinit(n: *Names, gpa: std.mem.Allocator) void { - if (n.walk) |*w| w.deinit(); n.bytes.deinit(gpa); n.lower.deinit(gpa); n.cands.deinit(gpa); @@ -93,8 +69,6 @@ pub fn deinit(n: *Names, gpa: std.mem.Allocator) void { /// No candidates, no walk: for a source filled again. pub fn reset(n: *Names) void { - if (n.walk) |*w| w.deinit(); - n.walk = null; n.bytes.clearRetainingCapacity(); n.lower.clearRetainingCapacity(); n.cands.clearRetainingCapacity(); @@ -136,62 +110,9 @@ pub fn baseOf(path: []const u8) u16 { return @intCast(@min(slash + 1, max_text)); } -/// The files under `dir`, walked from now on a slice a frame (`walkSlice`): -/// the first slice now, so a small tree is whole before the first frame. -pub fn startWalk(n: *Names, gpa: std.mem.Allocator, dir: []const u8) !void { - if (comptime !filesystem.platform_has_fs) return error.NoFilesystem; - const io = std.Io.Threaded.global_single_threaded.io(); - pardes.turn.yield(); - defer pardes.turn.back(); - var dir_handle = try std.Io.Dir.cwd().openDir(io, dir, .{ .iterate = true }); - errdefer dir_handle.close(io); - n.walk = .{ .root = dir_handle, .walker = try dir_handle.walkSelectively(gpa) }; - n.walkSlice(gpa); -} - -/// One frame's slice of a walk: up to `walk_slice` entries, files kept, -/// what a Find skips skipped and dot-directories too. -pub fn walkSlice(n: *Names, gpa: std.mem.Allocator) void { - const w = if (n.walk) |*w| w else return; - if (w.done) return; - const io = std.Io.Threaded.global_single_threaded.io(); - pardes.turn.yield(); - defer pardes.turn.back(); - var steps: usize = 0; - walk: while (steps < walk_slice) : (steps += 1) { - const e = (w.walker.next(io) catch { - w.skipped += 1; - continue; - }) orelse { - w.done = true; - break; - }; - if (e.kind != .directory) { - n.add(gpa, e.path, 0, e.path.len, baseOf(e.path), 0) catch { - w.done = true; - break; - }; - if (n.cands.items.len >= max_cands) { - w.done = true; - break; - } - continue; - } - if (e.depth() >= walk_max_depth or e.basename[0] == '.') continue; - for (filesystem.find_skip) |s| if (std.mem.eql(u8, e.basename, s)) continue :walk; - w.walker.enter(io, e) catch { - w.skipped += 1; - }; - } -} - -pub fn walking(n: *const Names) bool { - return if (n.walk) |w| !w.done else false; -} - -/// Still walking, or still scoring what it has: it wants the next frame. +/// Still scoring what it has: it wants the next frame. pub fn busy(n: *const Names) bool { - return n.walking() or n.scored < n.cands.items.len or n.pending_at < n.pending.items.len; + return n.scored < n.cands.items.len or n.pending_at < n.pending.items.len; } /// A new query: scored again, from what the last one matched when it only -- cgit v1.3