From aed99d3aadbab6aa5f30d6e1e2d83aa84b6048e3 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 10 Jul 2026 08:00:32 +0900 Subject: [PATCH] Advise MADV_HUGEPAGE independently of allow_large The THP advise was gated on the caller's allow_large parameter, which callers derive from segment laziness and which primarily exists to gate MAP_HUGETLB. hugetlbfs pages are physically committed at map time, so keeping them away from lazily-committed segments makes sense, but transparent huge pages materialize on fault and have no such cost. As a result of sharing the gate, huge objects (the OS singleton path) and each thread's first segment were never eligible for THP under Linux's madvise THP mode even with allow_thp enabled. With this change the advise depends only on the allow_thp option, matching the effective behavior of the dev3 branch, where singleton pages are committed eagerly and allow_large no longer depends on laziness. In the mold linker, whose largest data structures allocate through the huge-object path, this makes linking a Chromium debug build about 2% faster (3.37s to 3.29s) on a 64-core machine, and 15% faster (4.05s to 3.43s) when mold runs in its single-process mode, where the process exit cost is user-visible and freeing a heap of 2 MiB folios is much cheaper than freeing millions of 4 KiB folios. Co-Authored-By: Claude Fable 5 --- src/prim/unix/prim.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prim/unix/prim.c b/src/prim/unix/prim.c index 358678187..2ab0cd73b 100644 --- a/src/prim/unix/prim.c +++ b/src/prim/unix/prim.c @@ -389,7 +389,7 @@ static void* unix_mmap(void* addr, size_t size, size_t try_alignment, int protec *is_large = false; p = unix_mmap_prim_aligned(addr, size, try_alignment, protect_flags, flags, fd); #if !defined(MI_NO_THP) - if (p != NULL && allow_large && mi_option_is_enabled(mi_option_allow_thp) && _mi_os_canuse_large_page(size, try_alignment)) { + if (p != NULL && mi_option_is_enabled(mi_option_allow_thp) && _mi_os_canuse_large_page(size, try_alignment)) { #if defined(MADV_HUGEPAGE) // Many Linux systems don't allow MAP_HUGETLB but they support instead // transparent huge pages (THP). Generally, it is not required to call `madvise` with MADV_HUGE