From f0bf91af563f64166c8070ff4703f6a3f273971d Mon Sep 17 00:00:00 2001 From: Harshad Sane Date: Thu, 26 Feb 2026 11:29:22 -0800 Subject: [PATCH] Add size-gated hugepage optimization to system allocator This patch adds optional hugepage optimization by applying madvise(MADV_HUGEPAGE) to allocations that are large enough to benefit from transparent hugepages. Current behavior: - Uses MADV_NOHUGEPAGE for infrequent access patterns, MADV_HUGEPAGE only for memory collapsing - No proactive hugepages for large allocations Enhanced behavior: - Adds MADV_HUGEPAGE for allocations >= kHugePageSize (2MB) - Only applies to allocations that can actually benefit from hugepages - Avoids kernel overhead on small allocations The size threshold ensures we only hint hugepage usage for allocations that can actually be backed by hugepages, avoiding unnecessary kernel overhead for smaller allocations. --- tcmalloc/internal/system_allocator.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tcmalloc/internal/system_allocator.h b/tcmalloc/internal/system_allocator.h index a25e1f1b4..90f9bf328 100644 --- a/tcmalloc/internal/system_allocator.h +++ b/tcmalloc/internal/system_allocator.h @@ -377,6 +377,15 @@ SystemAllocator::MmapRegion::Alloc( // This is only advisory, so ignore the error. ErrnoRestorer errno_restorer; (void)madvise(result_ptr, actual_size, MADV_NOHUGEPAGE); + } else { + // Opt-in to transparent hugepages for large allocations when system is + // configured for transparent_hugepage=madvise. This can improve memory + // performance by reducing TLB pressure. + if (actual_size >= kHugePageSize) { + // This is only advisory, so ignore the error. + ErrnoRestorer errno_restorer; + (void)madvise(result_ptr, actual_size, MADV_HUGEPAGE); + } } free_size_ -= actual_size; return {result_ptr, actual_size};