Skip to content

feat: make jemalloc the default allocator of the native library - #7835

Merged
comphead merged 1 commit into
apache:mainfrom
comphead:jemalloc-default
Dec 5, 2026
Merged

comphead merged 1 commit into
apache:mainfrom
comphead:jemalloc-default

Conversation

@comphead

@comphead comphead commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Part of #7344.

Rationale for this change

The native library allocates its Rust memory with the system allocator, which is glibc malloc on
Linux. In long-running executors glibc keeps freed memory resident, and executors get OOM-killed
while Comet's pool and allocation counter stay under the limit
(#7108). In
#7344 (comment), building with the
jemalloc feature halved the smallest clean pod size for TPC-H q18 at SF1000, and cut the peak
anonymous memory at 20 GiB from 18.5 GiB to 10.4 GiB.

What changes are included in this PR?

  • native/core/Cargo.toml: jemalloc joins the default features, so cargo build, the make
    targets, CI and the release builds use jemalloc. A build with
    --no-default-features --features hdfs-opendal,delta keeps the system allocator.
  • native/core/src/lib.rs: an explicit mimalloc request now wins over the default jemalloc.
    Before, the two together selected the system allocator. jni_api.rs reads jemalloc's stats only
    when jemalloc is the selected backend, and a new test asserts the selected backend.
  • Release: the native library is built once per architecture, with JEMALLOC_SYS_WITH_LG_PAGE=12
    set first, and each Spark and Scala combination publishes one jar. The jemalloc classifier jar
    and the jemalloc Maven profile are removed. Neither has been released. The snapshot workflow
    pins the page size too. Users on arm64 kernels with 64 KiB pages, such as RHEL 8, need to build
    from source, as the FAQ says.
  • CI: the rust-test step that covered jemalloc now lints and tests the system allocator and
    mimalloc.
  • dev/licenses/LICENSE-binary.txt includes jemalloc's license, and the jar contents check
    verifies it against the version tikv-jemalloc-sys vendors.
  • Docs: source.md says that Comet uses jemalloc by default and why, and shows how to build with
    the system allocator. The FAQ, the memory tuning guide, release_process.md, tracing.md and
    memory_management.md follow.

How are these changes tested?

  • selects_the_requested_allocator_backend asserts the selected backend. CI runs it over jemalloc,
    the system allocator and mimalloc.
  • dev/ci/test-jar-contents.py covers the jemalloc LICENSE check: version drift, an unreadable
    version and a missing copyright line.
  • The existing tests now run over jemalloc.
  • Locally: cargo clippy --all-targets -- -D warnings for the three allocator configurations,
    cargo fmt, prettier, actionlint, check-ci-config.py, bash -n on the release scripts, and
    the release script's jar check against stand-in jars. The Docker release build and a snapshot
    dry run were not run.

@github-actions github-actions Bot added the enhancement New feature or request label Dec 2, 2026
@comphead
comphead force-pushed the jemalloc-default branch 2 times, most recently from 6b2284f to 8c58b66 Compare December 3, 2026 00:15

@sunchao sunchao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the release scripts along with the default. Both release builds now select jemalloc, and an explicit mimalloc build silently selects the system allocator. The extra allocator test could switch to --no-default-features --features hdfs-opendal,delta to keep exercising the system backend.

Automated review by Codex.

Comment thread native/core/Cargo.toml
[features]
backtrace = ["datafusion/backtrace"]
default = ["hdfs-opendal", "delta"]
default = ["hdfs-opendal", "delta", "jemalloc"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The release script builds once with defaults and once with --features jemalloc. Both now select jemalloc. If the libraries match, cmp aborts the release. If they differ, we still ship jemalloc twice, and only the second build pins the page size. Can we set JEMALLOC_SYS_WITH_LG_PAGE=12 before building the default library and update the packaging script to either drop the redundant classifier or explicitly build a system-allocator variant?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0692e4d. The release now builds one native library per architecture, with JEMALLOC_SYS_WITH_LG_PAGE=12 exported before the build, and publishes one jar per Spark and Scala combination. The jemalloc classifier jar would only duplicate the artifact, so it is gone, along with the unreleased jemalloc Maven profile. check_native_libs still checks that each jar holds the libraries the script built, and the snapshot workflow pins the page size the same way. Users on 64 KiB-page kernels build from source, as the FAQ and source.md say.

Comment thread native/core/Cargo.toml
[features]
backtrace = ["datafusion/backtrace"]
default = ["hdfs-opendal", "delta"]
default = ["hdfs-opendal", "delta", "jemalloc"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documented macOS command, make release COMET_FEATURES=mimalloc, now enables both features. Make only checks the explicit flags, so it misses the conflict and the Rust cfg silently selects System. Can we preserve the explicit mimalloc choice and test the selected backend? For example, that Make path could pass --no-default-features --features hdfs-opendal,delta,mimalloc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0692e4d. lib.rs now selects mimalloc whenever its feature is on, so make release COMET_FEATURES=mimalloc and a plain cargo build --features mimalloc both get mimalloc while jemalloc stays a default feature. jni_api.rs reads jemalloc's stats only when jemalloc is the selected backend. The new selects_the_requested_allocator_backend test asserts the backend, and the rust-test action now lints and runs the alloc_accounting tests over the system allocator (--no-default-features --features hdfs-opendal,delta) and over mimalloc. The Makefile's jemalloc and mimalloc conflict check is removed, since that combination now resolves to mimalloc.

Comment thread docs/source/about/faq.md

The jars with the `jemalloc` classifier are built for 4 KiB memory pages and fail on arm64 kernels that use
64 KiB pages, such as the kernel of RHEL 8. On those machines, use the jar with the system allocator or
The jars use the jemalloc allocator, built for 4 KiB memory pages, and fail on arm64 kernels that

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the default jar now ships the statically linked jemalloc library, can we include its copyright, conditions, and disclaimer in dev/licenses/LICENSE-binary.txt? Clause 2 covers binary redistribution. Maven packages that file as META-INF/LICENSE, and it currently has no jemalloc entry. The existing jar contents check could cover it alongside zstd.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0692e4d. LICENSE-binary.txt now has jemalloc 5.3.0's copyright notices, conditions and disclaimer. tikv-jemalloc-sys 0.6.1 vendors 5.3.0 plus one build-script commit, and its COPYING matches the one at 5.3.0. ensure-jars-have-correct-contents.sh reads the vendored version from tikv-jemalloc-sys in Cargo.lock and fails if the LICENSE names another one. test-jar-contents.py covers version drift, an unreadable version and a missing copyright line.

Add jemalloc to the default Cargo features of native/core, so cargo
builds, the make targets, CI and the release builds use jemalloc.
An explicit mimalloc request now wins over the default jemalloc.

The release builds one native library per architecture, with the
jemalloc page size pinned to 4 KiB, and publishes one jar per Spark and
Scala combination. The unreleased jemalloc classifier jar and jemalloc
Maven profile are removed. The binary LICENSE includes jemalloc, and the
jar contents check verifies it.

Document jemalloc as the default, why, and how to build with the system
allocator.
@andygrove

Copy link
Copy Markdown
Member

LGTM. Thanks @comphead for addressing the release script, mimalloc and license feedback.

@comphead
comphead added this pull request to the merge queue Dec 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Dec 3, 2026
@comphead
comphead added this pull request to the merge queue Dec 4, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Dec 4, 2026
@comphead
comphead added this pull request to the merge queue Dec 4, 2026
Merged via the queue into apache:main with commit 05c92fb Dec 5, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants