Offer a mimalloc build of the runtime (cargo feature, off by default)

What was wrong: nothing by default. The runtime allocates through the system
heap, and some builds want to compare another allocator on the CPU-bound
translation threads.

What changes: a cargo feature `mimalloc` on the leonida app (-> leonida-hle
-> leonida-gpu, optional workspace dependency mimalloc 0.1.52, no default
features) installs mimalloc as the global allocator. With diagnostics on
Windows, the counting allocator in copystat wraps mimalloc instead of the
system heap, so the allocation counters keep working. Default off: with the
feature off the code is unchanged (the system allocator, same counters), and
the only difference in the tree is the unused optional dependency in
Cargo.lock. How much mimalloc helps is not measured here. Only the allocator
part of the fork commit is ported, not its descriptor-count reporting.

Source: from TaynanGT/Vice-Tay dfdb8b4, GPL-3.0 (allocator part only).

Tests: cargo check -p leonida --features diagnostics (default) and
--features diagnostics,mimalloc; cargo check -p leonida-gpu --features
mimalloc. Not run on the game. AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NotRexed
2026-11-27 01:16:38 -05:00
parent c1445a1076
commit 130ada08ba
7 changed files with 44 additions and 6 deletions

19
d3d12/Cargo.lock generated
View File

@ -318,6 +318,7 @@ dependencies = [
"leonida-menu",
"leonida-pipeline",
"leonida-shader",
"mimalloc",
"tempfile",
"tracing",
"windows",
@ -411,6 +412,15 @@ version = "0.2.191"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
[[package]]
name = "libmimalloc-sys"
version = "0.1.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a45a52f43e1c16f667ccfe4dd8c85b7f7c204fd5e3bf46c5b0db9a5c3c0b8e9"
dependencies = [
"cc",
]
[[package]]
name = "linux-raw-sys"
version = "0.12.1"
@ -472,6 +482,15 @@ version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
[[package]]
name = "mimalloc"
version = "0.1.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d4139bb28d14ad1facf21d5eb8825051b326e172d216b39f6d31df53cc97862"
dependencies = [
"libmimalloc-sys",
]
[[package]]
name = "mlua"
version = "0.12.3"

View File

@ -39,6 +39,7 @@ windows = "0.62.2"
windows-core = "0.62.2"
blake3 = "1.8.7"
memchr = "2.8.3"
mimalloc = { version = "0.1.52", default-features = false }
# SIMD-accelerated pure-Rust zlib/deflate (Zlib license, runtime AVX2/SSE4.2 dispatch).
zlib-rs = { version = "0.6.8", default-features = false, features = ["std"] }
walkdir = "2.5.1"

View File

@ -34,5 +34,8 @@ diagnostics = [
"leonida-shader/diagnostics",
]
# mimalloc as the process allocator (off by default: the system allocator).
mimalloc = ["leonida-hle/mimalloc"]
[lints]
workspace = true

View File

@ -9,6 +9,7 @@ publish.workspace = true
[dependencies]
blake3.workspace = true
mimalloc = { workspace = true, optional = true }
zlib-rs.workspace = true
tracing.workspace = true
anyhow.workspace = true
@ -30,6 +31,8 @@ diagnostics = []
# NVIDIA DLSS for the title's upscale (`LEONIDA_UPSCALE=dlss`); needs the DLSS
# SDK (NVIDIA/DLSS: include, lib/Windows_x86_64) at `LEONIDA_DLSS_SDK`.
dlss = []
# mimalloc as the process allocator (off by default: the system allocator).
mimalloc = ["dep:mimalloc"]
[lints]
workspace = true

View File

@ -308,7 +308,7 @@ pub(crate) fn range(src: usize, len: usize, us: u64, kind: u32) {
/// Our own allocations alive now, by size of block (under 4 KiB, to 64 KiB,
/// to 1 MiB, larger): bytes, then blocks. "Our own" is the Rust code of
/// every crate in the process: in diagnostics builds [`Counting`] stands in
/// front of the system's heap. The memory map's "process heap segments"
/// front of the selected allocator (the system heap unless built with `mimalloc`). The memory map's "process heap segments"
/// hold the blocks under about half a megabyte of ours, of the graphics
/// runtime and of the driver alike (3.5 GB in run 60's gameplay, growing by
/// some 2 MB a frame, and nothing said whose): this says which part is
@ -383,12 +383,17 @@ fn big_blocks() -> String {
/// additions per allocation and per release: diagnostics builds only).
struct Counting;
// SAFETY: every call goes to the system's allocator unchanged; only
#[cfg(feature = "mimalloc")]
const INNER: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[cfg(not(feature = "mimalloc"))]
const INNER: std::alloc::System = std::alloc::System;
// SAFETY: every call goes to the selected allocator unchanged; only
// counters are kept beside it.
unsafe impl std::alloc::GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 {
// SAFETY: the caller's contract, passed on.
let at = unsafe { std::alloc::System.alloc(layout) };
let at = unsafe { INNER.alloc(layout) };
if !at.is_null() {
own(layout.size(), true);
big(at, layout.size(), true);
@ -398,7 +403,7 @@ unsafe impl std::alloc::GlobalAlloc for Counting {
unsafe fn alloc_zeroed(&self, layout: std::alloc::Layout) -> *mut u8 {
// SAFETY: the caller's contract, passed on.
let at = unsafe { std::alloc::System.alloc_zeroed(layout) };
let at = unsafe { INNER.alloc_zeroed(layout) };
if !at.is_null() {
own(layout.size(), true);
big(at, layout.size(), true);
@ -410,12 +415,12 @@ unsafe impl std::alloc::GlobalAlloc for Counting {
own(layout.size(), false);
big(at, layout.size(), false);
// SAFETY: the caller's contract, passed on.
unsafe { std::alloc::System.dealloc(at, layout) };
unsafe { INNER.dealloc(at, layout) };
}
unsafe fn realloc(&self, at: *mut u8, layout: std::alloc::Layout, size: usize) -> *mut u8 {
// SAFETY: the caller's contract, passed on.
let moved = unsafe { std::alloc::System.realloc(at, layout, size) };
let moved = unsafe { INNER.realloc(at, layout, size) };
if !moved.is_null() {
own(layout.size(), false);
own(size, true);

View File

@ -15,6 +15,12 @@
//! share, [`console`] which console model the title sees. Planned:
//! `present` (swap chain).
// With `mimalloc` the process allocates through mimalloc. On Windows with
// diagnostics, copystat's counting allocator wraps the same backend instead.
#[cfg(all(feature = "mimalloc", not(all(windows, feature = "diagnostics"))))]
#[global_allocator]
static HEAP: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[cfg(windows)]
mod ambient;
#[cfg(all(windows, feature = "diagnostics"))]

View File

@ -55,6 +55,7 @@ tempfile.workspace = true
[features]
# Debugging switches (scripted keys and the like).
diagnostics = ["leonida-gpu/diagnostics", "leonida-loader/diagnostics", "dep:windows", "dep:windows-core"]
mimalloc = ["leonida-gpu/mimalloc"]
[lints]
workspace = true