From d73de95f722247a56b5266a27267cd24668081e9 Mon Sep 17 00:00:00 2001 From: Slobodan Predolac Date: Wed, 4 Jun 2025 09:48:14 -0700 Subject: [PATCH] Experimental configuration option for fast path prefetch from cache_bin --- configure.ac | 30 +++++++++++++++++++ .../internal/jemalloc_internal_defs.h.in | 5 ++++ .../internal/jemalloc_internal_inlines_c.h | 6 ++++ 3 files changed, 41 insertions(+) diff --git a/configure.ac b/configure.ac index c703a6d1..d9153feb 100644 --- a/configure.ac +++ b/configure.ac @@ -1434,6 +1434,36 @@ if test "x$enable_experimental_smallocx" = "x1" ; then fi AC_SUBST([enable_experimental_smallocx]) +dnl Do not enable fastpath prefetch by default. +AC_ARG_ENABLE([experimental_fp_prefetch], + [AS_HELP_STRING([--enable-experimental-fp-prefetch], [Enable experimental fastpath prefetch])], +[if test "x$enable_experimental_fp_prefetch" = "xno" ; then +enable_experimental_fp_prefetch="0" +else + dnl Check if we have __builtin_prefetch. + JE_CFLAGS_SAVE() + JE_CFLAGS_ADD([-Werror=implicit-function-declaration]) + JE_COMPILABLE([builtin prefetch], [], [ +void foo(void *p) { __builtin_prefetch(p, 1, 3); } + ], + [je_cv_have_builtin_prefetch]) + + if test "x${je_cv_have_builtin_prefetch}" = "xyes" ; then + enable_experimental_fp_prefetch="1" + else + enable_experimental_fp_prefetch="0" + AC_MSG_ERROR([--enable--experimental-fp-prefetch can only be used when builtin_preftech is available]) + fi + JE_CFLAGS_RESTORE() +fi +], +[enable_experimental_fp_prefetch="0"] +) +if test "x$enable_experimental_fp_prefetch" = "x1" ; then + AC_DEFINE([JEMALLOC_EXPERIMENTAL_FASTPATH_PREFETCH], [ ], [ ]) +fi +AC_SUBST([enable_experimental_fp_prefetch]) + dnl Do not enable profiling by default. AC_ARG_ENABLE([prof], [AS_HELP_STRING([--enable-prof], [Enable allocation profiling])], diff --git a/include/jemalloc/internal/jemalloc_internal_defs.h.in b/include/jemalloc/internal/jemalloc_internal_defs.h.in index 31ae2e8e..3a945ba1 100644 --- a/include/jemalloc/internal/jemalloc_internal_defs.h.in +++ b/include/jemalloc/internal/jemalloc_internal_defs.h.in @@ -160,6 +160,11 @@ /* JEMALLOC_EXPERIMENTAL_SMALLOCX_API enables experimental smallocx API. */ #undef JEMALLOC_EXPERIMENTAL_SMALLOCX_API +/* JEMALLOC_EXPERIMENTAL_FASTPATH_PREFETCH enables prefetch + * on malloc fast path. + */ +#undef JEMALLOC_EXPERIMENTAL_FASTPATH_PREFETCH + /* JEMALLOC_PROF enables allocation profiling. */ #undef JEMALLOC_PROF diff --git a/include/jemalloc/internal/jemalloc_internal_inlines_c.h b/include/jemalloc/internal/jemalloc_internal_inlines_c.h index 2c61f8c4..16f86ad4 100644 --- a/include/jemalloc/internal/jemalloc_internal_inlines_c.h +++ b/include/jemalloc/internal/jemalloc_internal_inlines_c.h @@ -374,6 +374,12 @@ imalloc_fastpath(size_t size, void *(fallback_alloc)(size_t)) { */ ret = cache_bin_alloc_easy(bin, &tcache_success); if (tcache_success) { +#if defined(JEMALLOC_EXPERIMENTAL_FASTPATH_PREFETCH) + cache_bin_sz_t lb = (cache_bin_sz_t)(uintptr_t)bin->stack_head; + if(likely(lb != bin->low_bits_empty)) { + util_prefetch_write_range(*(bin->stack_head), usize); + } +#endif fastpath_success_finish(tsd, allocated_after, bin, ret); return ret; }