Skip to content

Commit

Permalink
Fix shadowed variable in faiss/impl/PolysemousTraining.cpp
Browse files Browse the repository at this point in the history
Summary:
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: D52582870

fbshipit-source-id: 4c81218e109ff97052036645ab33bc9b17802e91
  • Loading branch information
r-barnes authored and facebook-github-bot committed Jan 16, 2024
1 parent 3973017 commit 4150fb1
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions faiss/impl/PolysemousTraining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,18 +683,21 @@ struct RankingScore2 : Score3Computer<float, double> {
double accum_gt_weight_diff(
const std::vector<int>& a,
const std::vector<int>& b) {
int nb = b.size(), na = a.size();
const auto nb_2 = b.size();
const auto na = a.size();

double accu = 0;
int j = 0;
for (int i = 0; i < na; i++) {
int ai = a[i];
while (j < nb && ai >= b[j])
size_t j = 0;
for (size_t i = 0; i < na; i++) {
const auto ai = a[i];
while (j < nb_2 && ai >= b[j]) {
j++;
}

double accu_i = 0;
for (int k = j; k < b.size(); k++)
for (auto k = j; k < b.size(); k++) {
accu_i += rank_weight(b[k] - ai);
}

accu += rank_weight(ai) * accu_i;
}
Expand Down

0 comments on commit 4150fb1

Please sign in to comment.