Skip to content

Commit 0fa88b5

Browse files
authored
introduce memory estimate for hgraph (#282)
Signed-off-by: LHT129 <[email protected]>
1 parent 8b990ad commit 0fa88b5

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

include/vsag/index.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,17 @@ class Index {
334334
[[nodiscard]] virtual int64_t
335335
GetMemoryUsage() const = 0;
336336

337+
/**
338+
* @brief estimate the memory used by the index with given element counts
339+
*
340+
* @param num_elements
341+
* @return number of bytes estimate used.
342+
*/
343+
[[nodiscard]] virtual uint64_t
344+
EstimateMemory(const uint64_t num_elements) const {
345+
throw std::runtime_error("Index not support estimate the memory by element counts");
346+
}
347+
337348
/**
338349
* @brief Return the estimated memory required during building
339350
*

src/algorithm/hgraph.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,45 @@ HGraph::KnnSearch(const DatasetPtr& query,
218218
}
219219
}
220220

221+
uint64_t
222+
HGraph::EstimateMemory(const uint64_t num_elements) const {
223+
uint64_t estimate_memory = 0;
224+
auto block_size = Options::Instance().block_size_limit();
225+
auto element_count =
226+
next_multiple_of_power_of_two(num_elements, this->resize_increase_count_bit_);
227+
228+
auto block_memory_ceil = [](uint64_t memory, uint64_t block_size) -> uint64_t {
229+
return static_cast<uint64_t>(
230+
std::ceil(static_cast<double>(memory) / static_cast<double>(block_size)) *
231+
static_cast<double>(block_size));
232+
};
233+
234+
auto base_memory = this->basic_flatten_codes_->code_size_ * element_count;
235+
estimate_memory += block_memory_ceil(base_memory, block_size);
236+
237+
auto bottom_graph_memory =
238+
(this->bottom_graph_->maximum_degree_ + 1) * sizeof(InnerIdType) * element_count;
239+
estimate_memory += block_memory_ceil(bottom_graph_memory, block_size);
240+
241+
if (use_reorder_) {
242+
auto precise_memory = this->high_precise_codes_->code_size_ * element_count;
243+
estimate_memory += block_memory_ceil(precise_memory, block_size);
244+
}
245+
246+
auto label_map_memory =
247+
element_count * (sizeof(std::pair<LabelType, InnerIdType>) + 2 * sizeof(void*));
248+
estimate_memory += label_map_memory;
249+
250+
auto sparse_graph_memory = (this->mult_ * 0.05 * element_count) * sizeof(InnerIdType) *
251+
(this->bottom_graph_->maximum_degree_ / 2 + 1);
252+
estimate_memory += sparse_graph_memory;
253+
254+
auto other_memory = element_count * (sizeof(LabelType) + sizeof(std::shared_mutex));
255+
estimate_memory += other_memory;
256+
257+
return estimate_memory;
258+
}
259+
221260
tl::expected<BinarySet, Error>
222261
HGraph::Serialize() const {
223262
if (GetNumElements() == 0) {

src/algorithm/hgraph.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class HGraph {
9797
return this->basic_flatten_codes_->TotalCount();
9898
}
9999

100+
uint64_t
101+
EstimateMemory(const uint64_t num_elements) const;
102+
100103
// TODO(LHT): implement
101104
inline int64_t
102105
GetMemoryUsage() const {

src/index/hgraph_index.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ class HGraphIndex : public Index {
136136
return this->hgraph_->GetMemoryUsage();
137137
}
138138

139+
[[nodiscard]] uint64_t
140+
EstimateMemory(const uint64_t num_elements) const override {
141+
return this->hgraph_->EstimateMemory(num_elements);
142+
}
143+
139144
bool
140145
CheckFeature(IndexFeature feature) const override {
141146
return this->hgraph_->CheckFeature(feature);

0 commit comments

Comments
 (0)