Skip to content

Commit 89e814e

Browse files
junjieqifacebook-github-bot
authored andcommitted
Add braces around single-line control flow statements
Summary: Fix 136 `readability-braces-around-statements` lint warnings by adding braces to all single-line `if`, `else`, `for`, and `while` statements across 4 files: `IndexLSH.cpp` (14 issues), `IndexIVFPQ.cpp` (36 issues), `IndexPQ.cpp` (25 issues), and `VectorTransform.cpp` (61 issues). This improves code readability and prevents bugs from accidentally adding statements to unbraced blocks. Differential Revision: D100580151
1 parent 407b61e commit 89e814e

File tree

4 files changed

+136
-71
lines changed

4 files changed

+136
-71
lines changed

faiss/IndexIVFPQ.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ void IndexIVFPQ::train_encoder(
7171
pq.train(n, x);
7272

7373
if (do_polysemous_training) {
74-
if (verbose)
74+
if (verbose) {
7575
printf("doing polysemous training for PQ\n");
76+
}
7677
PolysemousTraining default_pt;
7778
PolysemousTraining* pt =
7879
polysemous_training ? polysemous_training : &default_pt;
@@ -97,8 +98,9 @@ void IndexIVFPQ::encode(idx_t key, const float* x, uint8_t* code) const {
9798
std::vector<float> residual_vec(d);
9899
quantizer->compute_residual(x, residual_vec.data(), key);
99100
pq.compute_code(residual_vec.data(), code);
100-
} else
101+
} else {
101102
pq.compute_code(x, code);
103+
}
102104
}
103105

