From 4150fb1827dc4b720c2814b6e983530fee155d59 Mon Sep 17 00:00:00 2001 From: Richard Barnes Date: Mon, 15 Jan 2024 20:53:34 -0800 Subject: [PATCH] Fix shadowed variable in faiss/impl/PolysemousTraining.cpp 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 --- faiss/impl/PolysemousTraining.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/faiss/impl/PolysemousTraining.cpp b/faiss/impl/PolysemousTraining.cpp index 1f01fc9dcf..a3bd400fb6 100644 --- a/faiss/impl/PolysemousTraining.cpp +++ b/faiss/impl/PolysemousTraining.cpp @@ -683,18 +683,21 @@ struct RankingScore2 : Score3Computer { double accum_gt_weight_diff( const std::vector& a, const std::vector& 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; }