Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Enable THP by default #21829

Merged
merged 4 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .cargo/config.toml

This file was deleted.

23 changes: 17 additions & 6 deletions py-polars/polars/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import contextlib

with contextlib.suppress(ImportError): # Module not available when building docs
# ensure the object constructor is known by polars
# we set this once on import

# This must be done before importing the Polars Rust bindings.
# This must be done before importing the Polars Rust bindings, otherwise we
# might execute illegal instructions.
import polars._cpu_check

polars._cpu_check.check_cpu_flags()

# we also set other function pointers needed
# on the rust side. This function is highly
# We also configure the allocator before importing the Polars Rust bindings.
# See https://github.com/pola-rs/polars/issues/18088,
# https://github.com/pola-rs/polars/pull/21829.
import os
import sys

jemalloc_conf = "dirty_decay_ms:500,muzzy_decay_ms:-1"
if sys.platform == "linux":
# We only enable this on Linux, otherwise jemalloc gives warnings.
jemalloc_conf += ",thp:always,metadata_thp:always"
if override := os.environ.get("_RJEM_MALLOC_CONF"):
jemalloc_conf += "," + override
os.environ["_RJEM_MALLOC_CONF"] = jemalloc_conf

# Initialize polars on the rust side. This function is highly
# unsafe and should only be called once.
from polars.polars import __register_startup_deps

Expand Down
4 changes: 2 additions & 2 deletions py-polars/src/allocator.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#[global_allocator]
#[cfg(all(
not(allocator = "mimalloc"),
not(allocator = "default"),
target_family = "unix",
not(target_os = "emscripten"),
))]
#[global_allocator]
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;

#[global_allocator]
#[cfg(all(
not(allocator = "default"),
any(
Expand All @@ -16,6 +15,7 @@ static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
allocator = "mimalloc"
),
))]
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;

use std::alloc::Layout;
Expand Down