From 567d071c117184cc600102a4f92e43df5a8f0565 Mon Sep 17 00:00:00 2001 From: Tamim Ehsan Date: Wed, 26 Aug 2026 20:11:53 +0800 Subject: [PATCH] back the hnsw index arrays with transparent huge pages --- include/rabitqlib/index/hnsw/hnsw.hpp | 6 +++--- include/rabitqlib/utils/memory.hpp | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/rabitqlib/index/hnsw/hnsw.hpp b/include/rabitqlib/index/hnsw/hnsw.hpp index 7c8eaf4..71b59e6 100644 --- a/include/rabitqlib/index/hnsw/hnsw.hpp +++ b/include/rabitqlib/index/hnsw/hnsw.hpp @@ -423,7 +423,7 @@ inline HierarchicalNSW::HierarchicalNSW( offsetExData_ + size_ex_data_; // (# of edges + edges) + (cluster_id) + (external // label) + (BinData) + (ExData) data_level0_memory_ = - reinterpret_cast(malloc(max_elements_ * size_data_per_element_)); + memory::huge_page_allocate(max_elements_ * size_data_per_element_); if (data_level0_memory_ == nullptr) { throw std::runtime_error("Not enough memory"); } @@ -549,7 +549,7 @@ inline void HierarchicalNSW::load(const char* filename) { input.read(reinterpret_cast(&ef_construction_), sizeof(size_t)); const size_t centroids_bytes = num_cluster_ * padded_dim_ * sizeof(float); - centroids_memory_ = reinterpret_cast(malloc(centroids_bytes)); + centroids_memory_ = memory::huge_page_allocate(centroids_bytes); if (centroids_memory_ == nullptr) { throw std::runtime_error("Not enough memory: loadIndex failed to allocate centroids" ); @@ -561,7 +561,7 @@ inline void HierarchicalNSW::load(const char* filename) { ); data_level0_memory_ = - reinterpret_cast(malloc(max_elements_ * size_data_per_element_)); + memory::huge_page_allocate(max_elements_ * size_data_per_element_); input.read( data_level0_memory_, diff --git a/include/rabitqlib/utils/memory.hpp b/include/rabitqlib/utils/memory.hpp index fb93ee1..8dced55 100644 --- a/include/rabitqlib/utils/memory.hpp +++ b/include/rabitqlib/utils/memory.hpp @@ -86,6 +86,14 @@ inline T* align_allocate(size_t nbytes) { return static_cast(ptr); } +inline constexpr size_t kHugePageSize = 2UL << 20; + +// Allocate a large buffer backed by transparent huge pages. +template +inline T* huge_page_allocate(size_t nbytes) { + return align_allocate(nbytes); +} + static inline void prefetch_l1(const void* addr) { #if defined(__SSE2__) _mm_prefetch(addr, _MM_HINT_T0);