query/commands/wire: trim the scan and request paths

Matching allocated an ArrayList per filter field per candidate document,
on the process-wide allocator, to hold what is almost always a single
value. Candidates now collect into a stack buffer that spills to the heap
only for arrays: measured 15.7 -> 12.0ms on a 65,536-document range scan.

The OOM-propagation test moves with it. Its point is that a failed
collection must surface as an error rather than an empty candidate list,
which would make $ne and $exists:false report a match -- a wrong answer
rather than a failed one. That invariant still holds on the spill path, so
the test now uses an array long enough to reach the allocator, and a new
test pins the flip side: the common single-value match now completes
correctly even when the allocator always fails, because it never calls it.

Query operators were dispatched by a chain of up to fourteen mem.eql per
value per document, with $gt/$gte/$lt/$lte re-comparing the operator name
inside the loop over candidate values. Names resolve to an enum once per
filter field. Command dispatch likewise walked a 30-entry table comparing
strings; it is a comptime StaticStringMap now.

Each request built a fresh reply arena and handed its pages straight back.
One reply per connection, reset between requests, keeps them.

countDocuments() arrives as [{$match: F}?, {$group: {_id: <literal>,
n: {$sum: 1}}}], which the general path answered by materializing every
matching document and discarding them all. It is now recognized and
answered from a counting scan: countDocuments({}) 2.3 -> 1.5ms.

The detector is deliberately conservative -- grouping by "$field", summing
a field, an unmodelled accumulator or any extra stage all fall through to
the general path, since those need the documents themselves. A unit test
pins each accept and reject, and the whole count path was checked against
the general one through the real driver, including the shapes that must
not take it.

The filtered range-scan row does not move: it is bound by walking 65,536
documents that each live in their own arena, not by the matcher. That is
Phase 4 work.

Verified: 77 unit tests under ReleaseFast and ReleaseSafe, e2e
29/16/17/3/2, the crash pair, e2e6 72/72.
This commit is contained in:
2026-08-02 19:13:29 +03:00
parent 552b916833
commit 75e412a4af
5 changed files with 327 additions and 61 deletions

View File

@@ -223,6 +223,18 @@ pub const Reply = struct {
self.arena.deinit();
}
/// Ready this reply for the next request on the same connection. The
/// arena keeps its pages instead of handing them back and asking the
/// allocator for fresh ones on every single command.
pub fn reset(self: *Reply) void {
_ = self.arena.reset(.retain_capacity);
// Resetting the arena invalidated every allocation made from it,
// including the pairs buffer — so drop it rather than reusing the
// (now dangling) capacity. Regrowing it just bumps the arena
// pointer through memory we already hold.
self.pairs = .empty;
}
pub fn arena_alloc(self: *Reply) std.mem.Allocator {
return self.arena.allocator();
}