Skip to content

Commit 7a51922

Browse files
r-barnesfacebook-github-bot
authored andcommitted
Fix shadowed variable in faiss/IndexPQ.cpp (facebookresearch#3959)
Summary: Pull Request resolved: facebookresearch#3959 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: D64398686 fbshipit-source-id: 44c60ea6e99d9542acf5af15adba6cdccda95577
1 parent cb1a512 commit 7a51922

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

faiss/IndexPQ.cpp

+24-24
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,16 @@ void IndexPQ::search(
159159
FAISS_THROW_IF_NOT(is_trained);
160160

161161
const SearchParametersPQ* params = nullptr;
162-
Search_type_t search_type = this->search_type;
162+
Search_type_t search_type_2 = this->search_type;
163163

164164
if (iparams) {
165165
params = dynamic_cast<const SearchParametersPQ*>(iparams);
166166
FAISS_THROW_IF_NOT_MSG(params, "invalid search params");
167167
FAISS_THROW_IF_NOT_MSG(!params->sel, "selector not supported");
168-
search_type = params->search_type;
168+
search_type_2 = params->search_type;
169169
}
170170

171-
if (search_type == ST_PQ) { // Simple PQ search
171+
if (search_type_2 == ST_PQ) { // Simple PQ search
172172

173173
if (metric_type == METRIC_L2) {
174174
float_maxheap_array_t res = {
@@ -183,19 +183,19 @@ void IndexPQ::search(
183183
indexPQ_stats.ncode += n * ntotal;
184184

185185
} else if (
186-
search_type == ST_polysemous ||
187-
search_type == ST_polysemous_generalize) {
186+
search_type_2 == ST_polysemous ||
187+
search_type_2 == ST_polysemous_generalize) {
188188
FAISS_THROW_IF_NOT(metric_type == METRIC_L2);
189-
int polysemous_ht =
189+
int polysemous_ht_2 =
190190
params ? params->polysemous_ht : this->polysemous_ht;
191191
search_core_polysemous(
192192
n,
193193
x,
194194
k,
195195
distances,
196196
labels,
197-
polysemous_ht,
198-
search_type == ST_polysemous_generalize);
197+
polysemous_ht_2,
198+
search_type_2 == ST_polysemous_generalize);
199199

200200
} else { // code-to-code distances
201201

@@ -215,7 +215,7 @@ void IndexPQ::search(
215215
}
216216
}
217217

218-
if (search_type == ST_SDC) {
218+
if (search_type_2 == ST_SDC) {
219219
float_maxheap_array_t res = {
220220
size_t(n), size_t(k), labels, distances};
221221

@@ -227,7 +227,7 @@ void IndexPQ::search(
227227
int_maxheap_array_t res = {
228228
size_t(n), size_t(k), labels, idistances.get()};
229229

230-
if (search_type == ST_HE) {
230+
if (search_type_2 == ST_HE) {
231231
hammings_knn_hc(
232232
&res,
233233
q_codes.get(),
@@ -236,7 +236,7 @@ void IndexPQ::search(
236236
pq.code_size,
237237
true);
238238

239-
} else if (search_type == ST_generalized_HE) {
239+
} else if (search_type_2 == ST_generalized_HE) {
240240
generalized_hammings_knn_hc(
241241
&res,
242242
q_codes.get(),
@@ -322,13 +322,13 @@ void IndexPQ::search_core_polysemous(
322322
idx_t k,
323323
float* distances,
324324
idx_t* labels,
325-
int polysemous_ht,
325+
int polysemous_ht_2,
326326
bool generalized_hamming) const {
327327
FAISS_THROW_IF_NOT(k > 0);
328328
FAISS_THROW_IF_NOT(pq.nbits == 8);
329329

330-
if (polysemous_ht == 0) {
331-
polysemous_ht = pq.nbits * pq.M + 1;
330+
if (polysemous_ht_2 == 0) {
331+
polysemous_ht_2 = pq.nbits * pq.M + 1;
332332
}
333333

334334
// PQ distance tables
@@ -374,7 +374,7 @@ void IndexPQ::search_core_polysemous(
374374
k,
375375
heap_dis,
376376
heap_ids,
377-
polysemous_ht);
377+
polysemous_ht_2);
378378

379379
} else { // generalized hamming
380380
switch (pq.code_size) {
@@ -387,7 +387,7 @@ void IndexPQ::search_core_polysemous(
387387
k, \
388388
heap_dis, \
389389
heap_ids, \
390-
polysemous_ht); \
390+
polysemous_ht_2); \
391391
break;
392392
DISPATCH(8)
393393
DISPATCH(16)
@@ -401,7 +401,7 @@ void IndexPQ::search_core_polysemous(
401401
k,
402402
heap_dis,
403403
heap_ids,
404-
polysemous_ht);
404+
polysemous_ht_2);
405405
} else {
406406
bad_code_size++;
407407
}
@@ -516,8 +516,8 @@ struct PreSortedArray {
516516
int N;
517517

518518
explicit PreSortedArray(int N) : N(N) {}
519-
void init(const T* x) {
520-
this->x = x;
519+
void init(const T* x_2) {
520+
this->x = x_2;
521521
}
522522
// get smallest value
523523
T get_0() {
@@ -557,11 +557,11 @@ struct SortedArray {
557557
perm.resize(N);
558558
}
559559

560-
void init(const T* x) {
561-
this->x = x;
560+
void init(const T* x_2) {
561+
this->x = x_2;
562562
for (int n = 0; n < N; n++)
563563
perm[n] = n;
564-
ArgSort<T> cmp = {x};
564+
ArgSort<T> cmp = {x_2};
565565
std::sort(perm.begin(), perm.end(), cmp);
566566
}
567567

@@ -639,8 +639,8 @@ struct SemiSortedArray {
639639
k_factor = 4;
640640
}
641641

642-
void init(const T* x) {
643-
this->x = x;
642+
void init(const T* x_2) {
643+
this->x = x_2;
644644
for (int n = 0; n < N; n++)
645645
perm[n] = n;
646646
k = 0;

0 commit comments

Comments
 (0)