diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-25 05:52:41 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-25 05:52:41 -0300 |
| commit | 18043e845b39ac63dfe4416787e5cf6476ce8434 (patch) | |
| tree | 0b28a690a5c671046b30498188e1567c164ccc88 /core/Names.zig | |
| parent | c22b57e13fec318ff4df96fb07b096ea53e31122 (diff) | |
| download | pardes-18043e845b39ac63dfe4416787e5cf6476ce8434.tar.gz pardes-18043e845b39ac63dfe4416787e5cf6476ce8434.zip | |
syntax, body_layer, panes, normal, root, Names, Pick: `]`'s painted targets in one query pass, kept while nothing moves, and Find's first key sorting only the rows it shows (walk UI P3 item 4, POLISH 19, F's and D's reports): (a) body_layer.targetLine ran the grammar's textobject query three times a frame while `]` or `[` showed (syntax.objectNext once each for function, comment, test); NOW syntax.objectNext finds any number of objects in one pass of the query (`objects`, `out`; the motion's one-object call in normal.zig unchanged in effect) and body_layer.treeTargets asks it once for the three, keeping the lines on the pane (Pane.targets, derived: never digested or dumped) while the text's revision, the caret's byte, the lines shown and the way stay, so a frame that paints them again asks nothing; paintTargets' drawing is untouched; (b) Names.rescore sorted every hit at every call (std.sort.pdq over 50,000 paths for a first key); NOW the best `reach` hits (`max_rows`, 200: what a query pane lists; a picker's cursor gone past them sorts further, Names.reachTo from Pick.key, and C-q's keep sorts its 2,000) are kept as a heap of the best so far and sorted, the rest after them in no order (O(n log k)); (c) the picker's preview read is left on the loop: measured, a 2,000-line file's tick and frame is 2.36M instructions, 285-531 us, under a twentieth of a 60 Hz frame (a read from a slow mount would still block it: not moved, said in the report); MEASURED (pardes-perf ReleaseFast tui, the same tree before and after this change): `]` painted, tick + frame, longest 11,066,602 instr (802 us) -> 4,677,637 (368 us, the frame that asks; the cached ones about the at-rest frame's 0.89M); pick key + frame (50,000 paths, first key) 58,751,637 (4,383 us) -> 41,394,164 (2,416 us, the rest is the scorer's); pick Down + frame 1,358,803 -> 1,356,960; pick preview tick + frame 2,357,436 -> 2,365,116; TESTS: core/Names.zig "Names keeps its best `reach` hits first in a full sort's order, the rest after them, and sorts further as a cursor goes past them"; the painted targets' and the motions' own tests unchanged; root.zig names Pane.targets among the kept-out fields; core-test 1064/1234 (170 skipped), behaviour-test 280/280
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE
Diffstat (limited to 'core/Names.zig')
| -rw-r--r-- | core/Names.zig | 92 |
1 files changed, 82 insertions, 10 deletions
diff --git a/core/Names.zig b/core/Names.zig index 47165f52..d063bd17 100644 --- a/core/Names.zig +++ b/core/Names.zig @@ -29,6 +29,10 @@ query_buf: [max_query]u8 = undefined, query_len: u8 = 0, /// Bumped whenever `hits` change: the rows are made again only then. gen: u32 = 0, +/// How many of `hits` are in order, best first: as many as a list shows +/// (`max_rows`), or as far as its cursor went (`reachTo`); the matches after +/// them are counted, in no order. +reach: u32 = max_rows, /// The query is `query_buf[0..query_len]`: what lies past it is undefined /// (0xAA in Debug, not in a release build), so no digest reads it. @@ -81,6 +85,7 @@ pub fn reset(n: *Names) void { n.pending.clearRetainingCapacity(); n.pending_at = 0; n.scored = 0; + n.reach = max_rows; n.gen +%= 1; } @@ -124,6 +129,7 @@ pub fn typed(n: *Names, gpa: std.mem.Allocator, text: []const u8) void { const grew = n.query_len > 0 and std.mem.startsWith(u8, q, n.queryText()) and std.mem.indexOfScalar(u8, q[n.query_len..], ' ') == null; n.query_len = @intCast(q.len); @memcpy(n.query_buf[0..q.len], q); + n.reach = max_rows; if (grew) { const pending = n.pending.items[n.pending_at..]; std.mem.copyForwards(u32, n.pending.items[0..pending.len], pending); @@ -165,19 +171,65 @@ pub fn rescore(n: *Names, gpa: std.mem.Allocator) void { if (!empty and left == 0) break; } // An empty query keeps the source's order (the walk's, recency): no - // row moves as the walk goes on, it only grows at the foot. - if (!empty) std.sort.pdq(Hit, n.hits.items, n.cands.items, struct { - fn lt(cands: []const Cand, a: Hit, b: Hit) bool { - if (a.score != b.score) return a.score > b.score; - const la = cands[a.cand].key_len; - const lb = cands[b.cand].key_len; - if (la != lb) return la < lb; - return a.cand < b.cand; - } - }.lt); + // row moves as the walk goes on, it only grows at the foot. Else the + // best `reach` first: a sort of every hit was most of a first key's + // 8 ms over 50,000 paths. + if (!empty) bestFirst(n.hits.items, n.cands.items, n.reach); if (n.hits.items.len != had or n.scored != was_scored or count > 0) n.gen +%= 1; } +/// Hits sorted as far as row `rows`, for a cursor gone past the ones that +/// were (an empty query's are in the source's order already). +pub fn reachTo(n: *Names, rows: usize) void { + if (rows <= n.reach or std.mem.trim(u8, n.queryText(), " ").len == 0) return; + n.reach = @intCast(@min(rows, max_cands)); + bestFirst(n.hits.items, n.cands.items, n.reach); + n.gen +%= 1; +} + +/// Hit `a` goes before `b`: the higher score, then the shorter key, then +/// the candidate added first. +fn before(cands: []const Cand, a: Hit, b: Hit) bool { + if (a.score != b.score) return a.score > b.score; + const la = cands[a.cand].key_len; + const lb = cands[b.cand].key_len; + if (la != lb) return la < lb; + return a.cand < b.cand; +} + +/// `hits`' best `k` to the front, in order, the rest after them in no +/// order: the best so far kept as a heap whose root is the worst of them, +/// then sorted. O(n log k). +fn bestFirst(hits: []Hit, cands: []const Cand, k_in: usize) void { + const k = @min(k_in, hits.len); + if (k == 0) return; + const heap = hits[0..k]; + var i = k / 2; + while (i > 0) { + i -= 1; + sink(heap, cands, i); + } + for (hits[k..]) |*h| if (before(cands, h.*, heap[0])) { + std.mem.swap(Hit, h, &heap[0]); + sink(heap, cands, 0); + }; + std.sort.pdq(Hit, heap, cands, before); +} + +/// Heap entry `at` taken down past the worse of its children while it is +/// better than that child. +fn sink(heap: []Hit, cands: []const Cand, from: usize) void { + var at = from; + while (true) { + const l = 2 * at + 1; + if (l >= heap.len) return; + const c = if (l + 1 < heap.len and before(cands, heap[l], heap[l + 1])) l + 1 else l; + if (!before(cands, heap[at], heap[c])) return; + std.mem.swap(Hit, &heap[at], &heap[c]); + at = c; + } +} + /// The row of hit `i` and, in `marks`, the bytes of it the query matched. pub fn row(n: *const Names, i: usize, marks: []u16, nmarks: *usize) []const u8 { const c = n.cands.items[n.hits.items[i].cand]; @@ -363,3 +415,23 @@ test "Names ranks real paths as the pick prototype did" { try std.testing.expectEqualStrings("src/gui/Post.zig", topFor("gui/po")); try std.testing.expectEqualStrings("src/Output.zig", topFor("Out")); } + +test "Names keeps its best `reach` hits first in a full sort's order, the rest after them, and sorts further as a cursor goes past them" { + const gpa = std.testing.allocator; + var n: Names = .{}; + defer n.deinit(gpa); + var buf: [64]u8 = undefined; + for (0..3000) |i| { + const path = try std.fmt.bufPrint(&buf, "dir{d}/sub{d}/file_{d}.zig", .{ i % 7, i % 13, i }); + try n.add(gpa, path, 0, path.len, baseOf(path), 0); + } + n.typed(gpa, "f1"); + while (n.busy()) n.rescore(gpa); + const all = try gpa.dupe(Hit, n.hits.items); + defer gpa.free(all); + std.sort.pdq(Hit, all, n.cands.items, before); + try std.testing.expect(all.len > max_rows); + try std.testing.expectEqualSlices(Hit, all[0..max_rows], n.hits.items[0..max_rows]); + n.reachTo(max_rows + 500); + try std.testing.expectEqualSlices(Hit, all[0 .. max_rows + 500], n.hits.items[0 .. max_rows + 500]); +} |
