From 513eabc91d52a9ebc11545ba0b21f3fd1ca99b32 Mon Sep 17 00:00:00 2001 From: Alibek Zhakubayev Date: Thu, 16 Oct 2025 20:16:59 -0700 Subject: [PATCH] Replace static constexpr with inline constexpr in header (#4613) Summary: Pull Request resolved: https://github.com/facebookresearch/faiss/pull/4613 Replace `static constexpr` with `inline constexpr` for the `hamdis_tab_ham_bytes` array in the header file to fix namespace-level static linkage issue. Using `static` at namespace level in a header file causes a separate copy of the aggregate to appear in every compilation unit that includes the header, which is inefficient and violates best practices. The `inline constexpr` specifier (C++17) provides external linkage while ensuring only one definition exists across all translation units. This change implements the TODO that was already documented in the code comments, as Faiss uses C++17 (confirmed in CMakeLists.txt). The table remains in the header file as required for SWIG compilation on Windows, but now uses the correct modern C++ approach. Reviewed By: limqiying Differential Revision: D84858704 fbshipit-source-id: 0ea502399e3bb3943474cd56b6112dabc7370ee7 --- faiss/utils/hamming_distance/common.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/faiss/utils/hamming_distance/common.h b/faiss/utils/hamming_distance/common.h index 664900e74..154e14e2c 100644 --- a/faiss/utils/hamming_distance/common.h +++ b/faiss/utils/hamming_distance/common.h @@ -30,8 +30,7 @@ inline int popcount64(uint64_t x) { // This table was moved from .cpp to .h file, because // otherwise it was causing compilation errors while trying to // compile swig modules on Windows. -// todo for C++17: switch to 'inline constexpr' -static constexpr uint8_t hamdis_tab_ham_bytes[256] = { +inline constexpr uint8_t hamdis_tab_ham_bytes[256] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4,