Skip to content

Commit c93d1fd

Browse files
r-barnesfacebook-github-bot
authored andcommitted
Fix shadowed variable in faiss/impl/HNSW.cpp (#3961)
Summary: Pull Request resolved: #3961 Our upcoming compiler upgrade will require us not to have shadowed variables. Such variables have a _high_ bug rate and reduce readability, so we would like to avoid them even if the compiler was not forcing us to do so. This codemod attempts to fix an instance of a shadowed variable. Please review with care: if it's failed the result will be a silent bug. **What's a shadowed variable?** Shadowed variables are variables in an inner scope with the same name as another variable in an outer scope. Having the same name for both variables might be semantically correct, but it can make the code confusing to read! It can also hide subtle bugs. This diff fixes such an issue by renaming the variable. - If you approve of this diff, please use the "Accept & Ship" button :-) Reviewed By: meyering Differential Revision: D64398743 fbshipit-source-id: 3ec24a1655133ee0d3b94a55e38857ffa8853268
1 parent 68f66bc commit c93d1fd

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

faiss/impl/HNSW.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ void HNSW::print_neighbor_stats(int level) const {
166166
}
167167

168168
void HNSW::fill_with_random_links(size_t n) {
169-
int max_level = prepare_level_tab(n);
169+
int max_level_2 = prepare_level_tab(n);
170170
RandomGenerator rng2(456);
171171

172-
for (int level = max_level - 1; level >= 0; --level) {
172+
for (int level = max_level_2 - 1; level >= 0; --level) {
173173
std::vector<int> elts;
174174
for (int i = 0; i < n; i++) {
175175
if (levels[i] > level) {
@@ -210,16 +210,16 @@ int HNSW::prepare_level_tab(size_t n, bool preset_levels) {
210210
}
211211
}
212212

213-
int max_level = 0;
213+
int max_level_2 = 0;
214214
for (int i = 0; i < n; i++) {
215215
int pt_level = levels[i + n0] - 1;
216-
if (pt_level > max_level)
217-
max_level = pt_level;
216+
if (pt_level > max_level_2)
217+
max_level_2 = pt_level;
218218
offsets.push_back(offsets.back() + cum_nb_neighbors(pt_level + 1));
219219
}
220220
neighbors.resize(offsets.back(), -1);
221221

222-
return max_level;
222+
return max_level_2;
223223
}
224224

225225
/** Enumerate vertices from nearest to farthest from query, keep a
@@ -493,17 +493,17 @@ void HNSW::add_links_starting_from(
493493

494494
::faiss::shrink_neighbor_list(ptdis, link_targets, M, keep_max_size_level0);
495495

496-
std::vector<storage_idx_t> neighbors;
497-
neighbors.reserve(link_targets.size());
496+
std::vector<storage_idx_t> neighbors_2;
497+
neighbors_2.reserve(link_targets.size());
498498
while (!link_targets.empty()) {
499499
storage_idx_t other_id = link_targets.top().id;
500500
add_link(*this, ptdis, pt_id, other_id, level, keep_max_size_level0);
501-
neighbors.push_back(other_id);
501+
neighbors_2.push_back(other_id);
502502
link_targets.pop();
503503
}
504504

505505
omp_unset_lock(&locks[pt_id]);
506-
for (storage_idx_t other_id : neighbors) {
506+
for (storage_idx_t other_id : neighbors_2) {
507507
omp_set_lock(&locks[other_id]);
508508
add_link(*this, ptdis, other_id, pt_id, level, keep_max_size_level0);
509509
omp_unset_lock(&locks[other_id]);

0 commit comments

Comments
 (0)