Skip to content

Commit c7d9919

Browse files
authored
fix(range-search): use size_t capacities (#268)
Use larger capacities for neighbor search, because we run out of capacity when doing a range search with a large radius. Note: The functions return `size_t`, so a `narrow_cast<uint32_t>` is still required. See: ahuber21/faiss#51
1 parent 5588e06 commit c7d9919

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

include/svs/index/vamana/dynamic_search_buffer.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,16 @@ template <typename Idx, typename Cmp = std::less<>> class MutableBuffer {
7676

7777
[[no_unique_address]] Cmp compare_{};
7878
// Equivalent of the `search_window_size_` in the traditional search buffer.
79-
uint16_t target_valid_ = 0;
79+
size_t target_valid_ = 0;
8080
// Number of valid elements can are configured to contain.
8181
// Equivalent to the `search_buffer_capacity_` in the traditional search buffer.
82-
uint16_t valid_capacity_ = 0;
82+
size_t valid_capacity_ = 0;
8383
// Index of the best unvisited candidate.
84-
uint16_t best_unvisited_ = 0;
84+
size_t best_unvisited_ = 0;
8585
// One past the "target_valid_"th entry.
86-
uint16_t roi_end_ = 0;
86+
size_t roi_end_ = 0;
8787
// number of valid neighbors.
88-
uint16_t valid_ = 0;
88+
size_t valid_ = 0;
8989
// A buffer of candidates.
9090
// Unlike the static buffer, this container *does* dynamically change size and does not
9191
// reserve one-past-the-end for copying neighbors.
@@ -101,8 +101,8 @@ template <typename Idx, typename Cmp = std::less<>> class MutableBuffer {
101101
SearchBufferConfig config, Cmp compare = Cmp{}, bool enable_visited = false
102102
)
103103
: compare_{std::move(compare)}
104-
, target_valid_{lib::narrow<uint16_t>(config.get_search_window_size())}
105-
, valid_capacity_{lib::narrow<uint16_t>(config.get_total_capacity())}
104+
, target_valid_{config.get_search_window_size()}
105+
, valid_capacity_{config.get_total_capacity()}
106106
, candidates_{valid_capacity_} {
107107
candidates_.clear();
108108
if (enable_visited) {
@@ -142,10 +142,8 @@ template <typename Idx, typename Cmp = std::less<>> class MutableBuffer {
142142
void change_maxsize(SearchBufferConfig config) {
143143
// Use temporary variables to ensure the given configuration is valid before
144144
// committing.
145-
//
146-
// `lib::narrow` may throw.
147-
uint16_t target_valid_temp = lib::narrow<uint16_t>(config.get_search_window_size());
148-
uint16_t valid_capacity_temp = lib::narrow<uint16_t>(config.get_total_capacity());
145+
size_t target_valid_temp = config.get_search_window_size();
146+
size_t valid_capacity_temp = config.get_total_capacity();
149147

150148
// If the new capacity is lower then the current size, shrink the buffer to fit.
151149
if (valid_capacity_temp < candidates_.size()) {
@@ -466,7 +464,7 @@ template <typename Idx, typename Cmp = std::less<>> class MutableBuffer {
466464
size_t i = pos - start;
467465
valid_ += static_cast<size_t>(neighbor.valid());
468466
unsafe_insert(neighbor, pos, i);
469-
best_unvisited_ = std::min(best_unvisited_, lib::narrow_cast<uint16_t>(i));
467+
best_unvisited_ = std::min(best_unvisited_, i);
470468
return i;
471469
}
472470

0 commit comments

Comments
 (0)