Skip to content

Commit 0ec06dc

Browse files
committed
Add: reserve_capacity_and_threads for Rust
1 parent a75bb4d commit 0ec06dc

File tree

6 files changed

+25
-5
lines changed

6 files changed

+25
-5
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@
135135
"xstring": "cpp",
136136
"xtr1common": "cpp",
137137
"xtree": "cpp",
138-
"xutility": "cpp"
138+
"xutility": "cpp",
139+
"*.rs": "cpp"
139140
},
140141
"cSpell.words": [
141142
"allclose",

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ cxx = "1.0.160"
3535
cxx-build = "1.0.160"
3636

3737
[dev-dependencies]
38-
fork_union = "2.1.0" # for concurrency tests
38+
fork_union = "2.1.1" # for concurrency tests
3939
rand_distr = "0.5.1" # uniform floats distribution
4040
rand_chacha = "0.9.0" # random number generator
4141
rand = "0.9.1"

rust/lib.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ size_t NativeIndex::count(vector_key_t key) const { return index_->count(key); }
134134
bool NativeIndex::contains(vector_key_t key) const { return index_->contains(key); }
135135

136136
void NativeIndex::reserve(size_t capacity) const { index_->reserve(capacity); }
137+
void NativeIndex::reserve_capacity_and_threads(size_t capacity, size_t threads) const {
138+
index_->reserve({capacity, threads});
139+
}
137140

138141
size_t NativeIndex::dimensions() const { return index_->dimensions(); }
139142
size_t NativeIndex::connectivity() const { return index_->connectivity(); }

rust/lib.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class NativeIndex {
2525
NativeIndex(std::unique_ptr<index_dense_t> index);
2626

2727
void reserve(size_t) const;
28+
void reserve_capacity_and_threads(size_t, size_t) const;
2829

2930
void add_b1x8(vector_key_t key, rust::Slice<uint8_t const> vector) const;
3031
void add_i8(vector_key_t key, rust::Slice<int8_t const> vector) const;

rust/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ pub mod ffi {
332332

333333
pub fn new_native_index(options: &IndexOptions) -> Result<UniquePtr<NativeIndex>>;
334334
pub fn reserve(self: &NativeIndex, capacity: usize) -> Result<()>;
335+
pub fn reserve_capacity_and_threads(self: &NativeIndex, capacity: usize, threads: usize) -> Result<()>;
335336
pub fn dimensions(self: &NativeIndex) -> usize;
336337
pub fn connectivity(self: &NativeIndex) -> usize;
337338
pub fn size(self: &NativeIndex) -> usize;
@@ -1143,6 +1144,20 @@ impl Index {
11431144
self.inner.reserve(capacity)
11441145
}
11451146

1147+
/// Reserves memory for a specified number of incoming vectors & active threads.
1148+
///
1149+
/// # Arguments
1150+
///
1151+
/// * `capacity` - The desired total capacity, including the current size.
1152+
/// * `threads` - The number of threads to use for the operation.
1153+
pub fn reserve_capacity_and_threads(
1154+
self: &Index,
1155+
capacity: usize,
1156+
threads: usize,
1157+
) -> Result<(), cxx::Exception> {
1158+
self.inner.reserve_capacity_and_threads(capacity, threads)
1159+
}
1160+
11461161
/// Retrieves the number of dimensions in the vectors indexed.
11471162
pub fn dimensions(self: &Index) -> usize {
11481163
self.inner.dimensions()
@@ -1758,7 +1773,7 @@ mod tests {
17581773
};
17591774

17601775
let index = Arc::new(Index::new(&options).unwrap());
1761-
index.reserve(VECTOR_COUNT).unwrap();
1776+
index.reserve_capacity_and_threads(VECTOR_COUNT, THREAD_COUNT).unwrap();
17621777

17631778
// Generate deterministic vectors using rand crate for reproducible testing
17641779
let seed = 42; // Fixed seed for reproducibility

0 commit comments

Comments
 (0)