rufio 1.5.1 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +37 -0
- data/README.md +8 -3
- data/README_ja.md +7 -3
- data/Rakefile +5 -0
- data/SECURITY.md +2 -2
- data/docs/guide.md +41 -8
- data/docs/guide_ja.md +41 -8
- data/docs/native_fuzzy_search_design_ja.md +684 -0
- data/info/help.md +13 -4
- data/info/keybindings.md +5 -3
- data/info/welcome.md +2 -2
- data/lib/rufio/config.rb +0 -2
- data/lib/rufio/config_loader.rb +24 -4
- data/lib/rufio/fuzzy_candidate_block.rb +131 -0
- data/lib/rufio/fuzzy_candidate_cache.rb +258 -0
- data/lib/rufio/fuzzy_file_source.rb +180 -0
- data/lib/rufio/fuzzy_finder.rb +313 -0
- data/lib/rufio/fuzzy_preview_loader.rb +107 -0
- data/lib/rufio/fuzzy_search.rb +327 -0
- data/lib/rufio/fuzzy_search_session.rb +426 -0
- data/lib/rufio/global_search.rb +292 -51
- data/lib/rufio/health_checker.rb +0 -22
- data/lib/rufio/help_menu.rb +5 -4
- data/lib/rufio/keybind_handler.rb +6 -5
- data/lib/rufio/native/aarch64-linux-gnu/librufio_zig.so +0 -0
- data/lib/rufio/native/aarch64-linux-musl/librufio_zig.so +0 -0
- data/lib/rufio/native/aarch64-macos/librufio_zig.dylib +0 -0
- data/lib/rufio/native/aarch64-windows/rufio_zig.dll +0 -0
- data/lib/rufio/native/x86_64-linux-gnu/librufio_zig.so +0 -0
- data/lib/rufio/native/x86_64-linux-musl/librufio_zig.so +0 -0
- data/lib/rufio/native/x86_64-macos/librufio_zig.dylib +0 -0
- data/lib/rufio/native/x86_64-windows/rufio_zig.dll +0 -0
- data/lib/rufio/native_content_search.rb +43 -7
- data/lib/rufio/native_scanner_zig.rb +17 -0
- data/lib/rufio/rga_result_source.rb +89 -0
- data/lib/rufio/screen.rb +5 -1
- data/lib/rufio/search_controller.rb +56 -54
- data/lib/rufio/terminal_ui.rb +70 -8
- data/lib/rufio/ui_renderer.rb +1 -1
- data/lib/rufio/version.rb +1 -1
- data/lib/rufio.rb +6 -0
- data/lib_zig/rufio_native/Makefile +16 -2
- data/lib_zig/rufio_native/build.zig +8 -0
- data/lib_zig/rufio_native/src/fuzzy.zig +520 -0
- data/lib_zig/rufio_native/src/fuzzy_test.zig +212 -0
- data/lib_zig/rufio_native/src/main.zig +27 -0
- data/lib_zig/rufio_native/src/search.zig +221 -44
- metadata +13 -2
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
//! Persistent UTF-8 subsequence index, bounded top-K and a Ruby-independent C ABI.
|
|
2
|
+
//!
|
|
3
|
+
//! Candidates are stored once as concatenated UTF-8 with a start offset and a
|
|
4
|
+
//! codepoint count each (about 12 bytes per candidate beyond the text). Folded
|
|
5
|
+
//! characters and bonuses are decoded per query into worker buffers, and only
|
|
6
|
+
//! for candidates that pass the subsequence check.
|
|
7
|
+
//!
|
|
8
|
+
//! A query whose previous query is a subsequence of it only scores the previous
|
|
9
|
+
//! matches plus candidates added since; everything else scans all candidates.
|
|
10
|
+
//! Large scans are split into chunks that worker threads claim atomically. All
|
|
11
|
+
//! buffers are allocated before threads start, so workers never allocate.
|
|
12
|
+
const std = @import("std");
|
|
13
|
+
const Allocator = std.mem.Allocator;
|
|
14
|
+
const NEGATIVE = -(1 << 60);
|
|
15
|
+
const MAX_QUERY_BYTES = 1024;
|
|
16
|
+
const MAX_CANDIDATE_BYTES = 65536;
|
|
17
|
+
const MAX_RESULTS = 10000;
|
|
18
|
+
const CHUNK = 1024;
|
|
19
|
+
const MAX_THREADS = 16;
|
|
20
|
+
|
|
21
|
+
pub const Match = struct { id: usize, score: i64 };
|
|
22
|
+
|
|
23
|
+
fn better(a: Match, b: Match) bool {
|
|
24
|
+
return a.score > b.score or (a.score == b.score and a.id < b.id);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
fn fold(char: u21) u21 {
|
|
28
|
+
return if (char >= 'A' and char <= 'Z') char + 32 else char;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
fn separator(char: u21) bool {
|
|
32
|
+
return switch (char) {
|
|
33
|
+
'/', '\\', '_', '-', '.', ' ' => true,
|
|
34
|
+
else => false,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
fn nextChar(bytes: []const u8, pos: usize, len: *usize) u21 {
|
|
39
|
+
if (bytes[pos] < 0x80) {
|
|
40
|
+
len.* = 1;
|
|
41
|
+
return bytes[pos];
|
|
42
|
+
}
|
|
43
|
+
len.* = std.unicode.utf8ByteSequenceLength(bytes[pos]) catch unreachable;
|
|
44
|
+
return std.unicode.utf8Decode(bytes[pos .. pos + len.*]) catch unreachable;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// Folded characters, bonuses and (optionally) byte offsets of validated UTF-8.
|
|
48
|
+
fn decode(bytes: []const u8, chars: []u21, bonuses: []u8, offsets: ?[]u32) void {
|
|
49
|
+
var basename: usize = 0;
|
|
50
|
+
var previous: u21 = 0;
|
|
51
|
+
var i: usize = 0;
|
|
52
|
+
var pos: usize = 0;
|
|
53
|
+
while (pos < bytes.len) : (i += 1) {
|
|
54
|
+
var len: usize = 1;
|
|
55
|
+
const char = nextChar(bytes, pos, &len);
|
|
56
|
+
if (offsets) |list| list[i] = @intCast(pos);
|
|
57
|
+
chars[i] = fold(char);
|
|
58
|
+
const boundary = i == 0 or separator(previous) or
|
|
59
|
+
(previous >= 'a' and previous <= 'z' and char >= 'A' and char <= 'Z');
|
|
60
|
+
bonuses[i] = 16 + @as(u8, if (boundary) 8 else 0);
|
|
61
|
+
if (char == '/' or char == '\\') basename = i + 1;
|
|
62
|
+
previous = char;
|
|
63
|
+
pos += len;
|
|
64
|
+
}
|
|
65
|
+
for (bonuses[basename..i]) |*bonus| bonus.* += 8;
|
|
66
|
+
// The first character of the file name ranks file-name prefixes above matches inside words.
|
|
67
|
+
if (basename < i) bonuses[basename] += 8;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// One pass over the raw bytes: non-matches never pay for decoding or the DP.
|
|
71
|
+
fn contains(bytes: []const u8, needle: []const u21, ascii_needle: bool) bool {
|
|
72
|
+
if (needle.len == 0) return true;
|
|
73
|
+
var matched: usize = 0;
|
|
74
|
+
if (ascii_needle) {
|
|
75
|
+
// UTF-8 continuation and lead bytes are >= 0x80 and never equal ASCII.
|
|
76
|
+
for (bytes) |byte| {
|
|
77
|
+
if (fold(byte) == needle[matched]) {
|
|
78
|
+
matched += 1;
|
|
79
|
+
if (matched == needle.len) return true;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
var pos: usize = 0;
|
|
85
|
+
while (pos < bytes.len) {
|
|
86
|
+
var len: usize = 1;
|
|
87
|
+
if (fold(nextChar(bytes, pos, &len)) == needle[matched]) {
|
|
88
|
+
matched += 1;
|
|
89
|
+
if (matched == needle.len) return true;
|
|
90
|
+
}
|
|
91
|
+
pos += len;
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fn isSubsequence(small: []const u21, large: []const u21) bool {
|
|
97
|
+
var matched: usize = 0;
|
|
98
|
+
for (large) |char| {
|
|
99
|
+
if (matched < small.len and char == small[matched]) matched += 1;
|
|
100
|
+
}
|
|
101
|
+
return matched == small.len;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Worst retained match at the root: O(N log K), never sort N matches.
|
|
105
|
+
fn siftUp(items: []Match, start: usize) void {
|
|
106
|
+
var i = start;
|
|
107
|
+
while (i > 0) {
|
|
108
|
+
const parent = (i - 1) / 2;
|
|
109
|
+
if (!better(items[parent], items[i])) break;
|
|
110
|
+
std.mem.swap(Match, &items[parent], &items[i]);
|
|
111
|
+
i = parent;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
fn siftDown(items: []Match) void {
|
|
116
|
+
var i: usize = 0;
|
|
117
|
+
while (i * 2 + 1 < items.len) {
|
|
118
|
+
var child = i * 2 + 1;
|
|
119
|
+
if (child + 1 < items.len and better(items[child], items[child + 1])) child += 1;
|
|
120
|
+
if (!better(items[i], items[child])) break;
|
|
121
|
+
std.mem.swap(Match, &items[i], &items[child]);
|
|
122
|
+
i = child;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
fn pushTop(heap: *std.ArrayList(Match), limit: usize, item: Match) void {
|
|
127
|
+
if (limit == 0) return;
|
|
128
|
+
if (heap.items.len < limit) {
|
|
129
|
+
heap.appendAssumeCapacity(item);
|
|
130
|
+
siftUp(heap.items, heap.items.len - 1);
|
|
131
|
+
} else if (better(item, heap.items[0])) {
|
|
132
|
+
heap.items[0] = item;
|
|
133
|
+
siftDown(heap.items);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/// Scores one contiguous range of work items. Owns its buffers; never allocates.
|
|
138
|
+
const Worker = struct {
|
|
139
|
+
engine: *const Engine,
|
|
140
|
+
needle: []const u21,
|
|
141
|
+
ascii_needle: bool,
|
|
142
|
+
narrowed: []const u32,
|
|
143
|
+
range_start: usize,
|
|
144
|
+
work: usize,
|
|
145
|
+
next_chunk: *std.atomic.Value(usize),
|
|
146
|
+
ids: []u32,
|
|
147
|
+
chunk_counts: []u32,
|
|
148
|
+
rows: []i64,
|
|
149
|
+
chars: []u21,
|
|
150
|
+
bonuses: []u8,
|
|
151
|
+
heap: std.ArrayList(Match) = .empty,
|
|
152
|
+
limit: usize,
|
|
153
|
+
total: u64 = 0,
|
|
154
|
+
|
|
155
|
+
fn run(self: *Worker) void {
|
|
156
|
+
while (true) {
|
|
157
|
+
const chunk = self.next_chunk.fetchAdd(1, .monotonic);
|
|
158
|
+
const begin = chunk * CHUNK;
|
|
159
|
+
if (begin >= self.work) return;
|
|
160
|
+
const end = @min(begin + CHUNK, self.work);
|
|
161
|
+
var count: u32 = 0;
|
|
162
|
+
for (begin..end) |item| {
|
|
163
|
+
const id: usize = if (item < self.narrowed.len) self.narrowed[item] else self.range_start + item - self.narrowed.len;
|
|
164
|
+
const n = self.engine.char_counts.items[id];
|
|
165
|
+
if (self.needle.len > n) continue;
|
|
166
|
+
const bytes = self.engine.bytesOf(id);
|
|
167
|
+
if (!contains(bytes, self.needle, self.ascii_needle)) continue;
|
|
168
|
+
var value: i64 = 0;
|
|
169
|
+
if (self.needle.len > 0) {
|
|
170
|
+
decode(bytes, self.chars[0..n], self.bonuses[0..n], null);
|
|
171
|
+
value = score(self.chars[0..n], self.bonuses[0..n], self.needle, self.rows, null) orelse continue;
|
|
172
|
+
}
|
|
173
|
+
self.ids[begin + count] = @intCast(id);
|
|
174
|
+
count += 1;
|
|
175
|
+
self.total += 1;
|
|
176
|
+
pushTop(&self.heap, self.limit, .{ .id = id, .score = value });
|
|
177
|
+
}
|
|
178
|
+
self.chunk_counts[chunk] = count;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
pub const Engine = struct {
|
|
184
|
+
allocator: Allocator,
|
|
185
|
+
text: std.ArrayList(u8) = .empty,
|
|
186
|
+
starts: std.ArrayList(usize) = .empty,
|
|
187
|
+
char_counts: std.ArrayList(u32) = .empty,
|
|
188
|
+
matches: std.ArrayList(Match) = .empty,
|
|
189
|
+
output: std.ArrayList(u8) = .empty,
|
|
190
|
+
total: u64 = 0,
|
|
191
|
+
max_chars: usize = 0,
|
|
192
|
+
/// Candidates scored by the last query (instrumentation for narrowing).
|
|
193
|
+
scanned: usize = 0,
|
|
194
|
+
/// Worker threads; 0 picks the CPU count (at most MAX_THREADS).
|
|
195
|
+
threads: usize = 0,
|
|
196
|
+
/// Scans below this many candidates stay on the calling thread. 2048 was the fastest of
|
|
197
|
+
/// 1024-16384 on 50k-168k paths (4 cores): narrowed steps no longer fall back to one thread.
|
|
198
|
+
min_parallel: usize = 2048,
|
|
199
|
+
previous_query: std.ArrayList(u21) = .empty,
|
|
200
|
+
previous_ids: std.ArrayList(u32) = .empty,
|
|
201
|
+
previous_count: usize = 0,
|
|
202
|
+
has_previous: bool = false,
|
|
203
|
+
|
|
204
|
+
pub fn init(allocator: Allocator) Engine {
|
|
205
|
+
return .{ .allocator = allocator };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
pub fn deinit(self: *Engine) void {
|
|
209
|
+
self.text.deinit(self.allocator);
|
|
210
|
+
self.starts.deinit(self.allocator);
|
|
211
|
+
self.char_counts.deinit(self.allocator);
|
|
212
|
+
self.matches.deinit(self.allocator);
|
|
213
|
+
self.output.deinit(self.allocator);
|
|
214
|
+
self.previous_query.deinit(self.allocator);
|
|
215
|
+
self.previous_ids.deinit(self.allocator);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
pub fn count(self: *const Engine) usize {
|
|
219
|
+
return self.starts.items.len;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/// Bytes held for candidates and the narrowing state.
|
|
223
|
+
pub fn storageBytes(self: *const Engine) usize {
|
|
224
|
+
return self.text.capacity + self.starts.capacity * @sizeOf(usize) +
|
|
225
|
+
self.char_counts.capacity * @sizeOf(u32) + self.previous_ids.capacity * @sizeOf(u32) +
|
|
226
|
+
self.previous_query.capacity * @sizeOf(u21);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
fn bytesOf(self: *const Engine, id: usize) []const u8 {
|
|
230
|
+
const end = if (id + 1 < self.starts.items.len) self.starts.items[id + 1] else self.text.items.len;
|
|
231
|
+
return self.text.items[self.starts.items[id]..end];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/// Validate the full batch first and reserve space, so a batch is added whole or not at all.
|
|
235
|
+
pub fn add(self: *Engine, batch: []const u8) !void {
|
|
236
|
+
if (batch.len == 0) return;
|
|
237
|
+
if (batch[batch.len - 1] != 0) return error.InvalidInput;
|
|
238
|
+
var strings = std.mem.splitScalar(u8, batch[0 .. batch.len - 1], 0);
|
|
239
|
+
var number: usize = 0;
|
|
240
|
+
var bytes: usize = 0;
|
|
241
|
+
while (strings.next()) |text| {
|
|
242
|
+
if (text.len > MAX_CANDIDATE_BYTES or !std.unicode.utf8ValidateSlice(text)) return error.InvalidInput;
|
|
243
|
+
number += 1;
|
|
244
|
+
bytes += text.len;
|
|
245
|
+
}
|
|
246
|
+
if (self.starts.items.len + number > std.math.maxInt(u32)) return error.OutOfMemory;
|
|
247
|
+
try self.text.ensureUnusedCapacity(self.allocator, bytes);
|
|
248
|
+
try self.starts.ensureUnusedCapacity(self.allocator, number);
|
|
249
|
+
try self.char_counts.ensureUnusedCapacity(self.allocator, number);
|
|
250
|
+
strings.reset();
|
|
251
|
+
while (strings.next()) |text| {
|
|
252
|
+
const chars = std.unicode.utf8CountCodepoints(text) catch unreachable;
|
|
253
|
+
self.starts.appendAssumeCapacity(self.text.items.len);
|
|
254
|
+
self.text.appendSliceAssumeCapacity(text);
|
|
255
|
+
self.char_counts.appendAssumeCapacity(@intCast(chars));
|
|
256
|
+
self.max_chars = @max(self.max_chars, chars);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
fn threadCount(self: *const Engine, work: usize) usize {
|
|
261
|
+
if (work < self.min_parallel) return 1;
|
|
262
|
+
const wanted = if (self.threads == 0) (std.Thread.getCpuCount() catch 1) else self.threads;
|
|
263
|
+
const chunks = (work + CHUNK - 1) / CHUNK;
|
|
264
|
+
return @max(1, @min(@min(wanted, MAX_THREADS), chunks));
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/// Serialized by the Ruby session; no concurrent mutation on a handle.
|
|
268
|
+
pub fn query(self: *Engine, text: []const u8, limit: usize) !void {
|
|
269
|
+
return self.queryPositions(text, limit, limit);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/// Like query, but recovers match positions only for the best `position_limit` results;
|
|
273
|
+
/// the others report zero positions (callers compute them on demand when shown).
|
|
274
|
+
pub fn queryPositions(self: *Engine, text: []const u8, limit: usize, position_limit: usize) !void {
|
|
275
|
+
if (text.len > MAX_QUERY_BYTES or limit > MAX_RESULTS or
|
|
276
|
+
std.mem.indexOfScalar(u8, text, 0) != null or !std.unicode.utf8ValidateSlice(text)) return error.InvalidInput;
|
|
277
|
+
var query_chars: [MAX_QUERY_BYTES]u21 = undefined;
|
|
278
|
+
var iterator = (try std.unicode.Utf8View.init(text)).iterator();
|
|
279
|
+
var length: usize = 0;
|
|
280
|
+
var ascii_needle = true;
|
|
281
|
+
while (iterator.nextCodepoint()) |char| {
|
|
282
|
+
query_chars[length] = fold(char);
|
|
283
|
+
if (char >= 0x80) ascii_needle = false;
|
|
284
|
+
length += 1;
|
|
285
|
+
}
|
|
286
|
+
const needle = query_chars[0..length];
|
|
287
|
+
self.matches.clearRetainingCapacity();
|
|
288
|
+
self.output.clearRetainingCapacity();
|
|
289
|
+
self.total = 0;
|
|
290
|
+
|
|
291
|
+
// Narrow only when every match of the new query must match the previous one.
|
|
292
|
+
const narrow = self.has_previous and length > 0 and isSubsequence(self.previous_query.items, needle);
|
|
293
|
+
self.has_previous = false;
|
|
294
|
+
const narrowed: []const u32 = if (narrow) self.previous_ids.items else &.{};
|
|
295
|
+
const range_start = if (narrow) self.previous_count else 0;
|
|
296
|
+
const work = narrowed.len + (self.starts.items.len - range_start);
|
|
297
|
+
self.scanned = work;
|
|
298
|
+
|
|
299
|
+
var next_ids: std.ArrayList(u32) = .empty;
|
|
300
|
+
defer next_ids.deinit(self.allocator);
|
|
301
|
+
try next_ids.resize(self.allocator, work);
|
|
302
|
+
const chunk_counts = try self.allocator.alloc(u32, (work + CHUNK - 1) / CHUNK);
|
|
303
|
+
defer self.allocator.free(chunk_counts);
|
|
304
|
+
@memset(chunk_counts, 0);
|
|
305
|
+
|
|
306
|
+
const thread_count = self.threadCount(work);
|
|
307
|
+
var workers: [MAX_THREADS]Worker = undefined;
|
|
308
|
+
var prepared: usize = 0;
|
|
309
|
+
defer for (workers[0..prepared]) |*worker| {
|
|
310
|
+
self.allocator.free(worker.rows);
|
|
311
|
+
self.allocator.free(worker.chars);
|
|
312
|
+
self.allocator.free(worker.bonuses);
|
|
313
|
+
worker.heap.deinit(self.allocator);
|
|
314
|
+
};
|
|
315
|
+
var next_chunk = std.atomic.Value(usize).init(0);
|
|
316
|
+
while (prepared < thread_count) : (prepared += 1) {
|
|
317
|
+
const rows = try self.allocator.alloc(i64, self.max_chars * 2);
|
|
318
|
+
errdefer self.allocator.free(rows);
|
|
319
|
+
const chars = try self.allocator.alloc(u21, self.max_chars);
|
|
320
|
+
errdefer self.allocator.free(chars);
|
|
321
|
+
const bonuses = try self.allocator.alloc(u8, self.max_chars);
|
|
322
|
+
errdefer self.allocator.free(bonuses);
|
|
323
|
+
var heap: std.ArrayList(Match) = .empty;
|
|
324
|
+
try heap.ensureTotalCapacity(self.allocator, @min(limit, work));
|
|
325
|
+
workers[prepared] = .{
|
|
326
|
+
.engine = self,
|
|
327
|
+
.needle = needle,
|
|
328
|
+
.ascii_needle = ascii_needle,
|
|
329
|
+
.narrowed = narrowed,
|
|
330
|
+
.range_start = range_start,
|
|
331
|
+
.work = work,
|
|
332
|
+
.next_chunk = &next_chunk,
|
|
333
|
+
.ids = next_ids.items,
|
|
334
|
+
.chunk_counts = chunk_counts,
|
|
335
|
+
.rows = rows,
|
|
336
|
+
.chars = chars,
|
|
337
|
+
.bonuses = bonuses,
|
|
338
|
+
.heap = heap,
|
|
339
|
+
.limit = limit,
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
try self.matches.ensureTotalCapacity(self.allocator, @min(limit, work) * thread_count);
|
|
343
|
+
|
|
344
|
+
// Threads that fail to start leave their chunks to the others.
|
|
345
|
+
var spawned: [MAX_THREADS]?std.Thread = @splat(null);
|
|
346
|
+
for (1..thread_count) |index| {
|
|
347
|
+
spawned[index] = std.Thread.spawn(.{ .stack_size = 256 * 1024 }, Worker.run, .{&workers[index]}) catch null;
|
|
348
|
+
}
|
|
349
|
+
workers[0].run();
|
|
350
|
+
for (spawned[1..thread_count]) |thread| {
|
|
351
|
+
if (thread) |t| t.join();
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Compact the per-chunk match lists in chunk order: ids stay ascending.
|
|
355
|
+
var matched: usize = 0;
|
|
356
|
+
for (chunk_counts, 0..) |chunk_count, chunk| {
|
|
357
|
+
const begin = chunk * CHUNK;
|
|
358
|
+
std.mem.copyForwards(u32, next_ids.items[matched .. matched + chunk_count], next_ids.items[begin .. begin + chunk_count]);
|
|
359
|
+
matched += chunk_count;
|
|
360
|
+
}
|
|
361
|
+
next_ids.shrinkRetainingCapacity(matched);
|
|
362
|
+
for (workers[0..thread_count]) |*worker| {
|
|
363
|
+
self.total += worker.total;
|
|
364
|
+
self.matches.appendSliceAssumeCapacity(worker.heap.items);
|
|
365
|
+
}
|
|
366
|
+
std.mem.sort(Match, self.matches.items, {}, struct {
|
|
367
|
+
fn less(_: void, a: Match, b: Match) bool {
|
|
368
|
+
return better(a, b);
|
|
369
|
+
}
|
|
370
|
+
}.less);
|
|
371
|
+
self.matches.shrinkRetainingCapacity(@min(limit, self.matches.items.len));
|
|
372
|
+
|
|
373
|
+
try self.writeResults(needle, &workers[0], position_limit);
|
|
374
|
+
|
|
375
|
+
if (length > 0) {
|
|
376
|
+
self.previous_query.clearRetainingCapacity();
|
|
377
|
+
try self.previous_query.appendSlice(self.allocator, needle);
|
|
378
|
+
std.mem.swap(std.ArrayList(u32), &self.previous_ids, &next_ids);
|
|
379
|
+
self.previous_count = self.starts.items.len;
|
|
380
|
+
self.has_previous = true;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
fn writeResults(self: *Engine, needle: []const u21, worker: *Worker, position_limit: usize) !void {
|
|
385
|
+
try self.write(u64, self.total);
|
|
386
|
+
try self.write(u32, @intCast(self.matches.items.len));
|
|
387
|
+
if (self.matches.items.len == 0) return;
|
|
388
|
+
const offsets = try self.allocator.alloc(u32, self.max_chars);
|
|
389
|
+
defer self.allocator.free(offsets);
|
|
390
|
+
for (self.matches.items, 0..) |item, rank| {
|
|
391
|
+
const with_positions = rank < position_limit;
|
|
392
|
+
try self.write(u64, @intCast(item.id));
|
|
393
|
+
try self.write(i64, item.score);
|
|
394
|
+
try self.write(u32, if (with_positions) @intCast(needle.len) else 0);
|
|
395
|
+
if (needle.len == 0 or !with_positions) continue;
|
|
396
|
+
const n = self.char_counts.items[item.id];
|
|
397
|
+
const chars = worker.chars[0..n];
|
|
398
|
+
decode(self.bytesOf(item.id), chars, worker.bonuses[0..n], offsets[0..n]);
|
|
399
|
+
const trace = try self.allocator.alloc(u32, n * needle.len);
|
|
400
|
+
defer self.allocator.free(trace);
|
|
401
|
+
_ = score(chars, worker.bonuses[0..n], needle, worker.rows, trace);
|
|
402
|
+
const final = worker.rows[(needle.len % 2) * n ..][0..n];
|
|
403
|
+
var end: usize = 0;
|
|
404
|
+
for (final, 0..) |value, i| {
|
|
405
|
+
if (value > final[end]) end = i;
|
|
406
|
+
}
|
|
407
|
+
var positions: [MAX_QUERY_BYTES]u32 = undefined;
|
|
408
|
+
var row = needle.len;
|
|
409
|
+
while (row > 0) {
|
|
410
|
+
row -= 1;
|
|
411
|
+
positions[row] = offsets[end];
|
|
412
|
+
end = trace[row * n + end];
|
|
413
|
+
}
|
|
414
|
+
for (positions[0..needle.len]) |position| try self.write(u32, position);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
fn write(self: *Engine, comptime T: type, value: T) !void {
|
|
419
|
+
var buf: [@sizeOf(T)]u8 = undefined;
|
|
420
|
+
std.mem.writeInt(T, &buf, value, .little);
|
|
421
|
+
try self.output.appendSlice(self.allocator, &buf);
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
/// Callers have already checked that `needle` is a subsequence of `chars`.
|
|
426
|
+
fn score(chars: []const u21, bonuses: []const u8, needle: []const u21, rows: []i64, trace: ?[]u32) ?i64 {
|
|
427
|
+
if (needle.len == 0) return 0;
|
|
428
|
+
const n = chars.len;
|
|
429
|
+
if (needle.len > n) return null;
|
|
430
|
+
@memset(rows[0..n], NEGATIVE);
|
|
431
|
+
for (needle, 0..) |char, row| {
|
|
432
|
+
const previous = rows[(row % 2) * n ..][0..n];
|
|
433
|
+
const current = rows[((row + 1) % 2) * n ..][0..n];
|
|
434
|
+
@memset(current, NEGATIVE);
|
|
435
|
+
var prefix_score: i64 = NEGATIVE;
|
|
436
|
+
var prefix_index: usize = 0;
|
|
437
|
+
for (chars, 0..) |c, i| {
|
|
438
|
+
if (i >= 2 and previous[i - 2] != NEGATIVE and previous[i - 2] + @as(i64, @intCast(i - 2)) > prefix_score) {
|
|
439
|
+
prefix_score = previous[i - 2] + @as(i64, @intCast(i - 2));
|
|
440
|
+
prefix_index = i - 2;
|
|
441
|
+
}
|
|
442
|
+
if (c != char) continue;
|
|
443
|
+
if (row == 0) {
|
|
444
|
+
// No penalty for the distance from the start: long directory prefixes must not
|
|
445
|
+
// outweigh a match in the file name (the length penalty still prefers short paths).
|
|
446
|
+
current[i] = @as(i64, bonuses[i]);
|
|
447
|
+
} else {
|
|
448
|
+
var value: i64 = if (prefix_score == NEGATIVE) NEGATIVE else prefix_score - @as(i64, @intCast(i)) + 1;
|
|
449
|
+
var source = prefix_index;
|
|
450
|
+
if (i > 0 and previous[i - 1] != NEGATIVE and previous[i - 1] + 24 > value) {
|
|
451
|
+
value = previous[i - 1] + 24;
|
|
452
|
+
source = i - 1;
|
|
453
|
+
}
|
|
454
|
+
if (value == NEGATIVE) continue;
|
|
455
|
+
current[i] = value + bonuses[i];
|
|
456
|
+
if (trace) |parents| parents[row * n + i] = @intCast(source);
|
|
457
|
+
}
|
|
458
|
+
if (row == 0) {
|
|
459
|
+
if (trace) |parents| parents[i] = 0;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const final = rows[(needle.len % 2) * n ..][0..n];
|
|
464
|
+
var best: i64 = NEGATIVE;
|
|
465
|
+
for (final) |value| best = @max(best, value);
|
|
466
|
+
if (best == NEGATIVE) return null;
|
|
467
|
+
return best - @as(i64, @intCast(n)) + @as(i64, if (needle.len == n) 64 else 0);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const ffi_allocator = std.heap.smp_allocator;
|
|
471
|
+
|
|
472
|
+
pub export fn core_fuzzy_create() u64 {
|
|
473
|
+
const engine = ffi_allocator.create(Engine) catch return 0;
|
|
474
|
+
engine.* = Engine.init(ffi_allocator);
|
|
475
|
+
return @intFromPtr(engine);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
pub export fn core_fuzzy_add(handle: u64, data: [*]const u8, length: usize) i32 {
|
|
479
|
+
if (handle == 0) return -1;
|
|
480
|
+
const engine: *Engine = @ptrFromInt(handle);
|
|
481
|
+
engine.add(data[0..length]) catch |err| return if (err == error.InvalidInput) -1 else -2;
|
|
482
|
+
return 0;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
pub export fn core_fuzzy_query(handle: u64, data: [*]const u8, length: usize, limit: u32) i32 {
|
|
486
|
+
if (handle == 0) return -1;
|
|
487
|
+
const engine: *Engine = @ptrFromInt(handle);
|
|
488
|
+
engine.query(data[0..length], limit) catch |err| return if (err == error.InvalidInput) -1 else -2;
|
|
489
|
+
return 0;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/// core_fuzzy_query that recovers positions only for the best `position_limit` results.
|
|
493
|
+
pub export fn core_fuzzy_query_positions(handle: u64, data: [*]const u8, length: usize, limit: u32, position_limit: u32) i32 {
|
|
494
|
+
if (handle == 0) return -1;
|
|
495
|
+
const engine: *Engine = @ptrFromInt(handle);
|
|
496
|
+
engine.queryPositions(data[0..length], limit, position_limit) catch |err| return if (err == error.InvalidInput) -1 else -2;
|
|
497
|
+
return 0;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
pub export fn core_fuzzy_results(handle: u64, length: *usize) ?[*]const u8 {
|
|
501
|
+
length.* = 0;
|
|
502
|
+
if (handle == 0) return null;
|
|
503
|
+
const engine: *Engine = @ptrFromInt(handle);
|
|
504
|
+
length.* = engine.output.items.len;
|
|
505
|
+
return engine.output.items.ptr;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/// Candidates scored by the last query; lets callers verify narrowing.
|
|
509
|
+
pub export fn core_fuzzy_scanned(handle: u64) u64 {
|
|
510
|
+
if (handle == 0) return 0;
|
|
511
|
+
const engine: *Engine = @ptrFromInt(handle);
|
|
512
|
+
return engine.scanned;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
pub export fn core_fuzzy_destroy(handle: u64) void {
|
|
516
|
+
if (handle == 0) return;
|
|
517
|
+
const engine: *Engine = @ptrFromInt(handle);
|
|
518
|
+
engine.deinit();
|
|
519
|
+
ffi_allocator.destroy(engine);
|
|
520
|
+
}
|