Repository navigation
Description
Describe the bug
When many keys hold the same vector, most of them get no incoming base-level link, so no search returns them, not even a search for their own vector. With 1,248 equal keys on main, 984 keys have no incoming base-level link (counted from the base-level neighbor lists). This happens with one thread, and the metric or scalar kind does not change it (ip/i8 and cos/f32 in the program below give the same counts, and so do cos/i8 and l2sq/f32). A large group of equal vectors also hides distinct vectors that are added after it (part 2 below).
I think the cause is the tie order in form_reverse_links_ (all links at main 7a4abfb, which is v2.26.5):
- When a neighbor's list is full, the new node goes into
top_for_refinefirst, and the old members after it (index.hpp#L4393-L4397). insert_reservedinserts atstd::lower_bound(index.hpp#L1124), andcandidate_tcompares only the distance (index.hpp#L2405). So each old member with an equal distance goes in front of the new node, and the new node ends up last.refine_prunes a candidate only ifinter_result_dist < candidate.distance(index.hpp#L4861). This is never true for ties, so it keeps the firstneededcandidates and drops the new node.
With identical vectors every distance ties. Once the lists of the keys that a search reaches are full, each new key of the group gets no reverse link. Its own links point into the group, so it can reach others, but nothing reaches it. A search that cannot fill top visits every node that it can reach from its start node (index.hpp#L4760), so search(count = n) below counts the reachable keys. A larger expansion_search does not change the result.
This is a different trigger from #735 (concurrent add). PR #805 improves part 1, but does not fix it, and part 2 gets worse with it (numbers below). #805 also mentions duplicates: its remaining self-search misses "look like duplicate vectors tied for the top 10".
Context: we hit this in Seek (github.com/dualeai/seek), a code search tool that keeps i8 vectors of code windows in USearch graphs of 16,384 keys. Repeated text (generated code, OpenAPI JSON, test fixtures) gives many windows the same vector. In our index of the Kubernetes repository (5.4 million keys in 328 graphs), about 427,000 keys (8.0 %) share their i8 vector with another key in the same graph, and the largest such group has 155 keys (same(usearch_metric_cos_k, usearch_scalar_f32_k, "cos f32", 155) in the program below returns 150 keys on 2.26.2, 149 on 2.26.5, and all 155 with #805). We can work around it on our side (add one key per distinct vector and keep an alias table), but it would be good if the graph kept these keys reachable.
Steps to reproduce
// Many keys with the same vector, added from one thread.
// Build: cc -std=c99 -O2 same_vector.c -I<dir of usearch.h> -L<dir of libusearch_c> -lusearch_c
#include <stdio.h>
#include <stdlib.h>
#include "usearch.h"
enum { dims = 48 };
static usearch_index_t make(usearch_metric_kind_t metric, usearch_scalar_kind_t quantization, size_t capacity) {
usearch_error_t error = NULL;
usearch_init_options_t options = {0};
options.metric_kind = metric;
options.quantization = quantization;
options.dimensions = dims;
options.connectivity = 16;
options.expansion_add = 128;
options.expansion_search = 64;
usearch_index_t index = usearch_init(&options, &error);
usearch_reserve(index, capacity, &error);
return index;
}
// Part 1: `n` keys, all with the same vector. Search for that vector with count = n.
static void same(usearch_metric_kind_t metric, usearch_scalar_kind_t quantization, char const* name, size_t n) {
usearch_error_t error = NULL;
usearch_index_t index = make(metric, quantization, n);
float vector[dims];
for (size_t i = 0; i != dims; ++i) vector[i] = (float)(i % 7) - 3.0f;
for (size_t key = 0; key != n; ++key) usearch_add(index, key, vector, usearch_scalar_f32_k, &error);
usearch_key_t* keys = malloc(n * sizeof(usearch_key_t));
usearch_distance_t* distances = malloc(n * sizeof(usearch_distance_t));
size_t found = usearch_search(index, vector, usearch_scalar_f32_k, n, keys, distances, &error);
printf("%s, %4zu keys with the same vector: search(count=%zu) returns %zu keys\n", name, n, n, found);
free(keys), free(distances), usearch_free(index, &error);
}
// Part 2: 4096 keys. The first `group` keys share one vector; the others are random.
// Count the random keys that a search for their own vector (count=10) misses.
static void mixed(size_t group) {
enum { n = 4096 };
static float data[n][dims];
usearch_error_t error = NULL;
usearch_index_t index = make(usearch_metric_cos_k, usearch_scalar_f32_k, n);
srand(42);
for (size_t key = 0; key != n; ++key)
for (size_t i = 0; i != dims; ++i)
data[key][i] = key < group ? (float)(i % 7) - 3.0f : (float)rand() / (float)RAND_MAX - 0.5f;
for (size_t key = 0; key != n; ++key) usearch_add(index, key, data[key], usearch_scalar_f32_k, &error);
size_t missed = 0;
for (size_t key = group; key != n; ++key) {
usearch_key_t keys[10];
usearch_distance_t distances[10];
size_t found = usearch_search(index, data[key], usearch_scalar_f32_k, 10, keys, distances, &error);
int self = 0;
for (size_t i = 0; i != found; ++i) self |= keys[i] == key;
missed += !self;
}
printf("cos f32, %4d keys, %3zu share one vector: %zu of %zu random keys not found by their own vector\n", n,
group, missed, n - group);
usearch_free(index, &error);
}
int main(void) {
printf("USearch %s\n", usearch_version());
same(usearch_metric_ip_k, usearch_scalar_i8_k, "ip i8 ", 1248);
size_t sizes[] = {128, 256, 1248, 4096};
for (size_t i = 0; i != 4; ++i) same(usearch_metric_cos_k, usearch_scalar_f32_k, "cos f32", sizes[i]);
mixed(0);
mixed(256);
return 0;
}Release libusearch_c.dylib of v2.26.2 (usearch_macos_arm64_2.26.2.zip):
USearch 2.26.2
ip i8 , 1248 keys with the same vector: search(count=1248) returns 177 keys
cos f32, 128 keys with the same vector: search(count=128) returns 128 keys
cos f32, 256 keys with the same vector: search(count=256) returns 153 keys
cos f32, 1248 keys with the same vector: search(count=1248) returns 177 keys
cos f32, 4096 keys with the same vector: search(count=4096) returns 177 keys
cos f32, 4096 keys, 0 share one vector: 0 of 4096 random keys not found by their own vector
cos f32, 4096 keys, 256 share one vector: 366 of 3840 random keys not found by their own vector
Release libusearch_c.dylib of v2.26.5 (main 7a4abfb built from source gives the same lines):
USearch 2.26.5
ip i8 , 1248 keys with the same vector: search(count=1248) returns 264 keys
cos f32, 128 keys with the same vector: search(count=128) returns 128 keys
cos f32, 256 keys with the same vector: search(count=256) returns 182 keys
cos f32, 1248 keys with the same vector: search(count=1248) returns 264 keys
cos f32, 4096 keys with the same vector: search(count=4096) returns 349 keys
cos f32, 4096 keys, 0 share one vector: 0 of 4096 random keys not found by their own vector
cos f32, 4096 keys, 256 share one vector: 347 of 3840 random keys not found by their own vector
PR #805 at 634e4ac (c/lib.cpp built with -O3 -DNDEBUG):
USearch 2.26.5
ip i8 , 1248 keys with the same vector: search(count=1248) returns 366 keys
cos f32, 128 keys with the same vector: search(count=128) returns 128 keys
cos f32, 256 keys with the same vector: search(count=256) returns 256 keys
cos f32, 1248 keys with the same vector: search(count=1248) returns 366 keys
cos f32, 4096 keys with the same vector: search(count=4096) returns 868 keys
cos f32, 4096 keys, 0 share one vector: 0 of 4096 random keys not found by their own vector
cos f32, 4096 keys, 256 share one vector: 885 of 3840 random keys not found by their own vector
I do not have a clear fix. One data point: as an experiment, I changed form_reverse_links_ on main to insert the old members first and the new node last, so that the new node wins ties. Part 1 then returns 887 of 1,248 and 3,124 of 4,096 keys, but part 2 does not improve (394 of 3,840 missed). So the tie order explains part of it, not all.
Expected behavior
search(v, n) returns all n keys that hold v, because v is an exact match for each of them. A group of equal vectors does not hide other vectors.
USearch version
v2.26.2 and v2.26.5 (main 7a4abfb); also PR #805 at 634e4ac
Operating System
macOS 27.0.1
Hardware architecture
Arm
Which interface are you using?
Other bindings
Contact Details
No response
Are you open to being tagged as a contributor?
- I am open to being mentioned in the project
.githistory as a contributor
Is there an existing issue for this?
- I have searched the existing issues
Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
Fix: Keep every node reachable when links are pruned
swasik commentedon Nov 4, 2026
@clemlesne, #805 now fixes the first part of this. All 1,248 keys with one vector are found, against 177 on main-dev, and the same holds with cosine
f32for 256, 1,248 and 4,096 equal keys. The cause was ties inrefine_: lists of equal vectors filled with each other, so most of them were left with no incoming link. #805 now gives every node left with at most one incoming link a link from outside its own neighborhood.The mixed case improves but isn't closed: averaged over 10 seeds, 100 of 3,840 keys are missed, against 853 on main-dev. Closing it needs a tie rule in
refine_, which should be a separate change tested oni8andf16, so #805 only refers to this issue. Your reproducer is one of #805's tests, credited to you.ashvardanian commentedon Nov 14, 2026
@clemlesne, thanks for the precise diagnosis! 3.0 rebuilds the graph core and adds API designed for exactly this:
healthaudits how much of the graph a search can reach,refinere-selects neighbors and re-links what it finds unreachable, andcursorwalks the stored links, so a cluster of identical vectors can be inspected directly. Reachability will be a continuous space of improvement, so 3.0 won't land the final algorithm: it solidifies the API first, and we'll keep improving on it from there, so I'm keeping this open. cc @swasik