From a9bef21dad3035353fba47038401e596e4ea09cb Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Fri, 8 May 2026 18:51:06 +0530 Subject: [PATCH 1/7] Fix `size` API for FAISS Indexes Co-authored-by: Copilot --- c_api/IndexBinary_c_ex.cpp | 25 +++++++++++++++++++++---- c_api/IndexBinary_c_ex.h | 8 +++++--- c_api/Index_c_ex.cpp | 22 ++++++++++++++++++---- c_api/Index_c_ex.h | 9 ++++++++- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/c_api/IndexBinary_c_ex.cpp b/c_api/IndexBinary_c_ex.cpp index 2e00ee5e53..9e9707973e 100644 --- a/c_api/IndexBinary_c_ex.cpp +++ b/c_api/IndexBinary_c_ex.cpp @@ -10,6 +10,7 @@ #include "IndexBinary_c_ex.h" #include +#include #include "macros_impl.h" extern "C" { @@ -33,9 +34,25 @@ int faiss_IndexBinary_search_with_params( CATCH_AND_HANDLE } -size_t faiss_IndexBinary_size(FaissIndexBinary* index) { - auto xIndex = reinterpret_cast(index); - size_t rv = sizeof(xIndex); - return rv; +int faiss_IndexBinary_size(FaissIndexBinary* index, size_t* p_size) { + try { + const faiss::IndexBinary* idx = reinterpret_cast(index); + + // Base: raw binary codes (d / 8 bytes per vector). + size_t size = (size_t)idx->ntotal * idx->code_size; + + // IVF-specific overhead not captured by code_size: + // centroids: quantizer->ntotal * quantizer->sa_code_size() + // stored IDs: ntotal * sizeof(idx_t) (per-vector ID in each inverted list) + if (auto ivf = dynamic_cast(idx)) { + auto ivfQuantizer = ivf->quantizer; + size += (size_t)ivfQuantizer->ntotal * ivfQuantizer->sa_code_size(); + size += (size_t)ivf->ntotal * sizeof(faiss::idx_t); + } + + *p_size = size; + return 0; + } + CATCH_AND_HANDLE } } diff --git a/c_api/IndexBinary_c_ex.h b/c_api/IndexBinary_c_ex.h index d43a3c6318..134105cf58 100644 --- a/c_api/IndexBinary_c_ex.h +++ b/c_api/IndexBinary_c_ex.h @@ -38,11 +38,13 @@ int faiss_IndexBinary_search_with_params( int32_t* distances, idx_t* labels); -/** return the size of the binary index - * +/** return the size in bytes that the binary index occupies in memory. + * For IVF-based binary indices, this includes centroid and stored ID overhead. + * * @param index opaque pointer to index object + * @param p_size output size in bytes */ -size_t faiss_IndexBinary_size(FaissIndexBinary* index); +int faiss_IndexBinary_size(FaissIndexBinary* index, size_t* p_size); #ifdef __cplusplus } diff --git a/c_api/Index_c_ex.cpp b/c_api/Index_c_ex.cpp index c5c1045142..42bd0400e8 100644 --- a/c_api/Index_c_ex.cpp +++ b/c_api/Index_c_ex.cpp @@ -13,6 +13,7 @@ #include "macros_impl.h" #include #include +#include extern "C" { @@ -39,10 +40,23 @@ int faiss_Index_merge_from( CATCH_AND_HANDLE } -size_t faiss_Index_size(FaissIndex* index) { - auto xIndex = reinterpret_cast(index); - size_t rv = sizeof(xIndex); - return rv; +int faiss_Index_size(FaissIndex* index, size_t* p_size) { + try { + const faiss::Index* idx = reinterpret_cast(index); + // Base: raw vector codes (works for Flat, SQ, and all other types). + size_t size = (size_t)idx->ntotal * idx->sa_code_size(); + // IVF-specific overhead not captured by sa_code_size(): + // centroids: quantizer->ntotal * quantizer->sa_code_size() + // stored IDs: ntotal * sizeof(idx_t) (per-vector ID in each inverted list) + if (auto ivf = dynamic_cast(idx)) { + auto ivfQuantizer = ivf->quantizer; + size += (size_t)ivfQuantizer->ntotal * ivfQuantizer->sa_code_size(); + size += (size_t)ivf->ntotal * sizeof(faiss::idx_t); + } + *p_size = size; + return 0; + } + CATCH_AND_HANDLE } int faiss_Index_dist_compute( diff --git a/c_api/Index_c_ex.h b/c_api/Index_c_ex.h index f8189f0844..bf36ffe186 100644 --- a/c_api/Index_c_ex.h +++ b/c_api/Index_c_ex.h @@ -28,7 +28,14 @@ int faiss_Index_reconstruct_batch( int faiss_Index_merge_from(FaissIndex* index, FaissIndex* other, idx_t add_id); -size_t faiss_Index_size(FaissIndex* index); +/** Compute the size of the index in bytes, this includes the + * size of the raw vector codes and any additional overhead + * (e.g. centroids, stored IDs) for IVF indices. + * + * @param index opaque pointer to index object + * @param p_size pointer to size_t to store the size + */ +int faiss_Index_size(FaissIndex* index, size_t* p_size); /** Compute distances between a query vector and a set of vectors * From 6f78f730273d6f08e3aa4fe8095ca30e4868836c Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Fri, 8 May 2026 19:40:32 +0530 Subject: [PATCH 2/7] const --- c_api/IndexBinary_c_ex.cpp | 2 +- c_api/IndexBinary_c_ex.h | 2 +- c_api/Index_c_ex.cpp | 2 +- c_api/Index_c_ex.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/c_api/IndexBinary_c_ex.cpp b/c_api/IndexBinary_c_ex.cpp index 9e9707973e..ef044688e4 100644 --- a/c_api/IndexBinary_c_ex.cpp +++ b/c_api/IndexBinary_c_ex.cpp @@ -34,7 +34,7 @@ int faiss_IndexBinary_search_with_params( CATCH_AND_HANDLE } -int faiss_IndexBinary_size(FaissIndexBinary* index, size_t* p_size) { +int faiss_IndexBinary_size(const FaissIndexBinary* index, size_t* p_size) { try { const faiss::IndexBinary* idx = reinterpret_cast(index); diff --git a/c_api/IndexBinary_c_ex.h b/c_api/IndexBinary_c_ex.h index 134105cf58..ff2b01e02a 100644 --- a/c_api/IndexBinary_c_ex.h +++ b/c_api/IndexBinary_c_ex.h @@ -44,7 +44,7 @@ int faiss_IndexBinary_search_with_params( * @param index opaque pointer to index object * @param p_size output size in bytes */ -int faiss_IndexBinary_size(FaissIndexBinary* index, size_t* p_size); +int faiss_IndexBinary_size(const FaissIndexBinary* index, size_t* p_size); #ifdef __cplusplus } diff --git a/c_api/Index_c_ex.cpp b/c_api/Index_c_ex.cpp index 42bd0400e8..013147c54f 100644 --- a/c_api/Index_c_ex.cpp +++ b/c_api/Index_c_ex.cpp @@ -40,7 +40,7 @@ int faiss_Index_merge_from( CATCH_AND_HANDLE } -int faiss_Index_size(FaissIndex* index, size_t* p_size) { +int faiss_Index_size(const FaissIndex* index, size_t* p_size) { try { const faiss::Index* idx = reinterpret_cast(index); // Base: raw vector codes (works for Flat, SQ, and all other types). diff --git a/c_api/Index_c_ex.h b/c_api/Index_c_ex.h index bf36ffe186..e4e81f6691 100644 --- a/c_api/Index_c_ex.h +++ b/c_api/Index_c_ex.h @@ -35,7 +35,7 @@ int faiss_Index_merge_from(FaissIndex* index, FaissIndex* other, idx_t add_id); * @param index opaque pointer to index object * @param p_size pointer to size_t to store the size */ -int faiss_Index_size(FaissIndex* index, size_t* p_size); +int faiss_Index_size(const FaissIndex* index, size_t* p_size); /** Compute distances between a query vector and a set of vectors * From 35bce35a13870c0739c745d99b5b17873a04ea44 Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Fri, 8 May 2026 22:14:45 +0530 Subject: [PATCH 3/7] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- c_api/IndexBinary_c_ex.cpp | 1 - c_api/IndexBinary_c_ex.h | 11 ++++++++--- c_api/Index_c_ex.cpp | 1 - c_api/Index_c_ex.h | 12 ++++++++---- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/c_api/IndexBinary_c_ex.cpp b/c_api/IndexBinary_c_ex.cpp index ef044688e4..815f76aa38 100644 --- a/c_api/IndexBinary_c_ex.cpp +++ b/c_api/IndexBinary_c_ex.cpp @@ -51,7 +51,6 @@ int faiss_IndexBinary_size(const FaissIndexBinary* index, size_t* p_size) { } *p_size = size; - return 0; } CATCH_AND_HANDLE } diff --git a/c_api/IndexBinary_c_ex.h b/c_api/IndexBinary_c_ex.h index ff2b01e02a..bd71d0cef7 100644 --- a/c_api/IndexBinary_c_ex.h +++ b/c_api/IndexBinary_c_ex.h @@ -38,11 +38,16 @@ int faiss_IndexBinary_search_with_params( int32_t* distances, idx_t* labels); -/** return the size in bytes that the binary index occupies in memory. - * For IVF-based binary indices, this includes centroid and stored ID overhead. +/** return an approximate size estimate in bytes for the binary index. + * The estimate accounts for stored codes and, for IVF-based binary indices, + * includes centroid and stored ID overhead. + * + * This is not a complete in-memory footprint: it does not attempt to include + * all internal allocations such as inverted-list container overhead, + * direct_map, or quantizer internals beyond centroid storage. * * @param index opaque pointer to index object - * @param p_size output size in bytes + * @param p_size output approximate size in bytes */ int faiss_IndexBinary_size(const FaissIndexBinary* index, size_t* p_size); diff --git a/c_api/Index_c_ex.cpp b/c_api/Index_c_ex.cpp index 013147c54f..9eac0d2dbd 100644 --- a/c_api/Index_c_ex.cpp +++ b/c_api/Index_c_ex.cpp @@ -54,7 +54,6 @@ int faiss_Index_size(const FaissIndex* index, size_t* p_size) { size += (size_t)ivf->ntotal * sizeof(faiss::idx_t); } *p_size = size; - return 0; } CATCH_AND_HANDLE } diff --git a/c_api/Index_c_ex.h b/c_api/Index_c_ex.h index e4e81f6691..5bc4f86e2f 100644 --- a/c_api/Index_c_ex.h +++ b/c_api/Index_c_ex.h @@ -28,12 +28,16 @@ int faiss_Index_reconstruct_batch( int faiss_Index_merge_from(FaissIndex* index, FaissIndex* other, idx_t add_id); -/** Compute the size of the index in bytes, this includes the - * size of the raw vector codes and any additional overhead - * (e.g. centroids, stored IDs) for IVF indices. +/** Estimate the size of the index in bytes. + * + * The returned value is an approximation based on the stored vector + * codes and any additional known overhead (for example centroids and + * stored IDs for IVF indices). It does not imply an exact total memory + * footprint, and may not be available for index types that do not + * support this estimate. * * @param index opaque pointer to index object - * @param p_size pointer to size_t to store the size + * @param p_size pointer to size_t to store the estimated size */ int faiss_Index_size(const FaissIndex* index, size_t* p_size); From 947a2cf1703d7592558bcb9edd4f6c66b44dc002 Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Fri, 8 May 2026 22:20:21 +0530 Subject: [PATCH 4/7] add a guard --- c_api/Index_c_ex.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/c_api/Index_c_ex.cpp b/c_api/Index_c_ex.cpp index 9eac0d2dbd..6d299530d9 100644 --- a/c_api/Index_c_ex.cpp +++ b/c_api/Index_c_ex.cpp @@ -50,7 +50,9 @@ int faiss_Index_size(const FaissIndex* index, size_t* p_size) { // stored IDs: ntotal * sizeof(idx_t) (per-vector ID in each inverted list) if (auto ivf = dynamic_cast(idx)) { auto ivfQuantizer = ivf->quantizer; - size += (size_t)ivfQuantizer->ntotal * ivfQuantizer->sa_code_size(); + if (ivfQuantizer != nullptr) { + size += (size_t)ivfQuantizer->ntotal * ivfQuantizer->sa_code_size(); + } size += (size_t)ivf->ntotal * sizeof(faiss::idx_t); } *p_size = size; From da2d10f523fc7302af7cb56b24bd2acbde812ed9 Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Mon, 11 May 2026 23:31:51 +0530 Subject: [PATCH 5/7] add nil check --- c_api/IndexBinary_c_ex.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/c_api/IndexBinary_c_ex.cpp b/c_api/IndexBinary_c_ex.cpp index 815f76aa38..583cb325c8 100644 --- a/c_api/IndexBinary_c_ex.cpp +++ b/c_api/IndexBinary_c_ex.cpp @@ -37,19 +37,18 @@ int faiss_IndexBinary_search_with_params( int faiss_IndexBinary_size(const FaissIndexBinary* index, size_t* p_size) { try { const faiss::IndexBinary* idx = reinterpret_cast(index); - // Base: raw binary codes (d / 8 bytes per vector). size_t size = (size_t)idx->ntotal * idx->code_size; - // IVF-specific overhead not captured by code_size: // centroids: quantizer->ntotal * quantizer->sa_code_size() // stored IDs: ntotal * sizeof(idx_t) (per-vector ID in each inverted list) if (auto ivf = dynamic_cast(idx)) { auto ivfQuantizer = ivf->quantizer; - size += (size_t)ivfQuantizer->ntotal * ivfQuantizer->sa_code_size(); + if (ivfQuantizer != nullptr) { + size += (size_t)ivfQuantizer->ntotal * ivfQuantizer->sa_code_size(); + } size += (size_t)ivf->ntotal * sizeof(faiss::idx_t); } - *p_size = size; } CATCH_AND_HANDLE From a8a6cc1a243c4360d77c9e03ff31f24f9eb4b439 Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Fri, 15 May 2026 23:36:10 +0530 Subject: [PATCH 6/7] add static size --- c_api/IndexBinary_c_ex.cpp | 36 +++++++++++++++++++++++++ c_api/IndexBinary_c_ex.h | 21 ++++++++++----- c_api/Index_c_ex.cpp | 54 ++++++++++++++++++++++++++++++++++++++ c_api/Index_c_ex.h | 25 +++++++++++------- 4 files changed, 120 insertions(+), 16 deletions(-) diff --git a/c_api/IndexBinary_c_ex.cpp b/c_api/IndexBinary_c_ex.cpp index 583cb325c8..04b10183c4 100644 --- a/c_api/IndexBinary_c_ex.cpp +++ b/c_api/IndexBinary_c_ex.cpp @@ -10,6 +10,7 @@ #include "IndexBinary_c_ex.h" #include +#include #include #include "macros_impl.h" @@ -39,13 +40,17 @@ int faiss_IndexBinary_size(const FaissIndexBinary* index, size_t* p_size) { const faiss::IndexBinary* idx = reinterpret_cast(index); // Base: raw binary codes (d / 8 bytes per vector). size_t size = (size_t)idx->ntotal * idx->code_size; + // Static struct footprint + size += faiss_index_binary_static_size(idx); // IVF-specific overhead not captured by code_size: // centroids: quantizer->ntotal * quantizer->sa_code_size() // stored IDs: ntotal * sizeof(idx_t) (per-vector ID in each inverted list) + // quantizer struct footprint if (auto ivf = dynamic_cast(idx)) { auto ivfQuantizer = ivf->quantizer; if (ivfQuantizer != nullptr) { size += (size_t)ivfQuantizer->ntotal * ivfQuantizer->sa_code_size(); + size += faiss_index_binary_static_size(ivfQuantizer); } size += (size_t)ivf->ntotal * sizeof(faiss::idx_t); } @@ -53,4 +58,35 @@ int faiss_IndexBinary_size(const FaissIndexBinary* index, size_t* p_size) { } CATCH_AND_HANDLE } + +static size_t faiss_index_binary_static_size(const faiss::IndexBinary* idx) { + if (idx == nullptr) { + return 0; + } + // BFlat + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexBinaryFlat); + } + // BIVF + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexBinaryIVF); + } + // Base + return sizeof(faiss::IndexBinary); +} + +int faiss_IndexBinary_static_size(const FaissIndexBinary* index, size_t* p_size) { + try { + const faiss::IndexBinary* idx = reinterpret_cast(index); + size_t size = faiss_index_binary_static_size(idx); + // For IVF indices, include quantizer struct footprint + if (auto ivf = dynamic_cast(idx)) { + if (ivf->quantizer != nullptr) { + size += faiss_index_binary_static_size(ivf->quantizer); + } + } + *p_size = size; + } + CATCH_AND_HANDLE +} } diff --git a/c_api/IndexBinary_c_ex.h b/c_api/IndexBinary_c_ex.h index bd71d0cef7..1bf7101630 100644 --- a/c_api/IndexBinary_c_ex.h +++ b/c_api/IndexBinary_c_ex.h @@ -38,19 +38,26 @@ int faiss_IndexBinary_search_with_params( int32_t* distances, idx_t* labels); -/** return an approximate size estimate in bytes for the binary index. - * The estimate accounts for stored codes and, for IVF-based binary indices, - * includes centroid and stored ID overhead. - * - * This is not a complete in-memory footprint: it does not attempt to include - * all internal allocations such as inverted-list container overhead, - * direct_map, or quantizer internals beyond centroid storage. +/** Return an approximate size estimate in bytes for the binary index. + * + * The estimate accounts for stored codes, the base struct size, and for + * IVF-based binary indices, includes centroid and stored ID overhead. * * @param index opaque pointer to index object * @param p_size output approximate size in bytes */ int faiss_IndexBinary_size(const FaissIndexBinary* index, size_t* p_size); +/** Return the static struct size of the binary index in bytes. + * + * This returns only the base struct footprint + * without accounting for any stored data + * + * @param index opaque pointer to index object + * @param p_size output static size in bytes + */ +int faiss_IndexBinary_static_size(const FaissIndexBinary* index, size_t* p_size); + #ifdef __cplusplus } #endif diff --git a/c_api/Index_c_ex.cpp b/c_api/Index_c_ex.cpp index 6d299530d9..4fe19c2d62 100644 --- a/c_api/Index_c_ex.cpp +++ b/c_api/Index_c_ex.cpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include extern "C" { @@ -45,13 +47,17 @@ int faiss_Index_size(const FaissIndex* index, size_t* p_size) { const faiss::Index* idx = reinterpret_cast(index); // Base: raw vector codes (works for Flat, SQ, and all other types). size_t size = (size_t)idx->ntotal * idx->sa_code_size(); + // Static struct footprint + size += faiss_index_static_size(idx); // IVF-specific overhead not captured by sa_code_size(): // centroids: quantizer->ntotal * quantizer->sa_code_size() // stored IDs: ntotal * sizeof(idx_t) (per-vector ID in each inverted list) + // quantizer struct footprint if (auto ivf = dynamic_cast(idx)) { auto ivfQuantizer = ivf->quantizer; if (ivfQuantizer != nullptr) { size += (size_t)ivfQuantizer->ntotal * ivfQuantizer->sa_code_size(); + size += faiss_index_static_size(ivfQuantizer); } size += (size_t)ivf->ntotal * sizeof(faiss::idx_t); } @@ -88,4 +94,52 @@ int faiss_Index_dist_compute( } CATCH_AND_HANDLE } + +static size_t faiss_index_static_size(const faiss::Index* idx) { + if (idx == nullptr) { + return 0; + } + // Flat Index + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexFlat); + } + // SQ Index + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexScalarQuantizer); + } + // IVF,SQ Index + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexIVFScalarQuantizer); + } + // IVF,Flat Index + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexIVFFlat); + } + // IVF,RaBitQ Index + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexIVFRaBitQ); + } + // IVF Index + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexIVF); + } + // Base Index + return sizeof(faiss::Index); +} + +int faiss_Index_static_size(const FaissIndex* index, size_t* p_size) { + try { + const faiss::Index* idx = reinterpret_cast(index); + size_t size = faiss_index_static_size(idx); + // For IVF indices, include quantizer struct footprint + if (auto ivf = dynamic_cast(idx)) { + if (ivf->quantizer != nullptr) { + size += faiss_index_static_size(ivf->quantizer); + } + } + *p_size = size; + } + CATCH_AND_HANDLE +} + } diff --git a/c_api/Index_c_ex.h b/c_api/Index_c_ex.h index 5bc4f86e2f..024b320ace 100644 --- a/c_api/Index_c_ex.h +++ b/c_api/Index_c_ex.h @@ -28,19 +28,26 @@ int faiss_Index_reconstruct_batch( int faiss_Index_merge_from(FaissIndex* index, FaissIndex* other, idx_t add_id); -/** Estimate the size of the index in bytes. +/** Return an approximate size estimate in bytes for the index. + * + * The estimate accounts for stored codes, the base struct size, and for + * IVF-based indices, includes centroid and stored ID overhead. * - * The returned value is an approximation based on the stored vector - * codes and any additional known overhead (for example centroids and - * stored IDs for IVF indices). It does not imply an exact total memory - * footprint, and may not be available for index types that do not - * support this estimate. - * - * @param index opaque pointer to index object - * @param p_size pointer to size_t to store the estimated size + * @param index opaque pointer to index object + * @param p_size output approximate size in bytes */ int faiss_Index_size(const FaissIndex* index, size_t* p_size); +/** Return the static struct size of the index in bytes. + * + * This returns only the base struct footprint + * without accounting for any stored data + * + * @param index opaque pointer to index object + * @param p_size output static size in bytes + */ +int faiss_Index_static_size(const FaissIndex* index, size_t* p_size); + /** Compute distances between a query vector and a set of vectors * * @param index opaque pointer to index object From fdb23850554834f43aa80e051dadbfd898009159 Mon Sep 17 00:00:00 2001 From: Rahul Rampure Date: Fri, 15 May 2026 23:41:13 +0530 Subject: [PATCH 7/7] reorder --- c_api/IndexBinary_c_ex.cpp | 32 +++++++++---------- c_api/Index_c_ex.cpp | 64 +++++++++++++++++++------------------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/c_api/IndexBinary_c_ex.cpp b/c_api/IndexBinary_c_ex.cpp index 04b10183c4..e1c47a92e0 100644 --- a/c_api/IndexBinary_c_ex.cpp +++ b/c_api/IndexBinary_c_ex.cpp @@ -35,6 +35,22 @@ int faiss_IndexBinary_search_with_params( CATCH_AND_HANDLE } +static size_t faiss_index_binary_static_size(const faiss::IndexBinary* idx) { + if (idx == nullptr) { + return 0; + } + // BFlat + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexBinaryFlat); + } + // BIVF + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexBinaryIVF); + } + // Base + return sizeof(faiss::IndexBinary); +} + int faiss_IndexBinary_size(const FaissIndexBinary* index, size_t* p_size) { try { const faiss::IndexBinary* idx = reinterpret_cast(index); @@ -59,22 +75,6 @@ int faiss_IndexBinary_size(const FaissIndexBinary* index, size_t* p_size) { CATCH_AND_HANDLE } -static size_t faiss_index_binary_static_size(const faiss::IndexBinary* idx) { - if (idx == nullptr) { - return 0; - } - // BFlat - if (dynamic_cast(idx)) { - return sizeof(faiss::IndexBinaryFlat); - } - // BIVF - if (dynamic_cast(idx)) { - return sizeof(faiss::IndexBinaryIVF); - } - // Base - return sizeof(faiss::IndexBinary); -} - int faiss_IndexBinary_static_size(const FaissIndexBinary* index, size_t* p_size) { try { const faiss::IndexBinary* idx = reinterpret_cast(index); diff --git a/c_api/Index_c_ex.cpp b/c_api/Index_c_ex.cpp index 4fe19c2d62..d217158f94 100644 --- a/c_api/Index_c_ex.cpp +++ b/c_api/Index_c_ex.cpp @@ -42,6 +42,38 @@ int faiss_Index_merge_from( CATCH_AND_HANDLE } +static size_t faiss_index_static_size(const faiss::Index* idx) { + if (idx == nullptr) { + return 0; + } + // Flat Index + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexFlat); + } + // SQ Index + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexScalarQuantizer); + } + // IVF,SQ Index + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexIVFScalarQuantizer); + } + // IVF,Flat Index + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexIVFFlat); + } + // IVF,RaBitQ Index + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexIVFRaBitQ); + } + // IVF Index + if (dynamic_cast(idx)) { + return sizeof(faiss::IndexIVF); + } + // Base Index + return sizeof(faiss::Index); +} + int faiss_Index_size(const FaissIndex* index, size_t* p_size) { try { const faiss::Index* idx = reinterpret_cast(index); @@ -95,38 +127,6 @@ int faiss_Index_dist_compute( CATCH_AND_HANDLE } -static size_t faiss_index_static_size(const faiss::Index* idx) { - if (idx == nullptr) { - return 0; - } - // Flat Index - if (dynamic_cast(idx)) { - return sizeof(faiss::IndexFlat); - } - // SQ Index - if (dynamic_cast(idx)) { - return sizeof(faiss::IndexScalarQuantizer); - } - // IVF,SQ Index - if (dynamic_cast(idx)) { - return sizeof(faiss::IndexIVFScalarQuantizer); - } - // IVF,Flat Index - if (dynamic_cast(idx)) { - return sizeof(faiss::IndexIVFFlat); - } - // IVF,RaBitQ Index - if (dynamic_cast(idx)) { - return sizeof(faiss::IndexIVFRaBitQ); - } - // IVF Index - if (dynamic_cast(idx)) { - return sizeof(faiss::IndexIVF); - } - // Base Index - return sizeof(faiss::Index); -} - int faiss_Index_static_size(const FaissIndex* index, size_t* p_size) { try { const faiss::Index* idx = reinterpret_cast(index);