Why &mut self for fn search in trait Index
ava57r opened this issue · comments
Hello.
Faiss binding required only *const FaissIndex
https://github.com/Enet4/faiss-rs/blob/master/faiss-sys/src/bindings.rs#L108
but here &mut self
https://github.com/Enet4/faiss-rs/blob/master/src/macros.rs#L71
https://github.com/Enet4/faiss-rs/blob/master/src/macros.rs#L76
A mutable reference to the index is required to comply with the thread safety guarantees of index implementations. In particular, an index of an unknown implementation is not guaranteed to be thread safe, even for searching. While the native Faiss API (and by consequence the C API) are more lenient on this matter by letting this operation be performed over a const reference, Rust has stricter aliasing requirements which are leveraged here to prevent data races at compile-time.
In general, you are advised to follow the guidelines for concurrent index searching in the wiki page above. Since they are already backed by multiple threads internally, calling search
from multiple threads can cripple performance. As a workaround, in the case of CPU-backed indexes, if you downcast the index to its implementation, then the interface on the ConcurrentIndex
trait will become available, which lets you search through an immutable reference.
Thanks.
I see.
https://docs.rs/faiss/0.9.0/faiss/index/trait.ConcurrentIndex.html
"... batched querying"
How can I split results for 2 different queries from SearchResult struct?
The API is currently a bit thin here, but the logic is the same as in the native API: in search
and assign
, each query maps to k
search results. So to get to the ith result of the jth query, you can index the distance or index property by j * k + i
.
Thanks!
I am trying.