Skip to content

Commit cdce7a9

Browse files
authored
Merge pull request #35619 from itrofimow/improve_hnsw_loading_times
feat searchlib: Improve HNSW-index loading times
2 parents 16d57e2 + 38be89f commit cdce7a9

4 files changed

Lines changed: 19 additions & 3 deletions

File tree

searchlib/src/vespa/searchlib/tensor/hnsw_index_loader.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class HnswIndexLoader : public NearestNeighborIndexLoader {
3939
return _reader->readHostOrder();
4040
}
4141

42+
void next_N_ints(uint32_t* buf, size_t n) {
43+
_reader->readNHostOrder(buf, n);
44+
}
45+
4246
public:
4347
HnswIndexLoader(HnswGraph<type>& graph, IdMapping& id_mapping, std::unique_ptr<ReaderType> reader);
4448
virtual ~HnswIndexLoader();

searchlib/src/vespa/searchlib/tensor/hnsw_index_loader.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ HnswIndexLoader<ReaderType, type>::HnswIndexLoader(HnswGraph<type>& graph, IdMap
3434
_id_mapping(id_mapping)
3535
{
3636
init();
37+
38+
graph.nodes.reserve(_num_nodes);
3739
}
3840

3941
template <typename ReaderType, HnswIndexType type>
@@ -50,9 +52,9 @@ HnswIndexLoader<ReaderType, type>::load_next()
5052
_graph.make_node(_nodeid, docid, subspace, num_levels);
5153
for (uint32_t level = 0; level < num_levels; ++level) {
5254
uint32_t num_links = next_int();
53-
_link_array.clear();
54-
while (num_links-- > 0) {
55-
_link_array.push_back(next_int());
55+
_link_array.resize(num_links);
56+
if (num_links != 0) {
57+
next_N_ints(_link_array.data(), num_links);
5658
}
5759
_graph.set_link_array(_nodeid, level, _link_array);
5860
}

searchlib/src/vespa/searchlib/test/vector_buffer_reader.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class VectorBufferReader {
2828
_pos += sizeof(uint32_t);
2929
return result;
3030
}
31+
32+
void readNHostOrder(uint32_t* buf, size_t n) {
33+
assert(_pos + sizeof(uint32_t) * n <= _data.size());
34+
std::memcpy(buf, _data.data() + _pos, sizeof(uint32_t) * n);
35+
_pos += sizeof(uint32_t) * n;
36+
}
3137
};
3238

3339
}

searchlib/src/vespa/searchlib/util/fileutil.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class FileReader : public FileReaderBase
9696
read(&result, sizeof(result));
9797
return result;
9898
}
99+
100+
void readNHostOrder(T* buf, size_t n) {
101+
read(buf, sizeof(T) * n);
102+
}
99103
};
100104

101105
template <typename T>

0 commit comments

Comments
 (0)