104106
void IndexIVFPQ::encode_multiple(
@@ -107,8 +109,9 @@ void IndexIVFPQ::encode_multiple(
107109
const float* x,
108110
uint8_t* xcodes,
109111
bool compute_keys) const {
110-
if (compute_keys)
112+
if (compute_keys) {
111113
quantizer->assign(n, x, keys);
114+
}
112115

113116
encode_vectors(n, x, keys, xcodes);
114117
}
@@ -153,11 +156,12 @@ static std::unique_ptr<float[]> compute_residuals(
153156
// Parallelize with OpenMP (each iteration is independent)
154157
#pragma omp parallel for if (n > 1000)
155158
for (idx_t i = 0; i < n; i++) {
156-
if (list_nos[i] < 0)
159+
if (list_nos[i] < 0) {
157160
memset(residuals.get() + i * d, 0, sizeof(float) * d);
158-
else
161+
} else {
159162
quantizer->compute_residual(
160163
x + i * d, residuals.get() + i * d, list_nos[i]);
164+
}
161165
}
162166
return residuals;
163167
}
@@ -290,8 +294,9 @@ void IndexIVFPQ::add_core_o(
290294
if (key < 0) {
291295
direct_map.add_single_id(id, -1, 0);
292296
n_ignore++;
293-
if (residuals_2)
297+
if (residuals_2) {
294298
memset(residuals_2, 0, sizeof(*residuals_2) * d);
299+
}
295300
continue;
296301
}
297302

@@ -303,8 +308,9 @@ void IndexIVFPQ::add_core_o(
303308
float* res2 = residuals_2 + i * d;
304309
const float* xi = to_encode + i * d;
305310
pq.decode(code, res2);
306-
for (int j = 0; j < d; j++)
311+
for (int j = 0; j < d; j++) {
307312
res2[j] = xi[j] - res2[j];
313+
}
308314
}
309315

310316
direct_map.add_single_id(id, key, offset);
@@ -313,8 +319,9 @@ void IndexIVFPQ::add_core_o(
313319
double t3 = getmillisecs();
314320
if (verbose) {
315321
char comment[100] = {0};
316-
if (n_ignore > 0)
322+
if (n_ignore > 0) {
317323
snprintf(comment, 100, "(%zd vectors ignored)", n_ignore);
324+
}
318325
printf(" add_core times: %.3f %.3f %.3f %s\n",
319326
t1 - t0,
320327
t2 - t1,
@@ -401,9 +408,9 @@ void initialize_IVFPQ_precomputed_table(
401408
}
402409
const MultiIndexQuantizer* miq =
403410
dynamic_cast<const MultiIndexQuantizer*>(quantizer);
404-
if (miq && pq.M % miq->pq.M == 0)
411+
if (miq && pq.M % miq->pq.M == 0) {
405412
use_precomputed_table = 2;
406-
else {
413+
} else {
407414
size_t table_size = pq.M * pq.ksub * nlist * sizeof(float);
408415
if (table_size > precomputed_table_max_bytes) {
409416
if (verbose) {
@@ -425,11 +432,12 @@ void initialize_IVFPQ_precomputed_table(
425432

426433
// squared norms of the PQ centroids
427434
std::vector<float> r_norms(pq.M * pq.ksub, NAN);
428-
for (size_t m = 0; m < pq.M; m++)
435+
for (size_t m = 0; m < pq.M; m++) {
429436
for (size_t j = 0; j < pq.ksub; j++) {
430437
r_norms[m * pq.ksub + j] =
431438
fvec_norm_L2sqr_dispatch(pq.get_centroids(m, j), pq.dsub);
432439
}
440+
}
433441

434442
if (use_precomputed_table == 1) {
435443
precomputed_table.resize(nlist * pq.M * pq.ksub);
@@ -559,12 +567,14 @@ struct QueryTables {
559567
// query-specific initialization
560568
void init_query(const float* qi_in) {
561569
this->qi = qi_in;
562-
if (metric_type == METRIC_INNER_PRODUCT)
570+
if (metric_type == METRIC_INNER_PRODUCT) {
563571
init_query_IP();
564-
else
572+
} else {
565573
init_query_L2();
566-
if (!by_residual && polysemous_ht != 0)
574+
}
575+
if (!by_residual && polysemous_ht != 0) {
567576
pq.compute_code(qi_in, q_code.data());
577+
}
568578
}
569579

570580
void init_query_IP() {
@@ -599,10 +609,11 @@ struct QueryTables {
599609
uint64_t t0;
600610
TIC;
601611
if (by_residual) {
602-
if (metric_type == METRIC_INNER_PRODUCT)
612+
if (metric_type == METRIC_INNER_PRODUCT) {
603613
dis0 = precompute_list_tables_IP();
604-
else
614+
} else {
605615
dis0 = precompute_list_tables_L2();
616+
}
606617
}
607618
init_list_cycles += TOC;
608619
return dis0;
@@ -613,10 +624,11 @@ struct QueryTables {
613624
uint64_t t0;
614625
TIC;
615626
if (by_residual) {
616-
if (metric_type == METRIC_INNER_PRODUCT)
627+
if (metric_type == METRIC_INNER_PRODUCT) {
617628
FAISS_THROW_MSG("not implemented");
618-
else
629+
} else {
619630
dis0 = precompute_list_table_pointers_L2();
631+
}
620632
}
621633
init_list_cycles += TOC;
622634
return dis0;
@@ -1328,8 +1340,9 @@ size_t IndexIVFPQ::find_duplicates(idx_t* dup_ids, size_t* lims) const {
13281340
for (size_t list_no = 0; list_no < nlist; list_no++) {
13291341
size_t n = invlists->list_size(list_no);
13301342
std::vector<int> ord(n);
1331-
for (size_t i = 0; i < n; i++)
1343+
for (size_t i = 0; i < n; i++) {
13321344
ord[i] = static_cast<int>(i);
1345+
}
13331346
InvertedLists::ScopedCodes codes(invlists, list_no);
13341347
CodeCmp cs = {codes.get(), code_size};
13351348
std::sort(ord.begin(), ord.end(), cs);

faiss/IndexLSH.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ const float* IndexLSH::apply_preprocess(idx_t n, const float* x) const {
5858
float* xp = xt;
5959
for (idx_t i = 0; i < n; i++) {
6060
const float* xl = x + i * d;
61-
for (int j = 0; j < nbits; j++)
61+
for (int j = 0; j < nbits; j++) {
6262
*xp++ = xl[j];
63+
}
6364
}
6465
}
6566

@@ -70,9 +71,11 @@ const float* IndexLSH::apply_preprocess(idx_t n, const float* x) const {
7071
}
7172

7273
float* xp = xt;
73-
for (idx_t i = 0; i < n; i++)
74-
for (int j = 0; j < nbits; j++)
74+
for (idx_t i = 0; i < n; i++) {
75+
for (int j = 0; j < nbits; j++) {
7576
*xp++ -= thresholds[j];
77+
}
78+
}
7679
}
7780

7881
return xt ? xt : x;
@@ -88,9 +91,11 @@ void IndexLSH::train(idx_t n, const float* x) {
8891

8992
std::unique_ptr<float[]> transposed_x(new float[n * nbits]);
9093

91-
for (idx_t i = 0; i < n; i++)
92-
for (idx_t j = 0; j < nbits; j++)
94+
for (idx_t i = 0; i < n; i++) {
95+
for (idx_t j = 0; j < nbits; j++) {
9396
transposed_x[j * n + i] = xt[i * nbits + j];
97+
}
98+
}
9499

95100
for (idx_t i = 0; i < nbits; i++) {
96101
float* xi = transposed_x.get() + i * n;
@@ -132,20 +137,23 @@ void IndexLSH::search(
132137
hammings_knn_hc(&res, qcodes.get(), codes.data(), ntotal, code_size, true);
133138

134139
// convert distances to floats
135-
for (int i = 0; i < k * n; i++)
140+
for (int i = 0; i < k * n; i++) {
136141
distances[i] = idistances[i];
142+
}
137143
}
138144

139145
void IndexLSH::transfer_thresholds(LinearTransform* vt) {
140-
if (!train_thresholds)
146+
if (!train_thresholds) {
141147
return;
148+
}
142149
FAISS_THROW_IF_NOT(nbits == vt->d_out);
143150
if (!vt->have_bias) {
144151
vt->b.resize(nbits, 0);
145152
vt->have_bias = true;
146153
}
147-
for (int i = 0; i < nbits; i++)
154+
for (int i = 0; i < nbits; i++) {
148155
vt->b[i] -= thresholds[i];
156+
}
149157
train_thresholds = false;
150158
thresholds.clear();
151159
}

faiss/IndexPQ.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ void IndexPQ::train(idx_t n, const float* x) {
5353
} else {
5454
idx_t ntrain_perm = polysemous_training.ntrain_permutation;
5555

56-
if (ntrain_perm > n / 4)
56+
if (ntrain_perm > n / 4) {
5757
ntrain_perm = n / 4;
58+
}
5859
if (verbose) {
5960
printf("PQ training on %" PRId64 " points, remains %" PRId64
6061
" points: "
@@ -222,9 +223,11 @@ void IndexPQ::search(
222223
for (idx_t i = 0; i < n; i++) {
223224
const float* xi = x + i * d;
224225
uint8_t* code = q_codes.get() + i * pq.code_size;
225-
for (size_t j = 0; j < static_cast<size_t>(d); j++)
226-
if (xi[j] > 0)
226+
for (size_t j = 0; j < static_cast<size_t>(d); j++) {
227+
if (xi[j] > 0) {
227228
code[j >> 3] |= 1 << (j & 7);
229+
}
230+
}
228231
}
229232
}
230233

@@ -260,8 +263,9 @@ void IndexPQ::search(
260263
}
261264

262265
// convert distances to floats
263-
for (idx_t i = 0; i < k * n; i++)
266+
for (idx_t i = 0; i < k * n; i++) {
264267
distances[i] = idistances[i];
268+
}
265269
}
266270

267271
indexPQ_stats.nq += n;
@@ -495,8 +499,9 @@ void IndexPQ::hamming_distance_histogram(
495499
for (idx_t q0 = 0; q0 < n; q0 += bs) {
496500
// printf ("dis stats: %zd/%zd\n", q0, n);
497501
size_t q1 = q0 + bs;
498-
if (q1 > static_cast<size_t>(n))
502+
if (q1 > static_cast<size_t>(n)) {
499503
q1 = n;
504+
}
500505

501506
hammings(
502507
q_codes.get() + q0 * pq.code_size,
@@ -506,13 +511,15 @@ void IndexPQ::hamming_distance_histogram(
506511
pq.code_size,
507512
distances.get());
508513

509-
for (size_t i = 0; i < nb * (q1 - q0); i++)
514+
for (size_t i = 0; i < nb * (q1 - q0); i++) {
510515
histi[distances[i]]++;
516+
}
511517
}
512518
#pragma omp critical
513519
{
514-
for (int i = 0; i <= nbits; i++)
520+
for (int i = 0; i <= nbits; i++) {
515521
hist[i] += histi[i];
522+
}
516523
}
517524
}
518525
}
@@ -572,8 +579,9 @@ struct SortedArray {
572579

573580
void init(const T* x_2) {
574581
this->x = x_2;
575-
for (int n = 0; n < N; n++)
582+
for (int n = 0; n < N; n++) {
576583
perm[n] = n;
584+
}
577585
ArgSort<T> cmp = {x_2};
578586
std::sort(perm.begin(), perm.end(), cmp);
579587
}
@@ -654,8 +662,9 @@ struct SemiSortedArray {
654662

655663
void init(const T* x_2) {
656664
this->x = x_2;
657-
for (int n = 0; n < N; n++)
665+
for (int n = 0; n < N; n++) {
658666
perm[n] = n;
667+
}
659668
k = 0;
660669
grow(initial_k);
661670
}
@@ -758,8 +767,9 @@ struct MinSumK {
758767
seen.resize((n_ids + 7) / 8);
759768
}
760769

761-
for (int m = 0; m < M; m++)
770+
for (int m = 0; m < M; m++) {
762771
ssx.push_back(SSA(N));
772+
}
763773
}
764774

765775
int64_t weight(int i) {
@@ -771,8 +781,9 @@ struct MinSumK {
771781
}
772782

773783
void mark_seen(int64_t i) {
774-
if (use_seen)
784+
if (use_seen) {
775785
seen[i >> 3] |= 1 << (i & 7);
786+
}
776787
}
777788

778789
void run(const T* x, int64_t ldx, T* sums, int64_t* terms) {
@@ -828,8 +839,9 @@ struct MinSumK {
828839
for (int m = 0; m < M; m++) {
829840
int64_t n = ii & (((int64_t)1 << nbit) - 1);
830841
ii >>= nbit;
831-
if (n + 1 >= N)
842+
if (n + 1 >= N) {
832843
continue;
844+
}
833845

834846
enqueue_follower(ti, m, n, sum);
835847
}
@@ -884,8 +896,9 @@ void MultiIndexQuantizer::train(idx_t n, const float* x) {
884896
is_trained = true;
885897
// count virtual elements in index
886898
ntotal = 1;
887-
for (size_t m = 0; m < pq.M; m++)
899+
for (size_t m = 0; m < pq.M; m++) {
888900
ntotal *= pq.ksub;
901+
}
889902
}
890903

891904
// block size used in MultiIndexQuantizer::search

0 commit comments

Comments
 (0)