Skip to content

Commit

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

Differential Revision: D52582927

fbshipit-source-id: be03fb1354898d0637d287f285ea99d096c39008
  • Loading branch information
r-barnes authored and facebook-github-bot committed Jan 16, 2024
1 parent 46320e0 commit 3973017
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions faiss/IndexBinaryIVF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ void IndexBinaryIVF::search(
FAISS_THROW_IF_NOT(k > 0);
FAISS_THROW_IF_NOT(nprobe > 0);

const size_t nprobe = std::min(nlist, this->nprobe);
std::unique_ptr<idx_t[]> idx(new idx_t[n * nprobe]);
std::unique_ptr<int32_t[]> coarse_dis(new int32_t[n * nprobe]);
const size_t nprobe_2 = std::min(nlist, this->nprobe);
std::unique_ptr<idx_t[]> idx(new idx_t[n * nprobe_2]);
std::unique_ptr<int32_t[]> coarse_dis(new int32_t[n * nprobe_2]);

double t0 = getmillisecs();
quantizer->search(n, x, nprobe, coarse_dis.get(), idx.get());
quantizer->search(n, x, nprobe_2, coarse_dis.get(), idx.get());
indexIVF_stats.quantization_time += getmillisecs() - t0;

t0 = getmillisecs();
invlists->prefetch_lists(idx.get(), n * nprobe);
invlists->prefetch_lists(idx.get(), n * nprobe_2);

search_preassigned(
n, x, k, idx.get(), coarse_dis.get(), distances, labels, false);
Expand Down Expand Up @@ -169,16 +169,16 @@ void IndexBinaryIVF::search_and_reconstruct(
const SearchParameters* params) const {
FAISS_THROW_IF_NOT_MSG(
!params, "search params not supported for this index");
const size_t nprobe = std::min(nlist, this->nprobe);
const size_t nprobe_2 = std::min(nlist, this->nprobe);
FAISS_THROW_IF_NOT(k > 0);
FAISS_THROW_IF_NOT(nprobe > 0);
FAISS_THROW_IF_NOT(nprobe_2 > 0);

std::unique_ptr<idx_t[]> idx(new idx_t[n * nprobe]);
std::unique_ptr<int32_t[]> coarse_dis(new int32_t[n * nprobe]);
std::unique_ptr<idx_t[]> idx(new idx_t[n * nprobe_2]);
std::unique_ptr<int32_t[]> coarse_dis(new int32_t[n * nprobe_2]);

quantizer->search(n, x, nprobe, coarse_dis.get(), idx.get());
quantizer->search(n, x, nprobe_2, coarse_dis.get(), idx.get());

invlists->prefetch_lists(idx.get(), n * nprobe);
invlists->prefetch_lists(idx.get(), n * nprobe_2);

// search_preassigned() with `store_pairs` enabled to obtain the list_no
// and offset into `codes` for reconstruction
Expand Down Expand Up @@ -321,8 +321,8 @@ struct IVFBinaryScannerL2 : BinaryInvertedListScanner {
}

idx_t list_no;
void set_list(idx_t list_no, uint8_t /* coarse_dis */) override {
this->list_no = list_no;
void set_list(idx_t list_no_2, uint8_t /* coarse_dis */) override {
this->list_no = list_no_2;
}

uint32_t distance_to_code(const uint8_t* code) const override {
Expand Down Expand Up @@ -812,16 +812,16 @@ void IndexBinaryIVF::range_search(
const SearchParameters* params) const {
FAISS_THROW_IF_NOT_MSG(
!params, "search params not supported for this index");
const size_t nprobe = std::min(nlist, this->nprobe);
std::unique_ptr<idx_t[]> idx(new idx_t[n * nprobe]);
std::unique_ptr<int32_t[]> coarse_dis(new int32_t[n * nprobe]);
const size_t nprobe_2 = std::min(nlist, this->nprobe);
std::unique_ptr<idx_t[]> idx(new idx_t[n * nprobe_2]);
std::unique_ptr<int32_t[]> coarse_dis(new int32_t[n * nprobe_2]);

double t0 = getmillisecs();
quantizer->search(n, x, nprobe, coarse_dis.get(), idx.get());
quantizer->search(n, x, nprobe_2, coarse_dis.get(), idx.get());
indexIVF_stats.quantization_time += getmillisecs() - t0;

t0 = getmillisecs();
invlists->prefetch_lists(idx.get(), n * nprobe);
invlists->prefetch_lists(idx.get(), n * nprobe_2);

range_search_preassigned(n, x, radius, idx.get(), coarse_dis.get(), res);

Expand All @@ -835,7 +835,7 @@ void IndexBinaryIVF::range_search_preassigned(
const idx_t* __restrict assign,
const int32_t* __restrict centroid_dis,
RangeSearchResult* __restrict res) const {
const size_t nprobe = std::min(nlist, this->nprobe);
const size_t nprobe_2 = std::min(nlist, this->nprobe);
bool store_pairs = false;
size_t nlistv = 0, ndis = 0;

Expand All @@ -851,7 +851,7 @@ void IndexBinaryIVF::range_search_preassigned(
all_pres[omp_get_thread_num()] = &pres;

auto scan_list_func = [&](size_t i, size_t ik, RangeQueryResult& qres) {
idx_t key = assign[i * nprobe + ik]; /* select the list */
idx_t key = assign[i * nprobe_2 + ik]; /* select the list */
if (key < 0)
return;
FAISS_THROW_IF_NOT_FMT(
Expand All @@ -868,7 +868,7 @@ void IndexBinaryIVF::range_search_preassigned(
InvertedLists::ScopedCodes scodes(invlists, key);
InvertedLists::ScopedIds ids(invlists, key);

scanner->set_list(key, assign[i * nprobe + ik]);
scanner->set_list(key, assign[i * nprobe_2 + ik]);
nlistv++;
ndis += list_size;
scanner->scan_codes_range(
Expand All @@ -881,7 +881,7 @@ void IndexBinaryIVF::range_search_preassigned(

RangeQueryResult& qres = pres.new_result(i);

for (size_t ik = 0; ik < nprobe; ik++) {
for (size_t ik = 0; ik < nprobe_2; ik++) {
scan_list_func(i, ik, qres);
}
}
Expand Down

0 comments on commit 3973017

Please sign in to comment.