No description
Find a file
Azat Khuzhin 7aa77c8e2a Add an inline fast path to JEMALLOC_TLS_ADDR for static TLS
The noinline accessor puts an opaque call on the malloc/free fastpath.
test/stress/microbench (malloc(1)/free pairs, pinned core, clang):

                     unpatched      accessor       this commit
  malloc, no LTO     7.0 ns/op      9.8 (+40%)     7.0 (+-0%)
  free,   no LTO     6.5 ns/op      9.2 (+40%)     6.5 (+-0%)
  malloc, ThinLTO    7.0 ns/op      9.0 (+28%)     7.6 (~+8%)
  free,   ThinLTO    6.5 ns/op      8.4 (+28%)     7.0 (~+8%)

Under a static TLS model the tsd address is `thread_pointer + offset` with a
thread-independent offset.  Capture the offset once (a noinline helper, lazy and
sentinel-initialized) and re-read the thread pointer per call with a `volatile`
asm the optimizer may not hoist or CSE (per-arch reads from mimalloc's
mi_prim_tls_slot).  Both inputs are hoist-proof: the tp read is volatile, the
offset is thread-independent.

The offset global is read and written with relaxed atomics: threads racing
their first allocation init it concurrently with the same value, so a plain
access would be a benign-but-UB data race (TSan-reported).  Relaxed adds no
fence -- it compiles to the same load/store on the fast path.

The offset must NOT be computed inline as `&tsd_tls - __builtin_thread_pointer()`:
the terms hoist independently, and clang ThinLTO keeps the stale `&tsd_tls` in a
callee-saved register while re-reading the thread pointer, so `fresh_tp +
(stale_addr - fresh_tp)` cancels back to the stale address.  Capturing behind a
call boundary evaluates both terms at one point on one thread.

Gated on JEMALLOC_TLS_MODEL_INITIAL_EXEC (a new configure define, set whenever
jemalloc applies its default initial-exec model), GNU asm, a known arch, and
!_WIN32 (MinGW's thread pointer lives in the TEB, not fs/gs:0); everything else
keeps the noinline accessor.  Verified with the fiber-migration reproducer under
clang ThinLTO.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 13:24:40 +02:00
.github/workflows ci: add a Linux whole-program-LTO lane (test-linux-lto) 2026-06-22 12:54:12 +02:00
bin Updated jeprof with more symbols to filter. 2024-10-14 10:31:58 -07:00
build-aux Update config.guess and config.sub to the latest versions 2026-03-10 18:14:33 -07:00
doc Replace experimental_infallible_new with compile-time flag 2026-06-06 09:50:37 -04:00
doc_internal update PROFILING_INTERNALS.md 2022-10-03 10:48:29 -07:00
include Add an inline fast path to JEMALLOC_TLS_ADDR for static TLS 2026-06-22 13:24:40 +02:00
m4 Support C++17 over-aligned allocation 2019-11-22 10:14:16 -08:00
msvc Move malloc routing into new malloc_dispatch module 2026-06-04 11:32:53 -04:00
scripts ci: add a Linux whole-program-LTO lane (test-linux-lto) 2026-06-22 12:54:12 +02:00
src Guard TSD thread-locals against LTO hoisting (JEMALLOC_TLS_ADDR) 2026-06-22 12:55:02 +02:00
test test: add tcache_fiber_migration standalone LTO reproducer 2026-06-22 12:54:12 +02:00
.appveyor.yml Update msys to vs2022 for appveyor 2026-04-19 22:49:25 -07:00
.autom4te.cfg Disable autom4te cache. 2014-09-02 17:49:29 -07:00
.clang-format Modify .clang-format to have declarations aligned 2025-05-28 19:22:21 -07:00
.git-blame-ignore-revs Ignore the clang-format changes in the git blame. 2026-03-10 18:14:33 -07:00
.gitattributes fix git handling of newlines on windows 2014-05-07 18:48:39 -04:00
.gitignore Adding trace analysis in preparation for page allocator microbenchmark. 2026-03-10 18:14:33 -07:00
.travis.yml Remove Travis Windows CI for now since it has infra failures. 2026-03-10 18:14:33 -07:00
autogen.sh build: Make autogen.sh accept quoted extra options 2024-01-03 14:20:34 -08:00
ChangeLog Update ChangeLog for release 5.3.1 2026-04-13 17:12:37 -07:00
config.stamp.in Move repo contents in jemalloc/ to top level. 2011-03-31 20:36:17 -07:00
configure.ac Add an inline fast path to JEMALLOC_TLS_ADDR for static TLS 2026-06-22 13:24:40 +02:00
COPYING Update copyright dates. 2019-01-25 13:25:20 -08:00
INSTALL.md Replace experimental_infallible_new with compile-time flag 2026-06-06 09:50:37 -04:00
jemalloc.pc.in Expose jemalloc_prefix via pkg-config 2023-09-05 14:30:21 -07:00
Makefile.in test: add tcache_fiber_migration standalone LTO reproducer 2026-06-22 12:54:12 +02:00
README switch to https 2023-03-09 11:44:02 -08:00
run_tests.sh Introduce scripts to run all possible tests 2017-01-30 17:51:57 -08:00
TUNING.md switch to https 2023-03-09 11:44:02 -08:00

jemalloc is a general purpose malloc(3) implementation that emphasizes
fragmentation avoidance and scalable concurrency support.  jemalloc first came
into use as the FreeBSD libc allocator in 2005, and since then it has found its
way into numerous applications that rely on its predictable behavior.  In 2010
jemalloc development efforts broadened to include developer support features
such as heap profiling and extensive monitoring/tuning hooks.  Modern jemalloc
releases continue to be integrated back into FreeBSD, and therefore versatility
remains critical.  Ongoing development efforts trend toward making jemalloc
among the best allocators for a broad range of demanding applications, and
eliminating/mitigating weaknesses that have practical repercussions for real
world applications.

The COPYING file contains copyright and licensing information.

The INSTALL file contains information on how to configure, build, and install
jemalloc.

The ChangeLog file contains a brief summary of changes for each release.

URL: https://jemalloc.net/