From 823b5e68240c7cad58a138e6e717f6276b51713c Mon Sep 17 00:00:00 2001 From: luoxiaojian Date: Fri, 5 Jun 2026 14:51:37 +0800 Subject: [PATCH] fix: resolve compiler warnings and enable -Werror across all CI platforms (#460) --- .github/workflows/03-macos-linux-build.yml | 1 + .github/workflows/04-android-build.yml | 1 + .github/workflows/05-windows-build.yml | 1 + .github/workflows/06-ios-build.yml | 1 + .github/workflows/07-linux-riscv-build.yml | 1 + cmake/bazel.cmake | 30 ++++++++++++--- cmake/option.cmake | 3 ++ src/ailego/utility/float_helper.cc | 10 +++-- src/binding/python/model/python_doc.cc | 8 ++-- .../algorithm/diskann/diskann_file_reader.h | 3 +- src/core/algorithm/diskann/diskann_indexer.cc | 37 ++++--------------- .../algorithm/diskann/diskann_streamer.cc | 2 +- src/core/algorithm/flat/flat_streamer.cc | 2 +- src/core/algorithm/flat/flat_utility.h | 2 +- src/core/algorithm/hnsw/hnsw_chunk.cc | 2 +- src/core/algorithm/hnsw/hnsw_chunk.h | 4 +- src/core/algorithm/hnsw/hnsw_context.cc | 2 +- src/core/algorithm/hnsw/hnsw_entity.h | 6 +-- .../algorithm/hnsw/hnsw_streamer_entity.cc | 2 +- .../algorithm/hnsw/hnsw_streamer_entity.h | 28 +++++++------- .../hnsw_rabitq/hnsw_rabitq_chunk.cc | 2 +- .../algorithm/hnsw_rabitq/hnsw_rabitq_chunk.h | 4 +- .../hnsw_rabitq/hnsw_rabitq_context.cc | 2 +- .../hnsw_rabitq/hnsw_rabitq_dist_calculator.h | 3 +- .../hnsw_rabitq/hnsw_rabitq_entity.cc | 24 ++++++------ .../hnsw_rabitq/hnsw_rabitq_entity.h | 6 +-- .../hnsw_rabitq/hnsw_rabitq_query_algorithm.h | 2 +- .../hnsw_rabitq_streamer_entity.cc | 6 +-- .../hnsw_rabitq/hnsw_rabitq_streamer_entity.h | 15 ++++---- .../algorithm/hnsw_rabitq/rabitq_reformer.cc | 2 +- .../algorithm/hnsw_rabitq/rabitq_reformer.h | 2 +- src/core/algorithm/hnsw_rabitq/rabitq_utils.h | 2 +- .../hnsw_sparse/hnsw_sparse_chunk.cc | 2 +- .../algorithm/hnsw_sparse/hnsw_sparse_chunk.h | 4 +- .../hnsw_sparse/hnsw_sparse_context.cc | 2 +- .../hnsw_sparse/hnsw_sparse_entity.h | 6 +-- .../hnsw_sparse_streamer_entity.cc | 2 +- .../hnsw_sparse/hnsw_sparse_streamer_entity.h | 15 ++++---- src/core/algorithm/vamana/vamana_entity.h | 9 +++-- src/core/plugin/diskann_plugin.cc | 6 ++- src/core/utility/buffer_storage.cc | 23 ++++++------ src/db/common/rocksdb_context.cc | 2 +- src/db/index/common/doc.cc | 7 +++- src/db/index/common/schema.cc | 8 ++-- src/db/index/segment/segment_helper.cc | 1 + src/db/sqlengine/planner/ops/contain_op.cc | 2 +- .../zvec/ailego/utility/string_helper_impl.h | 17 +++++---- .../zvec/core/framework/index_holder.h | 8 ++-- .../core/framework/index_segment_storage.h | 2 +- tools/core/local_builder_original.cc | 2 +- tools/core/txt2vecs.cc | 4 +- tools/core/vecs_common.h | 8 +++- tools/core/vecs_reader.h | 10 ++--- 53 files changed, 190 insertions(+), 166 deletions(-) diff --git a/.github/workflows/03-macos-linux-build.yml b/.github/workflows/03-macos-linux-build.yml index e8332f6..f9fdd8f 100644 --- a/.github/workflows/03-macos-linux-build.yml +++ b/.github/workflows/03-macos-linux-build.yml @@ -116,6 +116,7 @@ jobs: python -m pip install -v . \ --no-build-isolation \ --config-settings='cmake.define.BUILD_TOOLS=ON' \ + --config-settings='cmake.define.ENABLE_WERROR=ON' \ --config-settings='cmake.define.CMAKE_C_COMPILER_LAUNCHER=ccache' \ --config-settings='cmake.define.CMAKE_CXX_COMPILER_LAUNCHER=ccache' \ ${{ matrix.arch_flag }} diff --git a/.github/workflows/04-android-build.yml b/.github/workflows/04-android-build.yml index b635205..7dad365 100644 --- a/.github/workflows/04-android-build.yml +++ b/.github/workflows/04-android-build.yml @@ -121,6 +121,7 @@ jobs: -DBUILD_TOOLS=OFF \ -DENABLE_NATIVE=OFF \ -DAUTO_DETECT_ARCH=OFF \ + -DENABLE_WERROR=ON \ -DCMAKE_INSTALL_PREFIX="$BUILD_DIR/install" \ -DGLOBAL_CC_PROTOBUF_PROTOC="$GITHUB_WORKSPACE/build_host/bin/protoc" \ -DCMAKE_C_COMPILER_LAUNCHER=ccache \ diff --git a/.github/workflows/05-windows-build.yml b/.github/workflows/05-windows-build.yml index 4eab8b8..ef274ba 100644 --- a/.github/workflows/05-windows-build.yml +++ b/.github/workflows/05-windows-build.yml @@ -83,6 +83,7 @@ jobs: python -m pip install -v . ` --no-build-isolation ` --config-settings='cmake.define.BUILD_TOOLS=ON' ` + --config-settings='cmake.define.ENABLE_WERROR=ON' ` --config-settings='cmake.define.CMAKE_C_COMPILER_LAUNCHER=sccache' ` --config-settings='cmake.define.CMAKE_CXX_COMPILER_LAUNCHER=sccache' shell: powershell diff --git a/.github/workflows/06-ios-build.yml b/.github/workflows/06-ios-build.yml index 802716d..94d7c86 100644 --- a/.github/workflows/06-ios-build.yml +++ b/.github/workflows/06-ios-build.yml @@ -71,6 +71,7 @@ jobs: -DCMAKE_BUILD_TYPE=Release \ -DBUILD_PYTHON_BINDINGS=OFF \ -DBUILD_TOOLS=OFF \ + -DENABLE_WERROR=ON \ -DCMAKE_INSTALL_PREFIX="./install" \ -DGLOBAL_CC_PROTOBUF_PROTOC="$GITHUB_WORKSPACE/build_host/bin/protoc" \ -DIOS=ON \ diff --git a/.github/workflows/07-linux-riscv-build.yml b/.github/workflows/07-linux-riscv-build.yml index bd4fb3a..cbf71a0 100644 --- a/.github/workflows/07-linux-riscv-build.yml +++ b/.github/workflows/07-linux-riscv-build.yml @@ -221,6 +221,7 @@ jobs: $PYTHON -m pip install -v . \ --no-build-isolation \ --config-settings='cmake.define.BUILD_TOOLS="ON"' \ + --config-settings='cmake.define.ENABLE_WERROR=ON' \ --config-settings='cmake.define.CMAKE_C_COMPILER_LAUNCHER=ccache' \ --config-settings='cmake.define.CMAKE_CXX_COMPILER_LAUNCHER=ccache' shell: bash diff --git a/cmake/bazel.cmake b/cmake/bazel.cmake index 2572915..b097340 100644 --- a/cmake/bazel.cmake +++ b/cmake/bazel.cmake @@ -396,16 +396,23 @@ endif() if(NOT MSVC) # Use color in diagnostics set( - _COMPILER_FLAGS + _C_FLAGS + "$<$:-fcolor-diagnostics>" + "$<$:-fcolor-diagnostics>" + "$<$:-fdiagnostics-color=always>" + ) + set( + _CXX_FLAGS "$<$:-fcolor-diagnostics;${CLANG_STDLIB_OPTION}>" "$<$:-fcolor-diagnostics>" "$<$:-fdiagnostics-color=always>" ) add_compile_options( - "$<$:${_COMPILER_FLAGS}>" - "$<$:${_COMPILER_FLAGS}>" + "$<$:${_C_FLAGS}>" + "$<$:${_CXX_FLAGS}>" ) - unset(_COMPILER_FLAGS) + unset(_C_FLAGS) + unset(_CXX_FLAGS) else() option(ZVEC_USE_STATIC_CRT "Use static CRT (/MT) instead of dynamic CRT (/MD), default=ON" ON) @@ -477,6 +484,17 @@ set( ) # C/C++ strict compile flags +if(ENABLE_WERROR) + set(BAZEL_CC_WERROR_FLAGS + "$<$:-Werror>" + "$<$:-Werror>" + "$<$:-Werror>" + "$<$:/WX>" + ) +else() + set(BAZEL_CC_WERROR_FLAGS "") +endif() + if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0) set( BAZEL_CC_STRICT_COMPILE_FLAGS @@ -484,6 +502,7 @@ if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0) "$<$:-Wall;-Wextra;-Wshadow>" "$<$:-Wall;-Wextra;-Wshadow-local;-Wno-misleading-indentation>" "$<$:/W4>" + ${BAZEL_CC_WERROR_FLAGS} ${BAZEL_CC_ASAN_COMPILE_FLAGS} ${BAZEL_CC_COVERAGE_COMPILE_FLAGS} ) @@ -494,6 +513,7 @@ else() "$<$:-Wall;-Wextra;-Wshadow>" "$<$:-Wall;-Wextra;-Wshadow;-Wno-misleading-indentation>" "$<$:/W4>" + ${BAZEL_CC_WERROR_FLAGS} ${BAZEL_CC_ASAN_COMPILE_FLAGS} ${BAZEL_CC_COVERAGE_COMPILE_FLAGS} ) @@ -503,7 +523,6 @@ endif() # C/C++ strict link flags set( BAZEL_CC_STRICT_LINK_FLAGS - "$<$:${CLANG_STDLIB_OPTION}>" ${BAZEL_CC_ASAN_COMPILE_FLAGS} ${BAZEL_CC_COVERAGE_COMPILE_FLAGS} ) @@ -522,7 +541,6 @@ set( # C/C++ unstrict link flags set( BAZEL_CC_UNSTRICT_LINK_FLAGS - "$<$:${CLANG_STDLIB_OPTION}>" ${BAZEL_CC_ASAN_COMPILE_FLAGS} ${BAZEL_CC_COVERAGE_COMPILE_FLAGS} ) diff --git a/cmake/option.cmake b/cmake/option.cmake index ee97b57..fe10857 100644 --- a/cmake/option.cmake +++ b/cmake/option.cmake @@ -33,6 +33,9 @@ option(ENABLE_ARMV8.6A "Enable ARMv8.6-a architecture" OFF) ## OpenMP option option(ENABLE_OPENMP "Enable OpenMP support" OFF) +## Treat warnings as errors (for CI) +option(ENABLE_WERROR "Treat compiler warnings as errors in strict targets" OFF) + set(ARCH_OPTIONS ENABLE_NEHALEM ENABLE_SANDYBRIDGE ENABLE_HASWELL ENABLE_BROADWELL ENABLE_SKYLAKE ENABLE_SKYLAKE_AVX512 ENABLE_ICELAKE ENABLE_SAPPHIRERAPIDS ENABLE_EMERALDRAPIDS ENABLE_GRANITERAPIDS diff --git a/src/ailego/utility/float_helper.cc b/src/ailego/utility/float_helper.cc index 2d77f53..bc07eec 100644 --- a/src/ailego/utility/float_helper.cc +++ b/src/ailego/utility/float_helper.cc @@ -24,14 +24,16 @@ #if defined(__aarch64__) static inline float float32(uint16_t val) { - __fp16 *p = reinterpret_cast<__fp16 *>(&val); - return *p; + __fp16 f; + memcpy(&f, &val, sizeof(val)); + return static_cast(f); } static inline uint16_t float16(float val) { __fp16 f = static_cast<__fp16>(val); - uint16_t *fp = reinterpret_cast(&f); - return *fp; + uint16_t result; + memcpy(&result, &f, sizeof(result)); + return result; } static inline void convert_fp16_to_fp32(const uint16_t *arr, size_t size, diff --git a/src/binding/python/model/python_doc.cc b/src/binding/python/model/python_doc.cc index f54d310..6803197 100644 --- a/src/binding/python/model/python_doc.cc +++ b/src/binding/python/model/python_doc.cc @@ -285,7 +285,7 @@ void ZVecPyDoc::bind_doc(py::module_ &m) { d[py::int_(indices[i])] = py::float_(static_cast(values[i])); } - return std::move(d); + return d; } case DataType::SPARSE_VECTOR_FP32: { auto vector = @@ -297,7 +297,7 @@ void ZVecPyDoc::bind_doc(py::module_ &m) { for (size_t i = 0; i < indices.size(); ++i) { d[py::int_(indices[i])] = py::float_(values[i]); } - return std::move(d); + return d; } default: throw py::type_error("Unsupported type for field: " + field); @@ -414,7 +414,7 @@ void ZVecPyDoc::bind_doc(py::module_ &m) { d[py::int_(indices[i])] = py::float_(static_cast(values[i])); } - return std::move(d); + return d; } case DataType::SPARSE_VECTOR_FP32: { auto vector = self.get< @@ -426,7 +426,7 @@ void ZVecPyDoc::bind_doc(py::module_ &m) { for (size_t i = 0; i < indices.size(); ++i) { d[py::int_(indices[i])] = py::float_(values[i]); } - return std::move(d); + return d; } default: throw py::type_error("Unsupported type for field: " + vec); diff --git a/src/core/algorithm/diskann/diskann_file_reader.h b/src/core/algorithm/diskann/diskann_file_reader.h index 8099946..432247e 100644 --- a/src/core/algorithm/diskann/diskann_file_reader.h +++ b/src/core/algorithm/diskann/diskann_file_reader.h @@ -62,7 +62,7 @@ class AlignedFileReader { public: virtual IOContext &get_ctx() = 0; - virtual ~AlignedFileReader() {}; + virtual ~AlignedFileReader() {} virtual void register_thread() = 0; virtual void deregister_thread() = 0; @@ -77,7 +77,6 @@ class AlignedFileReader { class LinuxAlignedFileReader : public AlignedFileReader { private: - uint64_t file_sz; int file_desc; IOContext bad_ctx = (IOContext)-1; diff --git a/src/core/algorithm/diskann/diskann_indexer.cc b/src/core/algorithm/diskann/diskann_indexer.cc index 4dd9188..32e7ff6 100644 --- a/src/core/algorithm/diskann/diskann_indexer.cc +++ b/src/core/algorithm/diskann/diskann_indexer.cc @@ -402,8 +402,6 @@ int DiskAnnIndexer::linear_search(DiskAnnContext *ctx) { ailego::ElapsedTime query_timer; ailego::ElapsedTime cpu_timer; - uint32_t num_ios = 0; - std::vector frontier; frontier.reserve(2 * beam_width_); @@ -462,7 +460,6 @@ int DiskAnnIndexer::linear_search(DiskAnnContext *ctx) { stats.disk_page_reads++; stats.io_num++; - num_ios++; } io_timer.reset(); @@ -540,8 +537,6 @@ int DiskAnnIndexer::keys_search(const std::vector &keys, ailego::ElapsedTime io_timer; ailego::ElapsedTime cpu_timer; - uint32_t num_ios = 0; - std::vector frontier; frontier.reserve(2 * beam_width_); @@ -602,7 +597,6 @@ int DiskAnnIndexer::keys_search(const std::vector &keys, stats.disk_page_reads++; stats.io_num++; - num_ios++; } io_timer.reset(); @@ -675,8 +669,6 @@ int DiskAnnIndexer::get_vector(diskann_id_t id, IndexContext::Pointer &context, ailego::ElapsedTime io_timer; ailego::ElapsedTime cpu_timer; - uint32_t num_ios = 0; - std::vector frontier; frontier.reserve(2 * beam_width_); @@ -714,7 +706,6 @@ int DiskAnnIndexer::get_vector(diskann_id_t id, IndexContext::Pointer &context, stats.disk_page_reads++; stats.io_num++; - num_ios++; io_timer.reset(); @@ -798,8 +789,6 @@ int DiskAnnIndexer::cached_beam_search(DiskAnnContext *ctx) { candidates.insert(Neighbor(best_medoid, dist)); visit_filter.set_visited(best_medoid); - uint32_t cmps = 0; - uint32_t hops = 0; uint32_t num_ios = 0; std::vector frontier; @@ -896,10 +885,10 @@ int DiskAnnIndexer::cached_beam_search(DiskAnnContext *ctx) { cpu_timer.reset(); - float distances[neighbor_num]; + std::vector distances(neighbor_num); pq_table_->compute_dists(neighbor_num, node_neighbors, pq_chunk_num_, ctx->pq_table_dist_buffer(), - ctx->pq_coord_buffer(), distances); + ctx->pq_coord_buffer(), distances.data()); stats.dist_num += neighbor_num; stats.cpu_us += cpu_timer.micro_seconds(); @@ -907,7 +896,6 @@ int DiskAnnIndexer::cached_beam_search(DiskAnnContext *ctx) { for (uint64_t m = 0; m < neighbor_num; ++m) { diskann_id_t id = node_neighbors[m]; visit_filter.set_visited(id); - cmps++; Neighbor nn(id, distances[m]); candidates.insert(nn); @@ -937,10 +925,10 @@ int DiskAnnIndexer::cached_beam_search(DiskAnnContext *ctx) { reinterpret_cast(node_buf + 1); cpu_timer.reset(); - float distances[neighbor_num]; + std::vector distances(neighbor_num); pq_table_->compute_dists(neighbor_num, node_neighbors, pq_chunk_num_, ctx->pq_table_dist_buffer(), - ctx->pq_coord_buffer(), distances); + ctx->pq_coord_buffer(), distances.data()); stats.dist_num += neighbor_num; stats.cpu_us += cpu_timer.micro_seconds(); @@ -949,7 +937,6 @@ int DiskAnnIndexer::cached_beam_search(DiskAnnContext *ctx) { for (uint64_t m = 0; m < neighbor_num; ++m) { diskann_id_t id = node_neighbors[m]; visit_filter.set_visited(id); - cmps++; stats.dist_num++; Neighbor nn(id, distances[m]); @@ -958,8 +945,6 @@ int DiskAnnIndexer::cached_beam_search(DiskAnnContext *ctx) { stats.cpu_us += cpu_timer.micro_seconds(); } - - hops++; } stats.total_us += query_timer.micro_seconds(); @@ -1034,8 +1019,6 @@ int DiskAnnIndexer::cached_beam_search_by_group(DiskAnnContext *ctx) { pq_table_->preprocess_pq_dist_table(ctx->query_rotated(), ctx->pq_table_dist_buffer()); - uint32_t cmps = 0; - uint32_t hops = 0; uint32_t num_ios = 0; std::vector frontier; @@ -1138,10 +1121,10 @@ int DiskAnnIndexer::cached_beam_search_by_group(DiskAnnContext *ctx) { cpu_timer.reset(); - float distances[neighbor_num]; + std::vector distances(neighbor_num); pq_table_->compute_dists(neighbor_num, node_neighbors, pq_chunk_num_, ctx->pq_table_dist_buffer(), - ctx->pq_coord_buffer(), distances); + ctx->pq_coord_buffer(), distances.data()); stats.dist_num += neighbor_num; stats.cpu_us += cpu_timer.micro_seconds(); @@ -1149,7 +1132,6 @@ int DiskAnnIndexer::cached_beam_search_by_group(DiskAnnContext *ctx) { for (uint64_t m = 0; m < neighbor_num; ++m) { diskann_id_t id = node_neighbors[m]; visit_filter.set_visited(id); - cmps++; Neighbor nn(id, distances[m]); candidates.insert(nn); @@ -1189,12 +1171,12 @@ int DiskAnnIndexer::cached_beam_search_by_group(DiskAnnContext *ctx) { cpu_timer.reset(); - float distances[neighbor_num]; + std::vector distances(neighbor_num); diskann_id_t *node_neighbors = reinterpret_cast(node_buf + 1); pq_table_->compute_dists(neighbor_num, node_neighbors, pq_chunk_num_, ctx->pq_table_dist_buffer(), - ctx->pq_coord_buffer(), distances); + ctx->pq_coord_buffer(), distances.data()); stats.dist_num += neighbor_num; stats.cpu_us += cpu_timer.micro_seconds(); @@ -1203,7 +1185,6 @@ int DiskAnnIndexer::cached_beam_search_by_group(DiskAnnContext *ctx) { for (uint64_t m = 0; m < neighbor_num; ++m) { diskann_id_t id = node_neighbors[m]; visit_filter.set_visited(id); - cmps++; stats.dist_num++; Neighbor nn(id, distances[m]); @@ -1212,8 +1193,6 @@ int DiskAnnIndexer::cached_beam_search_by_group(DiskAnnContext *ctx) { stats.cpu_us += cpu_timer.micro_seconds(); } - - hops++; } stats.total_us += query_timer.micro_seconds(); diff --git a/src/core/algorithm/diskann/diskann_streamer.cc b/src/core/algorithm/diskann/diskann_streamer.cc index 2f18ba1..7a978b6 100644 --- a/src/core/algorithm/diskann/diskann_streamer.cc +++ b/src/core/algorithm/diskann/diskann_streamer.cc @@ -272,7 +272,7 @@ int DiskAnnStreamer::get_vector(uint64_t key, Context::Pointer &context, return diskann_indexer_->get_vector(key, context, vector); } -const void *DiskAnnStreamer::get_vector_by_id(uint32_t id) const { +const void *DiskAnnStreamer::get_vector_by_id(uint32_t /*id*/) const { // DiskAnn vectors are stored on disk in sector format; // a const void* access requires sector I/O via create_context // Return nullptr to indicate this path is not supported. diff --git a/src/core/algorithm/flat/flat_streamer.cc b/src/core/algorithm/flat/flat_streamer.cc index 5e61716..9f5b9ec 100644 --- a/src/core/algorithm/flat/flat_streamer.cc +++ b/src/core/algorithm/flat/flat_streamer.cc @@ -181,7 +181,7 @@ int FlatStreamer::flush(uint64_t checkpoint) { template int FlatStreamer::dump(const IndexDumper::Pointer &dumper) { std::string searcher_name = "FlatSearcher"; - if (BATCH_SIZE == 16) { + if constexpr (BATCH_SIZE == 16) { searcher_name = "FlatSearcher16"; } meta_.set_searcher(searcher_name, 0U, ailego::Params()); diff --git a/src/core/algorithm/flat/flat_utility.h b/src/core/algorithm/flat/flat_utility.h index a8e05bc..9912c58 100644 --- a/src/core/algorithm/flat/flat_utility.h +++ b/src/core/algorithm/flat/flat_utility.h @@ -105,7 +105,7 @@ static inline void Transpose(size_t align_size, const void *src, size_t m, template void TransposeQueries(const void *query, const IndexQueryMeta &qmeta, size_t query_count, std::string *out) { - if (K <= 1) { + if constexpr (K <= 1) { ailego_assert(query_count == 1); (void)query_count; out->append(reinterpret_cast(query) + out->size(), diff --git a/src/core/algorithm/hnsw/hnsw_chunk.cc b/src/core/algorithm/hnsw/hnsw_chunk.cc index 7ebb8c0..2d1ea24 100644 --- a/src/core/algorithm/hnsw/hnsw_chunk.cc +++ b/src/core/algorithm/hnsw/hnsw_chunk.cc @@ -70,7 +70,7 @@ int ChunkBroker::load_storage(uint32_t &chunk_size) { chunk_meta_segment_->data_size()); return IndexError_InvalidFormat; } - std::memcpy(&chunk_meta_, data_block.data(), size); + std::memcpy(static_cast(&chunk_meta_), data_block.data(), size); if (chunk_meta_.chunk_size != chunk_size) { LOG_WARN( "chunk_size mismatch: config=%u, index=%zu. " diff --git a/src/core/algorithm/hnsw/hnsw_chunk.h b/src/core/algorithm/hnsw/hnsw_chunk.h index 5d96d37..034a2dc 100644 --- a/src/core/algorithm/hnsw/hnsw_chunk.h +++ b/src/core/algorithm/hnsw/hnsw_chunk.h @@ -108,10 +108,10 @@ class ChunkBroker { struct HnswChunkMeta { HnswChunkMeta(void) { - memset(this, 0, sizeof(HnswChunkMeta)); + memset(static_cast(this), 0, sizeof(HnswChunkMeta)); } void clear() { - memset(this, 0, sizeof(HnswChunkMeta)); + memset(static_cast(this), 0, sizeof(HnswChunkMeta)); } uint64_t chunk_cnts[CHUNK_TYPE_MAX]; diff --git a/src/core/algorithm/hnsw/hnsw_context.cc b/src/core/algorithm/hnsw/hnsw_context.cc index 5ad6c93..dd25e53 100644 --- a/src/core/algorithm/hnsw/hnsw_context.cc +++ b/src/core/algorithm/hnsw/hnsw_context.cc @@ -205,7 +205,7 @@ int HnswContext::update_context(ContextType type, const IndexMeta &meta, uint32_t magic_num) { uint32_t doc_cnt; - if (ailego_unlikely(type != type_)) { + if (ailego_unlikely(static_cast(type) != type_)) { LOG_ERROR( "HnswContext doesn't support shared by different type, " "src=%u dst=%u", diff --git a/src/core/algorithm/hnsw/hnsw_entity.h b/src/core/algorithm/hnsw/hnsw_entity.h index bae57ec..197d8e9 100644 --- a/src/core/algorithm/hnsw/hnsw_entity.h +++ b/src/core/algorithm/hnsw/hnsw_entity.h @@ -77,11 +77,11 @@ struct HNSWHeader { } HNSWHeader(const HNSWHeader &header) { - memcpy(this, &header, sizeof(header)); + memcpy(static_cast(this), &header, sizeof(header)); } HNSWHeader &operator=(const HNSWHeader &header) { - memcpy(this, &header, sizeof(header)); + memcpy(static_cast(this), &header, sizeof(header)); return *this; } @@ -94,7 +94,7 @@ struct HNSWHeader { //! Clear all fields to init value void inline clear() { - memset(this, 0, sizeof(HNSWHeader)); + memset(static_cast(this), 0, sizeof(HNSWHeader)); hnsw.entry_point = kInvalidNodeId; graph.size = sizeof(GraphHeader); hnsw.size = sizeof(HnswHeader); diff --git a/src/core/algorithm/hnsw/hnsw_streamer_entity.cc b/src/core/algorithm/hnsw/hnsw_streamer_entity.cc index a8ada19..b3c2903 100644 --- a/src/core/algorithm/hnsw/hnsw_streamer_entity.cc +++ b/src/core/algorithm/hnsw/hnsw_streamer_entity.cc @@ -505,7 +505,7 @@ int HnswStreamerEntity::dump(const IndexDumper::Pointer &dumper) { return 0U; }; auto meta = reinterpret_cast(&it->second); - return meta->level; + return meta->bits.level; }; auto ret = dump_segments(dumper, keys.data(), get_level); if (ailego_unlikely(ret < 0)) { diff --git a/src/core/algorithm/hnsw/hnsw_streamer_entity.h b/src/core/algorithm/hnsw/hnsw_streamer_entity.h index f97f122..4e75661 100644 --- a/src/core/algorithm/hnsw/hnsw_streamer_entity.h +++ b/src/core/algorithm/hnsw/hnsw_streamer_entity.h @@ -225,7 +225,7 @@ class HnswStreamerEntity : public HnswEntity { uint32_t index : 28; // index is composite type: chunk idx, and the // N th neighbors in chunk, they two composite // the 28 bits location - }; + } bits; uint32_t data; }; @@ -259,6 +259,7 @@ class HnswStreamerEntity : public HnswEntity { std::shared_ptr> node_bases, std::shared_ptr> upper_bases) : stats_(stats), + upper_neighbor_rw_mutex_(upper_neighbor_rw_mutex), chunk_size_(chunk_size), node_index_mask_bits_(node_index_mask_bits), node_cnt_per_chunk_(1UL << node_index_mask_bits_), @@ -268,7 +269,6 @@ class HnswStreamerEntity : public HnswEntity { filter_same_key_(filter_same_key), get_vector_enabled_(get_vector_enabled), use_key_info_map_(use_key_info_map), - upper_neighbor_rw_mutex_(upper_neighbor_rw_mutex), upper_neighbor_index_(upper_neighbor_index), keys_map_lock_(keys_map_lock), keys_map_(keys_map), @@ -331,9 +331,10 @@ class HnswStreamerEntity : public HnswEntity { ailego_assert_abort(it != upper_neighbor_index_->end(), "Get upper neighbor header failed"); auto meta = reinterpret_cast(&it->second); - uint32_t chunk_idx = (meta->index) >> upper_neighbor_mask_bits_; - uint32_t offset = (((meta->index) & upper_neighbor_mask_) + level - 1) * - upper_neighbor_size_; + uint32_t chunk_idx = (meta->bits.index) >> upper_neighbor_mask_bits_; + uint32_t offset = + (((meta->bits.index) & upper_neighbor_mask_) + level - 1) * + upper_neighbor_size_; sync_chunks(ChunkBroker::CHUNK_TYPE_UPPER_NEIGHBOR, chunk_idx, &upper_neighbor_chunks_); ailego_assert_abort(chunk_idx < upper_neighbor_chunks_.size(), @@ -413,9 +414,9 @@ class HnswStreamerEntity : public HnswEntity { ailego_assert_with(chunk_index < (1U << (28 - upper_neighbor_mask_bits_)), "invalid chunk index"); UpperNeighborIndexMeta meta; - meta.level = level; - meta.index = (chunk_index << upper_neighbor_mask_bits_) | - (chunk_offset / upper_neighbor_size_); + meta.bits.level = level; + meta.bits.index = (chunk_index << upper_neighbor_mask_bits_) | + (chunk_offset / upper_neighbor_size_); size_t zero_start = chunk_offset; chunk_offset += upper_neighbor_size_ * level; @@ -821,9 +822,10 @@ class HnswMmapStreamerEntity : public HnswStreamerEntity { ailego_assert_abort(it != upper_neighbor_index_->end(), "Get upper neighbor header failed"); auto meta = reinterpret_cast(&it->second); - uint32_t chunk_idx = (meta->index) >> upper_neighbor_mask_bits_; - uint32_t offset = (((meta->index) & upper_neighbor_mask_) + level - 1) * - upper_neighbor_size_; + uint32_t chunk_idx = (meta->bits.index) >> upper_neighbor_mask_bits_; + uint32_t offset = + (((meta->bits.index) & upper_neighbor_mask_) + level - 1) * + upper_neighbor_size_; const char *base = get_upper_neighbor_chunk_base(chunk_idx); MmapMemoryBlock block(const_cast(base + offset)); return TypedNeighbors(std::move(block)); @@ -985,8 +987,8 @@ class HnswContiguousStreamerEntity : public HnswMmapStreamerEntity { ailego_assert_abort(it != upper_neighbor_index_->end(), "Get upper neighbor header failed"); auto meta = reinterpret_cast(&it->second); - uint32_t chunk_idx = (meta->index) >> upper_neighbor_mask_bits_; - uint32_t local_idx = (meta->index) & upper_neighbor_mask_; + uint32_t chunk_idx = (meta->bits.index) >> upper_neighbor_mask_bits_; + uint32_t local_idx = (meta->bits.index) & upper_neighbor_mask_; size_t global_offset = upper_chunk_offsets_[chunk_idx] + static_cast(local_idx + level - 1) * upper_neighbor_size_; diff --git a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_chunk.cc b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_chunk.cc index 7be23a5..8833a61 100644 --- a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_chunk.cc +++ b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_chunk.cc @@ -70,7 +70,7 @@ int HnswRabitqChunkBroker::load_storage(size_t chunk_size) { chunk_meta_segment_->data_size()); return IndexError_InvalidFormat; } - std::memcpy(&chunk_meta_, data_block.data(), size); + std::memcpy(static_cast(&chunk_meta_), data_block.data(), size); if (chunk_meta_.chunk_size != chunk_size) { LOG_ERROR( "Params hnsw chunk size=%zu mismatch from previous %zu " diff --git a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_chunk.h b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_chunk.h index 823bf5c..f0bc339 100644 --- a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_chunk.h +++ b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_chunk.h @@ -95,10 +95,10 @@ class HnswRabitqChunkBroker { struct HnswChunkMeta { HnswChunkMeta(void) { - memset(this, 0, sizeof(HnswChunkMeta)); + memset(static_cast(this), 0, sizeof(HnswChunkMeta)); } void clear() { - memset(this, 0, sizeof(HnswChunkMeta)); + memset(static_cast(this), 0, sizeof(HnswChunkMeta)); } uint64_t chunk_cnts[CHUNK_TYPE_MAX]; diff --git a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_context.cc b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_context.cc index 9866598..e723177 100644 --- a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_context.cc +++ b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_context.cc @@ -206,7 +206,7 @@ int HnswRabitqContext::update_context(ContextType type, const IndexMeta &meta, uint32_t magic_num) { uint32_t doc_cnt; - if (ailego_unlikely(type != type_)) { + if (ailego_unlikely(static_cast(type) != type_)) { LOG_ERROR( "HnswRabitqContext doesn't support shared by different type, " "src=%u dst=%u", diff --git a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_dist_calculator.h b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_dist_calculator.h index 5ccda8a..faef466 100644 --- a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_dist_calculator.h +++ b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_dist_calculator.h @@ -228,8 +228,7 @@ class HnswRabitqAddDistCalculator { const void *query_; uint32_t dim_; - uint32_t compare_cnt_; // record distance compute times - uint32_t compare_cnt_batch_; // record batch distance compute time + uint32_t compare_cnt_; // record distance compute times bool error_{false}; // get raw vector diff --git a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_entity.cc b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_entity.cc index bbaf9ca..d3c6a49 100644 --- a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_entity.cc +++ b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_entity.cc @@ -190,8 +190,7 @@ int64_t HnswRabitqEntity::dump_vectors( size_t padding_size = AlignSize(vector_dump_size) - vector_dump_size; - char padding[padding_size]; - memset(padding, 0, sizeof(padding)); + std::vector padding(padding_size, 0); const void *data = nullptr; uint32_t crc = 0U; size_t vecs_size = 0UL; @@ -216,12 +215,12 @@ int64_t HnswRabitqEntity::dump_vectors( continue; } - len = dumper->write(padding, padding_size); + len = dumper->write(padding.data(), padding_size); if (len != padding_size) { LOG_ERROR("Dump vectors failed, write=%zu expect=%zu", len, padding_size); return IndexError_WriteData; } - crc = ailego::Crc32c::Hash(padding, padding_size, crc); + crc = ailego::Crc32c::Hash(padding.data(), padding_size, crc); vecs_size += padding_size; } @@ -242,7 +241,7 @@ int64_t HnswRabitqEntity::dump_graph_neighbors( graph_meta.reserve(doc_cnt()); size_t offset = 0; uint32_t crc = 0; - node_id_t mapping[l0_neighbor_cnt()]; + std::vector mapping(l0_neighbor_cnt()); uint32_t min_neighbor_count = 10000; uint32_t max_neighbor_count = 0; @@ -271,7 +270,7 @@ int64_t HnswRabitqEntity::dump_graph_neighbors( for (node_id_t i = 0; i < neighbors.size(); ++i) { mapping[i] = neighbor_mapping[neighbors[i]]; } - data = mapping; + data = mapping.data(); } if (dumper->write(data, size) != size) { LOG_ERROR("Dump graph neighbor id=%zu failed, size %zu", @@ -322,7 +321,7 @@ int64_t HnswRabitqEntity::dump_upper_neighbors( hnsw_meta.reserve(doc_cnt()); size_t offset = 0; uint32_t crc = 0; - node_id_t buffer[upper_neighbor_cnt() + 1]; + std::vector buffer(upper_neighbor_cnt() + 1); for (node_id_t id = 0; id < doc_cnt(); ++id) { node_id_t new_id = reorder_mapping.empty() ? id : reorder_mapping[id]; auto level = get_level(new_id); @@ -337,7 +336,8 @@ int64_t HnswRabitqEntity::dump_upper_neighbors( ailego_assert_with(!!neighbors.data, "invalid neighbors"); ailego_assert_with(neighbors.size() <= neighbor_cnt(cur_level), "invalid neighbors"); - memset(buffer, 0, sizeof(buffer)); + size_t buffer_bytes = buffer.size() * sizeof(node_id_t); + memset(buffer.data(), 0, buffer_bytes); buffer[0] = neighbors.size(); if (neighbor_mapping.empty()) { memcpy(&buffer[1], &neighbors[0], neighbors.size() * sizeof(node_id_t)); @@ -346,13 +346,13 @@ int64_t HnswRabitqEntity::dump_upper_neighbors( buffer[i + 1] = neighbor_mapping[neighbors[i]]; } } - if (dumper->write(buffer, sizeof(buffer)) != sizeof(buffer)) { + if (dumper->write(buffer.data(), buffer_bytes) != buffer_bytes) { LOG_ERROR("Dump graph neighbor id=%zu failed, size %zu", - static_cast(id), sizeof(buffer)); + static_cast(id), buffer_bytes); return IndexError_WriteData; } - crc = ailego::Crc32c::Hash(buffer, sizeof(buffer), crc); - offset += sizeof(buffer); + crc = ailego::Crc32c::Hash(buffer.data(), buffer_bytes, crc); + offset += buffer_bytes; } } size_t padding_size = 0; diff --git a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_entity.h b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_entity.h index bb05c8f..6d78eea 100644 --- a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_entity.h +++ b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_entity.h @@ -113,11 +113,11 @@ struct HNSWHeader { } HNSWHeader(const HNSWHeader &header) { - memcpy(this, &header, sizeof(header)); + memcpy(static_cast(this), &header, sizeof(header)); } HNSWHeader &operator=(const HNSWHeader &header) { - memcpy(this, &header, sizeof(header)); + memcpy(static_cast(this), &header, sizeof(header)); return *this; } @@ -130,7 +130,7 @@ struct HNSWHeader { //! Clear all fields to init value void inline clear() { - memset(this, 0, sizeof(HNSWHeader)); + memset(static_cast(this), 0, sizeof(HNSWHeader)); hnsw.entry_point = kInvalidNodeId; graph.size = sizeof(GraphHeader); hnsw.size = sizeof(HnswHeader); diff --git a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_query_algorithm.h b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_query_algorithm.h index 713c9f6..bcf6e42 100644 --- a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_query_algorithm.h +++ b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_query_algorithm.h @@ -23,7 +23,7 @@ namespace zvec { namespace core { -class HnswRabitqQueryEntity; +struct HnswRabitqQueryEntity; //! hnsw graph algorithm implement class HnswRabitqQueryAlgorithm { diff --git a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_streamer_entity.cc b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_streamer_entity.cc index cef59c3..27643aa 100644 --- a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_streamer_entity.cc +++ b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_streamer_entity.cc @@ -81,8 +81,8 @@ int HnswRabitqStreamerEntity::cleanup() { int HnswRabitqStreamerEntity::update_neighbors( level_t level, node_id_t id, const std::vector> &neighbors) { - char buffer[neighbor_size_]; - NeighborsHeader *hd = reinterpret_cast(buffer); + std::vector buffer(neighbor_size_); + NeighborsHeader *hd = reinterpret_cast(buffer.data()); hd->neighbor_cnt = neighbors.size(); size_t i = 0; for (; i < neighbors.size(); ++i) { @@ -434,7 +434,7 @@ int HnswRabitqStreamerEntity::dump(const IndexDumper::Pointer &dumper) { return 0U; }; auto meta = reinterpret_cast(&it->second); - return meta->level; + return meta->bits.level; }; auto ret = dump_segments(dumper, keys.data(), get_level); if (ailego_unlikely(ret < 0)) { diff --git a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_streamer_entity.h b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_streamer_entity.h index 435b44d..511fbad 100644 --- a/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_streamer_entity.h +++ b/src/core/algorithm/hnsw_rabitq/hnsw_rabitq_streamer_entity.h @@ -195,7 +195,7 @@ class HnswRabitqStreamerEntity : public HnswRabitqEntity { uint32_t index : 28; // index is composite type: chunk idx, and the // N th neighbors in chunk, they two composite // the 28 bits location - }; + } bits; uint32_t data; }; @@ -294,9 +294,10 @@ class HnswRabitqStreamerEntity : public HnswRabitqEntity { ailego_assert_abort(it != upper_neighbor_index_->end(), "Get upper neighbor header failed"); auto meta = reinterpret_cast(&it->second); - uint32_t chunk_idx = (meta->index) >> upper_neighbor_mask_bits_; - uint32_t offset = (((meta->index) & upper_neighbor_mask_) + level - 1) * - upper_neighbor_size_; + uint32_t chunk_idx = (meta->bits.index) >> upper_neighbor_mask_bits_; + uint32_t offset = + (((meta->bits.index) & upper_neighbor_mask_) + level - 1) * + upper_neighbor_size_; sync_chunks(HnswRabitqChunkBroker::CHUNK_TYPE_UPPER_NEIGHBOR, chunk_idx, &upper_neighbor_chunks_); ailego_assert_abort(chunk_idx < upper_neighbor_chunks_.size(), @@ -378,9 +379,9 @@ class HnswRabitqStreamerEntity : public HnswRabitqEntity { ailego_assert_with(chunk_index < (1U << (28 - upper_neighbor_mask_bits_)), "invalid chunk index"); UpperNeighborIndexMeta meta; - meta.level = level; - meta.index = (chunk_index << upper_neighbor_mask_bits_) | - (chunk_offset / upper_neighbor_size_); + meta.bits.level = level; + meta.bits.index = (chunk_index << upper_neighbor_mask_bits_) | + (chunk_offset / upper_neighbor_size_); size_t zero_start = chunk_offset; chunk_offset += upper_neighbor_size_ * level; diff --git a/src/core/algorithm/hnsw_rabitq/rabitq_reformer.cc b/src/core/algorithm/hnsw_rabitq/rabitq_reformer.cc index d8ffa95..4a4c0a5 100644 --- a/src/core/algorithm/hnsw_rabitq/rabitq_reformer.cc +++ b/src/core/algorithm/hnsw_rabitq/rabitq_reformer.cc @@ -144,7 +144,7 @@ int RabitqReformer::load(IndexStorage::Pointer storage) { LOG_ERROR("Failed to read header"); return IndexError_InvalidFormat; } - memcpy(&header, block.data(), sizeof(header)); + memcpy(static_cast(&header), block.data(), sizeof(header)); impl_->dimension = header.dim; impl_->padded_dim = header.padded_dim; impl_->ex_bits = header.ex_bits; diff --git a/src/core/algorithm/hnsw_rabitq/rabitq_reformer.h b/src/core/algorithm/hnsw_rabitq/rabitq_reformer.h index 72bdc53..e3ee9d2 100644 --- a/src/core/algorithm/hnsw_rabitq/rabitq_reformer.h +++ b/src/core/algorithm/hnsw_rabitq/rabitq_reformer.h @@ -23,7 +23,7 @@ namespace zvec { namespace core { -class HnswRabitqQueryEntity; +struct HnswRabitqQueryEntity; /*! RaBitQ Reformer * Loads centroids and performs query transformation and vector quantization. diff --git a/src/core/algorithm/hnsw_rabitq/rabitq_utils.h b/src/core/algorithm/hnsw_rabitq/rabitq_utils.h index cf4db27..567c768 100644 --- a/src/core/algorithm/hnsw_rabitq/rabitq_utils.h +++ b/src/core/algorithm/hnsw_rabitq/rabitq_utils.h @@ -34,7 +34,7 @@ struct RabitqConverterHeader { uint32_t reserve[3]; RabitqConverterHeader() { - memset(this, 0, sizeof(RabitqConverterHeader)); + memset(static_cast(this), 0, sizeof(RabitqConverterHeader)); } }; static_assert(sizeof(RabitqConverterHeader) % 32 == 0, diff --git a/src/core/algorithm/hnsw_sparse/hnsw_sparse_chunk.cc b/src/core/algorithm/hnsw_sparse/hnsw_sparse_chunk.cc index b728bed..b3e6f49 100644 --- a/src/core/algorithm/hnsw_sparse/hnsw_sparse_chunk.cc +++ b/src/core/algorithm/hnsw_sparse/hnsw_sparse_chunk.cc @@ -71,7 +71,7 @@ int SparseChunkBroker::load_storage(size_t chunk_size) { chunk_meta_segment_->data_size()); return IndexError_InvalidFormat; } - std::memcpy(&chunk_meta_, data_block.data(), size); + std::memcpy(static_cast(&chunk_meta_), data_block.data(), size); if (chunk_meta_.chunk_size != chunk_size) { LOG_ERROR( "Params hnsw chunk size=%zu mismatch from previous %zu " diff --git a/src/core/algorithm/hnsw_sparse/hnsw_sparse_chunk.h b/src/core/algorithm/hnsw_sparse/hnsw_sparse_chunk.h index c0c3da0..88000ce 100644 --- a/src/core/algorithm/hnsw_sparse/hnsw_sparse_chunk.h +++ b/src/core/algorithm/hnsw_sparse/hnsw_sparse_chunk.h @@ -97,10 +97,10 @@ class SparseChunkBroker { struct HnswSparseChunkMeta { HnswSparseChunkMeta(void) { - memset(this, 0, sizeof(HnswSparseChunkMeta)); + memset(static_cast(this), 0, sizeof(HnswSparseChunkMeta)); } void clear() { - memset(this, 0, sizeof(HnswSparseChunkMeta)); + memset(static_cast(this), 0, sizeof(HnswSparseChunkMeta)); } uint64_t chunk_cnts[CHUNK_TYPE_MAX]; diff --git a/src/core/algorithm/hnsw_sparse/hnsw_sparse_context.cc b/src/core/algorithm/hnsw_sparse/hnsw_sparse_context.cc index 142e151..946d1e9 100644 --- a/src/core/algorithm/hnsw_sparse/hnsw_sparse_context.cc +++ b/src/core/algorithm/hnsw_sparse/hnsw_sparse_context.cc @@ -203,7 +203,7 @@ int HnswSparseContext::update_context(ContextType type, uint32_t magic_num) { uint32_t doc_cnt; - if (ailego_unlikely(type != type_)) { + if (ailego_unlikely(static_cast(type) != type_)) { LOG_ERROR( "HnswSparseContext doesn't support shared by different type, " "src=%u dst=%u", diff --git a/src/core/algorithm/hnsw_sparse/hnsw_sparse_entity.h b/src/core/algorithm/hnsw_sparse/hnsw_sparse_entity.h index 6f0c1e1..7e6f814 100644 --- a/src/core/algorithm/hnsw_sparse/hnsw_sparse_entity.h +++ b/src/core/algorithm/hnsw_sparse/hnsw_sparse_entity.h @@ -94,11 +94,11 @@ struct HNSWSparseHeader { } HNSWSparseHeader(const HNSWSparseHeader &header) { - memcpy(this, &header, sizeof(header)); + memcpy(static_cast(this), &header, sizeof(header)); } HNSWSparseHeader &operator=(const HNSWSparseHeader &header) { - memcpy(this, &header, sizeof(header)); + memcpy(static_cast(this), &header, sizeof(header)); return *this; } @@ -112,7 +112,7 @@ struct HNSWSparseHeader { //! Clear all fields to init value void inline clear() { - memset(this, 0, sizeof(HNSWSparseHeader)); + memset(static_cast(this), 0, sizeof(HNSWSparseHeader)); hnsw.entry_point = kInvalidNodeId; graph.size = sizeof(SparseGraphHeader); hnsw.size = sizeof(HnswSparseHeader); diff --git a/src/core/algorithm/hnsw_sparse/hnsw_sparse_streamer_entity.cc b/src/core/algorithm/hnsw_sparse/hnsw_sparse_streamer_entity.cc index 811347f..90d6b3a 100644 --- a/src/core/algorithm/hnsw_sparse/hnsw_sparse_streamer_entity.cc +++ b/src/core/algorithm/hnsw_sparse/hnsw_sparse_streamer_entity.cc @@ -538,7 +538,7 @@ int HnswSparseStreamerEntity::dump(const IndexDumper::Pointer &dumper) { return 0U; }; auto meta = reinterpret_cast(&it->second); - return meta->level; + return meta->bits.level; }; auto ret = dump_segments(dumper, keys.data(), get_level); if (ailego_unlikely(ret < 0)) { diff --git a/src/core/algorithm/hnsw_sparse/hnsw_sparse_streamer_entity.h b/src/core/algorithm/hnsw_sparse/hnsw_sparse_streamer_entity.h index 2a53a54..a474561 100644 --- a/src/core/algorithm/hnsw_sparse/hnsw_sparse_streamer_entity.h +++ b/src/core/algorithm/hnsw_sparse/hnsw_sparse_streamer_entity.h @@ -207,7 +207,7 @@ class HnswSparseStreamerEntity : public HnswSparseEntity { uint32_t index : 28; // index is composite type: chunk idx, and the // N th neighbors in chunk, they two composite // the 28 bits location - }; + } bits; uint32_t data; }; @@ -311,9 +311,10 @@ class HnswSparseStreamerEntity : public HnswSparseEntity { ailego_assert_abort(it != upper_neighbor_index_->end(), "Get upper neighbor header failed"); auto meta = reinterpret_cast(&it->second); - uint32_t chunk_idx = (meta->index) >> upper_neighbor_mask_bits_; - uint32_t offset = (((meta->index) & upper_neighbor_mask_) + level - 1) * - upper_neighbor_size_; + uint32_t chunk_idx = (meta->bits.index) >> upper_neighbor_mask_bits_; + uint32_t offset = + (((meta->bits.index) & upper_neighbor_mask_) + level - 1) * + upper_neighbor_size_; sync_chunks(SparseChunkBroker::CHUNK_TYPE_UPPER_NEIGHBOR, chunk_idx, &upper_neighbor_chunks_); ailego_assert_abort(chunk_idx < upper_neighbor_chunks_.size(), @@ -392,9 +393,9 @@ class HnswSparseStreamerEntity : public HnswSparseEntity { ailego_assert_with(chunk_index < (1U << (28 - upper_neighbor_mask_bits_)), "invalid chunk index"); UpperNeighborIndexMeta meta; - meta.level = level; - meta.index = (chunk_index << upper_neighbor_mask_bits_) | - (chunk_offset / upper_neighbor_size_); + meta.bits.level = level; + meta.bits.index = (chunk_index << upper_neighbor_mask_bits_) | + (chunk_offset / upper_neighbor_size_); chunk_offset += upper_neighbor_size_ * level; if (ailego_unlikely(!upper_neighbor_index_->insert(id, meta.data))) { LOG_ERROR("HashMap insert value failed"); diff --git a/src/core/algorithm/vamana/vamana_entity.h b/src/core/algorithm/vamana/vamana_entity.h index be06bf2..976d385 100644 --- a/src/core/algorithm/vamana/vamana_entity.h +++ b/src/core/algorithm/vamana/vamana_entity.h @@ -55,11 +55,11 @@ struct VamanaHeader { } VamanaHeader(const VamanaHeader &header) { - memcpy(this, &header, sizeof(header)); + memcpy(static_cast(this), &header, sizeof(header)); } VamanaHeader &operator=(const VamanaHeader &header) { - memcpy(this, &header, sizeof(header)); + memcpy(static_cast(this), &header, sizeof(header)); return *this; } @@ -69,7 +69,7 @@ struct VamanaHeader { } void inline clear() { - memset(this, 0, sizeof(VamanaHeader)); + memset(static_cast(this), 0, sizeof(VamanaHeader)); graph.entry_point = kInvalidNodeId; graph.size = sizeof(VamanaGraphHeader); graph.alpha = 1.2f; @@ -204,7 +204,8 @@ class VamanaEntity { // data_type: IndexMeta::DataType value (e.g. DT_FP32=2, DT_INT8=4, // DT_FP16=1) // Returns the medoid node ID, or kInvalidNodeId if no valid data. - virtual node_id_t calculate_medoid(uint32_t dimension, uint32_t data_type) { + virtual node_id_t calculate_medoid(uint32_t /*dimension*/, + uint32_t /*data_type*/) { return kInvalidNodeId; } diff --git a/src/core/plugin/diskann_plugin.cc b/src/core/plugin/diskann_plugin.cc index d26b87a..9d4e9d0 100644 --- a/src/core/plugin/diskann_plugin.cc +++ b/src/core/plugin/diskann_plugin.cc @@ -42,10 +42,12 @@ constexpr const char *kLibAioSoNames[] = { }; constexpr bool kPlatformSupportsDiskAnnPlugin = true; #elif defined(__APPLE__) -constexpr const char *kPluginFileName = "libzvec_diskann_plugin.dylib"; +[[maybe_unused]] constexpr const char *kPluginFileName = + "libzvec_diskann_plugin.dylib"; constexpr bool kPlatformSupportsDiskAnnPlugin = false; #else -constexpr const char *kPluginFileName = "zvec_diskann_plugin.dll"; +[[maybe_unused]] constexpr const char *kPluginFileName = + "zvec_diskann_plugin.dll"; constexpr bool kPlatformSupportsDiskAnnPlugin = false; #endif diff --git a/src/core/utility/buffer_storage.cc b/src/core/utility/buffer_storage.cc index 7625183..c7cba5e 100644 --- a/src/core/utility/buffer_storage.cc +++ b/src/core/utility/buffer_storage.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -522,8 +523,8 @@ class BufferStorage : public IndexStorage { return ret; } LOG_INFO( - "BufferStorage opened: file=%s, writable=%d, max_segment_size=%lu, " - "segment_count=%zu", + "BufferStorage opened: file=%s, writable=%d, max_segment_size=%" PRIu64 + ", segment_count=%zu", file_name_.c_str(), static_cast(create_if_missing), max_segment_size_, segments_.size()); return 0; @@ -682,11 +683,10 @@ class BufferStorage : public IndexStorage { if (footer_offset < current_header_start_offset_ || footer_offset + sizeof(IndexFormat::MetaFooter) > buffer_pool_->file_size()) { - LOG_ERROR( - "ParseToMapping: invalid footer_offset=%lu (header=%lu, " - "file_size=%lu), file[%s]", - footer_offset, current_header_start_offset_, - buffer_pool_->file_size(), file_name_.c_str()); + LOG_ERROR("ParseToMapping: invalid footer_offset=%" PRIu64 + " (header=%" PRIu64 ", file_size=%zu), file[%s]", + footer_offset, current_header_start_offset_, + buffer_pool_->file_size(), file_name_.c_str()); return IndexError_InvalidValue; } ret = ParseFooter(footer_offset); @@ -728,11 +728,10 @@ class BufferStorage : public IndexStorage { if (next_off <= current_header_start_offset_ || next_off + sizeof(IndexFormat::MetaHeader) > buffer_pool_->file_size()) { - LOG_ERROR( - "ParseToMapping: invalid next_meta_header_offset=%lu " - "(current=%lu, file_size=%lu), file[%s]", - next_off, current_header_start_offset_, buffer_pool_->file_size(), - file_name_.c_str()); + LOG_ERROR("ParseToMapping: invalid next_meta_header_offset=%" PRIu64 + " (current=%" PRIu64 ", file_size=%zu), file[%s]", + next_off, current_header_start_offset_, + buffer_pool_->file_size(), file_name_.c_str()); return IndexError_InvalidValue; } // Bound chain count: 1024 chains @ default 1MB segment_meta_capacity diff --git a/src/db/common/rocksdb_context.cc b/src/db/common/rocksdb_context.cc index 44814a9..24176e8 100644 --- a/src/db/common/rocksdb_context.cc +++ b/src/db/common/rocksdb_context.cc @@ -489,7 +489,7 @@ Status RocksdbContext::reset_cf(const std::string &cf_name) { } rocksdb::ColumnFamilyHandle *cf_handle{nullptr}; - size_t index; + size_t index = 0; for (size_t i = 0; i < cf_handles_.size(); ++i) { if (cf_handles_[i]->GetName() == cf_name) { cf_handle = cf_handles_[i]; diff --git a/src/db/index/common/doc.cc b/src/db/index/common/doc.cc index 8948ca4..b16b403 100644 --- a/src/db/index/common/doc.cc +++ b/src/db/index/common/doc.cc @@ -124,9 +124,12 @@ namespace { template T byte_swap(T value) { if constexpr (std::is_same_v) { - uint16_t val = *reinterpret_cast(&value); + uint16_t val; + std::memcpy(&val, static_cast(&value), sizeof(val)); val = ailego_bswap16(val); - return *reinterpret_cast(&val); + float16_t result; + std::memcpy(static_cast(&result), &val, sizeof(result)); + return result; } else if constexpr (sizeof(T) == 1) { return value; } else if constexpr (sizeof(T) == 2) { diff --git a/src/db/index/common/schema.cc b/src/db/index/common/schema.cc index 5562c4e..8886a6b 100644 --- a/src/db/index/common/schema.cc +++ b/src/db/index/common/schema.cc @@ -164,9 +164,11 @@ Status FieldSchema::validate() const { "RabitQ requires AVX2/AVX512F to be supported"); } - if (kRabitqCompiledAvx512 && !flags.AVX512F) { - return Status::NotSupported( - "RabitQ compiled with AVX512F while runtime does not support"); + if constexpr (kRabitqCompiledAvx512) { + if (!flags.AVX512F) { + return Status::NotSupported( + "RabitQ compiled with AVX512F while runtime does not support"); + } } } diff --git a/src/db/index/segment/segment_helper.cc b/src/db/index/segment/segment_helper.cc index d2b0b74..1238a48 100644 --- a/src/db/index/segment/segment_helper.cc +++ b/src/db/index/segment/segment_helper.cc @@ -867,6 +867,7 @@ Status SegmentHelper::PrepareQuantizeField( } #if !RABITQ_SUPPORTED + (void)raw_vector_provider; return Status::NotSupported( "RabitQ is not supported on this platform (Linux x86_64 only)"); #else diff --git a/src/db/sqlengine/planner/ops/contain_op.cc b/src/db/sqlengine/planner/ops/contain_op.cc index 9eddb26..122d66b 100644 --- a/src/db/sqlengine/planner/ops/contain_op.cc +++ b/src/db/sqlengine/planner/ops/contain_op.cc @@ -27,7 +27,7 @@ bool match_value(const arrow::Array *value_array, int64_t offset, auto *value_typed_arr = static_cast(value_array); auto *value_set_typed_arr = static_cast(value_set_array); - if (contain_type == ContainType::kContainAll) { + if constexpr (contain_type == ContainType::kContainAll) { for (int j = 0; j < value_set_typed_arr->length(); ++j) { bool contain = false; for (int i = 0; i < length; ++i) { diff --git a/src/include/zvec/ailego/utility/string_helper_impl.h b/src/include/zvec/ailego/utility/string_helper_impl.h index 9817214..d098ddb 100644 --- a/src/include/zvec/ailego/utility/string_helper_impl.h +++ b/src/include/zvec/ailego/utility/string_helper_impl.h @@ -33,18 +33,19 @@ static T CStringToType(const char *begin, char **endptr) { std::is_same::value || std::is_same::value || std::is_same::value || std::is_same::value, "type not supported"); - if (std::is_same::value || std::is_same::value || - std::is_same::value) { + if constexpr (std::is_same::value || + std::is_same::value || + std::is_same::value) { return static_cast(strtol(begin, endptr, 0)); - } else if (std::is_same::value) { + } else if constexpr (std::is_same::value) { return static_cast(strtoll(begin, endptr, 0)); - } else if (std::is_same::value || - std::is_same::value || - std::is_same::value) { + } else if constexpr (std::is_same::value || + std::is_same::value || + std::is_same::value) { return static_cast(strtoul(begin, endptr, 0)); - } else if (std::is_same::value) { + } else if constexpr (std::is_same::value) { return static_cast(strtoull(begin, endptr, 0)); - } else if (std::is_same::value) { + } else if constexpr (std::is_same::value) { return static_cast(strtof(begin, endptr)); } else { return static_cast(strtod(begin, endptr)); diff --git a/src/include/zvec/core/framework/index_holder.h b/src/include/zvec/core/framework/index_holder.h index 952d20a..7828837 100644 --- a/src/include/zvec/core/framework/index_holder.h +++ b/src/include/zvec/core/framework/index_holder.h @@ -99,13 +99,13 @@ struct IndexHybridHolder : public IndexHolder { ~Iterator(void) override {} //! Retrieve pointer of data - virtual const void *data(void) const = 0; + const void *data(void) const override = 0; //! Test if the iterator is valid - virtual bool is_valid(void) const = 0; + bool is_valid(void) const override = 0; //! Retrieve primary key - virtual uint64_t key(void) const = 0; + uint64_t key(void) const override = 0; //! Retrieve sparse count virtual uint32_t sparse_count() const = 0; @@ -117,7 +117,7 @@ struct IndexHybridHolder : public IndexHolder { virtual const void *sparse_data() const = 0; //! Next iterator - virtual void next(void) = 0; + void next(void) override = 0; }; //! Destructor diff --git a/src/include/zvec/core/framework/index_segment_storage.h b/src/include/zvec/core/framework/index_segment_storage.h index 1562084..06b1777 100644 --- a/src/include/zvec/core/framework/index_segment_storage.h +++ b/src/include/zvec/core/framework/index_segment_storage.h @@ -106,7 +106,7 @@ class IndexSegmentStorage : public IndexStorage { } //! Retrieve offset of data - virtual size_t data_offset(void) const { + size_t data_offset(void) const override { return 0; } diff --git a/tools/core/local_builder_original.cc b/tools/core/local_builder_original.cc index 60aa9a2..77c8513 100644 --- a/tools/core/local_builder_original.cc +++ b/tools/core/local_builder_original.cc @@ -123,7 +123,7 @@ int setup_hnsw_rabitq_streamer(const IndexStreamer::Pointer &streamer, RabitqConverter rabitq_converter; ailego::Params rabitq_converter_params; if (config_root["RabitqConverterParams"] && - !prepare_params(std::move(config_root["RabitqConverterParams"]), + !prepare_params(config_root["RabitqConverterParams"], rabitq_converter_params)) { cerr << "Failed to prepare rabitq converter params" << endl; return -1; diff --git a/tools/core/txt2vecs.cc b/tools/core/txt2vecs.cc index 6db7d03..b1fc1b5 100644 --- a/tools/core/txt2vecs.cc +++ b/tools/core/txt2vecs.cc @@ -33,7 +33,7 @@ DEFINE_string(method, "L2", "available method: L2, IP"); DEFINE_int32(dimension, 256, "data dimension"); DEFINE_string(vector_type, "dense", "available type: dense, hybrid, sparse"); -bool write_header_output(VecsHeader header, const IndexMeta &meta, +bool write_header_output(VecsHeader &header, const IndexMeta &meta, size_t &total_writes, FILE *wfp) { // write header std::cout << "Begin to Write Header Section..." << std::endl; @@ -69,7 +69,7 @@ bool write_header_output(VecsHeader header, const IndexMeta &meta, return true; } -bool write_header_output_sparse(VecsHeader header, const IndexMeta &meta, +bool write_header_output_sparse(VecsHeader &header, const IndexMeta &meta, size_t &total_writes, FILE *wfp) { // write header std::cout << "Begin to Write Header Section..." << std::endl; diff --git a/tools/core/vecs_common.h b/tools/core/vecs_common.h index 482ca3e..629e016 100644 --- a/tools/core/vecs_common.h +++ b/tools/core/vecs_common.h @@ -42,7 +42,13 @@ struct VecsHeader { uint64_t partition_size; // size for partition uint64_t taglist_offset; // offset for taglist uint64_t taglist_size; // size for taglist - uint8_t meta_buf[0]; + + uint8_t *meta_buf() { + return reinterpret_cast(this + 1); + } + const uint8_t *meta_buf() const { + return reinterpret_cast(this + 1); + } }; #pragma pack() diff --git a/tools/core/vecs_reader.h b/tools/core/vecs_reader.h index 110e520..61099c9 100644 --- a/tools/core/vecs_reader.h +++ b/tools/core/vecs_reader.h @@ -65,14 +65,14 @@ class VecsReader { num_vecs_ = header->num_vecs; // deserialize - bool bret = index_meta_.deserialize(&header->meta_buf, header->meta_size); + bool bret = index_meta_.deserialize(header->meta_buf(), header->meta_size); if (!bret) { std::cerr << "deserialize index meta error." << std::endl; return false; } const char *data_base_ptr = - reinterpret_cast(header + 1) + header->meta_size; + reinterpret_cast(header->meta_buf()) + header->meta_size; vector_base_ = reinterpret_cast(data_base_ptr); key_base_ = reinterpret_cast( @@ -250,17 +250,17 @@ class SparseVecsReader { num_vecs_ = header->num_vecs; // deserialize - bool bret = index_meta_.deserialize(&header->meta_buf, header->meta_size); + bool bret = index_meta_.deserialize(header->meta_buf(), header->meta_size); if (!bret) { std::cerr << "deserialize index meta error." << std::endl; return false; } const char *data_base_ptr = - reinterpret_cast(header + 1) + header->meta_size; + reinterpret_cast(header->meta_buf()) + header->meta_size; key_base_ = reinterpret_cast( - reinterpret_cast(header + 1) + header->meta_size); + reinterpret_cast(header->meta_buf()) + header->meta_size); sparse_base_meta_ = reinterpret_cast(key_base_ + num_vecs_); sparse_base_data_ = reinterpret_cast( sparse_base_meta_ + num_vecs_ * sizeof(uint64_t));