Skip to content

Commit

Permalink
Fix shadowed variable in faiss/MatrixStats.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: algoriddle

Differential Revision: D52582890

fbshipit-source-id: ef7e44805fb212c6415030b98f65c00b2fd4a9b7
  • Loading branch information
r-barnes authored and facebook-github-bot committed Jan 9, 2024
1 parent db09984 commit 952941b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions faiss/MatrixStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ MatrixStats::MatrixStats(size_t n, size_t d, const float* x) : n(n), d(d) {

double max_std = 0, min_std = HUGE_VAL;

size_t n_dangerous_range = 0, n_0_range = 0, n0 = 0;
size_t n_dangerous_range = 0, n_0_range = 0, n0_2 = 0;

for (size_t j = 0; j < d; j++) {
PerDimStats& st = per_dim_stats[j];
st.compute_mean_std();
n0 += st.n0;
n0_2 += st.n0;

if (st.max == st.min) {
n_0_range++;
Expand All @@ -200,12 +200,12 @@ MatrixStats::MatrixStats(size_t n, size_t d, const float* x) : n(n), d(d) {
min_std = st.stddev;
}

if (n0 == 0) {
if (n0_2 == 0) {
do_comment("matrix contains no 0s\n");
} else {
do_comment(
"matrix contains %.2f %% 0 entries\n",
n0 * 100.0 / (n * d));
n0_2 * 100.0 / (n * d));
}

if (n_0_range == 0) {
Expand Down

0 comments on commit 952941b

Please sign in to comment.