Skip to content

Commit

Permalink
Shrink level hash tables too.
Browse files Browse the repository at this point in the history
It's important to have O(1) begin() on these hash tables, because it's used in search.
  • Loading branch information
andrusha97 committed Oct 20, 2017
1 parent 88cc86d commit a02982a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions include/hnsw/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,18 @@ struct hnsw_index {

level_it->second.erase(key);

// Shrink the hash table when it becomes too sparse
// (to reduce memory usage and ensure linear complexity for iteration).
if (4 * level_it->second.load_factor() < level_it->second.max_load_factor()) {
level_it->second.rehash(size_t(2 * level_it->second.size() / level_it->second.max_load_factor()));
}

if (level_it->second.empty()) {
levels.erase(level_it);
}

nodes.erase(node_it);

// Shrink the hash table when it becomes too sparse
// (to reduce memory usage and ensure linear complexity for iteration).
if (4 * nodes.load_factor() < nodes.max_load_factor()) {
nodes.rehash(size_t(2 * nodes.size() / nodes.max_load_factor()));
}
Expand Down

0 comments on commit a02982a

Please sign in to comment.