Repository navigation
Description
Describe what you are looking for
On POSIX, page_allocator_t in include/usearch/index_plugins.hpp allocates via mmap / munmap directly. That bypasses any allocator-interception layer the embedding application has in place — heap profilers, jemalloc / tcmalloc LD_PRELOAD shims, or custom memory-accounting hooks that wrap malloc / free. For a long-running process that embeds USearch alongside other components, the index's page allocations are invisible to the tooling that accounts for every other allocation in the process, which makes memory budgets, leak detection, and heap profiling incomplete.
I'd like a compile-time switch — proposed name USEARCH_USE_MALLOC_ALLOCATOR, defaulting OFF — that makes page_allocator_t route allocations through aligned_alloc(page_size(), count_bytes) / free(ptr) instead. The alignment contract page_allocator_t guarantees to its callers is preserved because aligned_alloc is explicitly asked for page_size() alignment, and count_bytes is already rounded up to page_size() by checked_round_up before the allocation call (so the C11 aligned_alloc size/alignment contract is satisfied).
Shape and naming would follow the existing USEARCH_USE_OPENMP / USEARCH_USE_NUMKONG convention already on main-v2 — CMake option(...) + target_compile_definitions(... "USEARCH_USE_MALLOC_ALLOCATOR=$<BOOL:${USEARCH_USE_MALLOC_ALLOCATOR}>") + #if !defined(...) #define ... 0 #endif default at the top of the header + #if USEARCH_USE_MALLOC_ALLOCATOR branch inside page_allocator_t::allocate / ::deallocate. Windows (VirtualAlloc / VirtualFree) and POSIX mmap / munmap paths stay unchanged. When the flag is OFF, generated code is byte-identical to today.
Rough sketch:
// include/usearch/index_plugins.hpp — near the other USEARCH_USE_* defaults
#if !defined(USEARCH_USE_MALLOC_ALLOCATOR)
#define USEARCH_USE_MALLOC_ALLOCATOR 0
#endif
// inside page_allocator_t::allocate
#if defined(USEARCH_DEFINED_WINDOWS)
return (byte_t*)(::VirtualAlloc(NULL, count_bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE));
#elif USEARCH_USE_MALLOC_ALLOCATOR
return (byte_t*)aligned_alloc(page_size(), count_bytes);
#else
auto* result = mmap(NULL, count_bytes, PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
return (result == MAP_FAILED) ? nullptr : (byte_t*)result;
#endif
// inside page_allocator_t::deallocate (mirror branch, with (void)count_bytes; free(page_pointer);)From reading the code, the 3.0 redesign on main replaces page_allocator_t entirely, so this request is scoped to main-v2 — the 2.x line that downstream integrations (including vcpkg ports pinned to 2.x tags) still consume.
I'll open a PR against main-v2 with the implementation.
Can you contribute to the implementation?
- I can contribute
Is your feature request specific to a certain interface?
C++ implementation
Contact Details
No response
Is there an existing issue for this?
- I have searched the existing issues
Code of Conduct
- I agree to follow this project's Code of Conduct
Evidence the problem exists
On current main-v2 HEAD (33d3ab6, unpatched), built a 20-line LD_PRELOAD shim that wraps malloc / aligned_alloc / free and counts calls + bytes, then ran a tiny program that allocates 10 × 128 KiB via page_allocator_t:
[bench] allocated 10 x 131072 bytes, all page-aligned
[intercept] malloc=0 (0 bytes) aligned_alloc=0 (0 bytes) free=0
All 10 × 128 KiB (= 1,310,720 bytes) are invisible to the interceptor — the allocations go straight through mmap to the kernel, bypassing anything that wraps the C allocator. This is the accounting gap a USEARCH_USE_MALLOC_ALLOCATOR build flag would close.
Activity