1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: Apache-2.0
//! Per-invocation scratch arena for HNSW beam-search heaps.
//!
//! `BeamSearchArena` holds pre-allocated backing buffers for the three
//! data structures consumed by every `search_layer` call:
//!
//! - `candidates` — min-heap of `(dist, id)` pairs (wrapped in `Reverse`).
//! - `results` — max-heap of `(dist, id)` pairs.
//! - `visited` — hash set tracking nodes seen during traversal.
//!
//! The arena is owned by `HnswIndex` (inside a `RefCell`) and reset at the
//! start of each search. Over time the backing `Vec`s grow to the
//! high-water-mark capacity of the largest search executed, giving amortised
//! zero-allocation steady state.
//!
//! `BeamSearchArena` is intentionally a per-core resource. It must not be
//! shared or accessed concurrently — the wrapping `RefCell` in `HnswIndex`
//! enforces single-borrower access at runtime.
use Reverse;
use HashSet;
use Candidate;
/// Scratch arena for a single HNSW beam-search invocation.
///
/// Call [`BeamSearchArena::reset`] at the start of each search to clear the
/// buffers while retaining their allocated capacity.