Skip to content

Commit 2ce3883

Browse files
MB-59575: Revert memcpy optimizations for flat indexes (#23)
the recent changes for optimising memcpy operations around flat indexes is giving issues when compiled with `RelWithDebInfo` (the cause is still being investigated). reverting this to stabilize the build (the perf tests don't crash with the revert). --------- Co-authored-by: Abhi Dangeti <[email protected]>
1 parent 7c3c7d1 commit 2ce3883

5 files changed

Lines changed: 6 additions & 68 deletions

File tree

faiss/IndexFlat.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ FlatCodesDistanceComputer* IndexFlat::get_FlatCodesDistanceComputer() const {
169169
}
170170

171171
void IndexFlat::reconstruct(idx_t key, float* recons) const {
172-
if (mmaped) {
173-
memcpy(recons, &(codes_ptr[key * code_size]), code_size);
174-
return;
175-
}
176172
memcpy(recons, &(codes[key * code_size]), code_size);
177173
}
178174

faiss/IndexFlat.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,9 @@ struct IndexFlat : IndexFlatCodes {
5454

5555
// get pointer to the floating point data
5656
float* get_xb() {
57-
if (mmaped) {
58-
return (float*)(codes_ptr);
59-
}
6057
return (float*)codes.data();
6158
}
6259
const float* get_xb() const {
63-
if (mmaped) {
64-
return (const float*)(codes_ptr);
65-
}
6660
return (const float*)codes.data();
6761
}
6862

faiss/IndexFlatCodes.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,9 @@ namespace faiss {
1818
IndexFlatCodes::IndexFlatCodes(size_t code_size, idx_t d, MetricType metric)
1919
: Index(d, metric), code_size(code_size) {}
2020

21-
IndexFlatCodes::IndexFlatCodes() :
22-
code_size(0),
23-
mmaped_size(0),
24-
mmaped(false),
25-
codes_ptr(nullptr) {}
26-
27-
IndexFlatCodes::~IndexFlatCodes() {
28-
// setting the pointer to nullptr so that the mmap'd region is zero counted
29-
// from faiss side and safe to be free'd/GC'd etc. on calling application layer
30-
// of faiss.
31-
if (mmaped) {
32-
codes_ptr = nullptr;
33-
}
34-
}
21+
IndexFlatCodes::IndexFlatCodes() : code_size(0) {}
22+
23+
IndexFlatCodes::~IndexFlatCodes() {}
3524

3625
void IndexFlatCodes::add(idx_t n, const float* x) {
3726
FAISS_THROW_IF_NOT(is_trained);

faiss/IndexFlatCodes.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ struct IndexFlatCodes : Index {
2424

2525
/// encoded dataset, size ntotal * code_size
2626
std::vector<uint8_t> codes;
27-
bool mmaped; // true if codes_ptr is pointing to a mmaped region
28-
size_t mmaped_size;
29-
uint8_t* codes_ptr;
3027

3128
IndexFlatCodes();
3229

faiss/impl/index_read.cpp

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -523,40 +523,6 @@ static IndexIVFPQ* read_ivfpq(IOReader* f, uint32_t h, int io_flags) {
523523

524524
int read_old_fmt_hack = 0;
525525

526-
/**
527-
* flat indexes which store the codes directly, can use this API instead to have a
528-
* pointer to the mmaped region to avoid allocation costs. works specifically with
529-
* BufIOReader as of now.
530-
**/
531-
void read_codes_mmaped(IOReader* f, IndexFlat* idxf) {
532-
idxf->mmaped = true;
533-
534-
// read the size of codes data
535-
size_t size;
536-
READANDCHECK(&size, 1);
537-
FAISS_THROW_IF_NOT(size >= 0 && size < (uint64_t{1} << 40));
538-
size *= 4;
539-
540-
// size == ntotal * code_size == ntotal * d * sizeof(float) for IndexFlat
541-
// NOTE: the code_size value can change for indexes with encodings like
542-
// SQ, PQ although the size value will still be equal to ntotal * code_size
543-
// bytes which is accessible via codes_ptr.
544-
FAISS_THROW_IF_NOT(size == idxf->ntotal * idxf->code_size);
545-
idxf->mmaped_size = size;
546-
547-
// BufIOReader is the reader which has a direct pointer to the mmaped
548-
// byte array, so we can directly set the codes_ptr to the mmaped region
549-
BufIOReader* reader = dynamic_cast<BufIOReader*>(f);
550-
FAISS_THROW_IF_NOT_MSG(reader, "reading over mmap'd region is supported only with BufIOReader");
551-
FAISS_THROW_IF_NOT_MSG(reader->buf, "reader buffer is null");
552-
553-
idxf->codes_ptr = const_cast<uint8_t*>(reader->buf);
554-
// seek to the point where the codes section begins
555-
idxf->codes_ptr += reader->rp;
556-
// update read pointer appropriately
557-
reader->rp += size;
558-
}
559-
560526
Index* read_index(IOReader* f, int io_flags) {
561527
Index* idx = nullptr;
562528
uint32_t h;
@@ -573,13 +539,9 @@ Index* read_index(IOReader* f, int io_flags) {
573539
read_index_header(idxf, f);
574540
idxf->code_size = idxf->d * sizeof(float);
575541

576-
if (io_flags & IO_FLAG_READ_MMAP) {
577-
read_codes_mmaped(f, idxf);
578-
} else {
579-
READXBVECTOR(idxf->codes);
580-
FAISS_THROW_IF_NOT(
581-
idxf->codes.size() == idxf->ntotal * idxf->code_size);
582-
}
542+
READXBVECTOR(idxf->codes);
543+
FAISS_THROW_IF_NOT(
544+
idxf->codes.size() == idxf->ntotal * idxf->code_size);
583545
// leak!
584546
idx = idxf;
585547
} else if (h == fourcc("IxHE") || h == fourcc("IxHe")) {

0 commit comments

Comments
 (0)