Skip to content

Commit 9147904

Browse files
committed
support mimalloc
1 parent 6bb2d3f commit 9147904

26 files changed

Lines changed: 135 additions & 22 deletions

File tree

Cargo.lock

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ members = [
4444
"lib/vey-journal",
4545
"lib/vey-json",
4646
"lib/vey-macros",
47+
"lib/vey-mimalloc",
4748
"lib/vey-msgpack",
4849
"lib/vey-openssl",
4950
"lib/vey-redis-client",
@@ -227,6 +228,7 @@ vey-jemalloc = { version = "0.1", path = "lib/vey-jemalloc" }
227228
vey-journal = { version = "0.4", path = "lib/vey-journal" }
228229
vey-json = { version = "0.5", path = "lib/vey-json" }
229230
vey-macros = { version = "0.1", path = "lib/vey-macros" }
231+
vey-mimalloc = { version = "0.1", path = "lib/vey-mimalloc" }
230232
vey-msgpack = { version = "0.4", path = "lib/vey-msgpack" }
231233
vey-openssl = { version = "0.4", path = "lib/vey-openssl" }
232234
vey-redis-client = { version = "0.3", path = "lib/vey-redis-client" }

lib/vey-mimalloc/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "vey-mimalloc"
3+
version = "0.1.0"
4+
license.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
8+
[dependencies]
9+
mimalloc-sys = { package = "vey-mimalloc-sys", version = "0.1" }

lib/vey-mimalloc/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* SPDX-FileCopyrightText: 2026 VEY-OSS Developers.
4+
*/
5+
6+
use core::alloc::{GlobalAlloc, Layout};
7+
8+
mod version;
9+
pub use version::lib_version;
10+
11+
pub struct Mimalloc;
12+
13+
unsafe impl GlobalAlloc for Mimalloc {
14+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
15+
unsafe { mimalloc_sys::mi_aligned_alloc(layout.align(), layout.size()) as _ }
16+
}
17+
18+
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
19+
unsafe { mimalloc_sys::mi_free_size_aligned(ptr as _, layout.size(), layout.align()) as _ }
20+
}
21+
22+
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
23+
unsafe { mimalloc_sys::mi_zalloc_aligned(layout.size(), layout.align()) as _ }
24+
}
25+
26+
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
27+
unsafe { mimalloc_sys::mi_realloc_aligned(ptr as _, new_size, layout.align()) as _ }
28+
}
29+
}

lib/vey-mimalloc/src/version.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* SPDX-FileCopyrightText: 2026 VEY-OSS Developers.
4+
*/
5+
6+
pub fn lib_version() -> i32 {
7+
unsafe { mimalloc_sys::mi_version() }
8+
}

vey-bench/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ vey-openssl.workspace = true
5656
vey-hickory-client.workspace = true
5757
vey-rustls-provider.workspace = true
5858
vey-jemalloc = { workspace = true, optional = true }
59+
vey-mimalloc = { workspace = true, optional = true }
5960

6061
[build-dependencies]
6162
vey-build-env.workspace = true
6263

6364
[features]
6465
default = ["quic", "rustls-ring"]
6566
jemalloc = ["dep:vey-jemalloc"]
67+
mimalloc = ["dep:vey-mimalloc"]
6668
quic = ["vey-types/quic", "vey-socks/quic", "vey-io-ext/quic", "vey-hickory-client/quic", "dep:quinn", "dep:h3", "dep:h3-quinn"]
6769
rustls-ring = ["vey-rustls-provider/rustls-ring", "vey-types/rustls-ring", "quinn?/rustls-ring"]
6870
rustls-aws-lc = ["vey-rustls-provider/rustls-aws-lc", "vey-types/rustls-aws-lc", "quinn?/rustls-aws-lc-rs"]

vey-bench/src/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub fn print_version() {
2626
if let Some(version) = vey_jemalloc::lib_version() {
2727
print!(" jemalloc({})", version.to_string_lossy());
2828
}
29+
#[cfg(feature = "mimalloc")]
30+
println!(" mimalloc({})", vey_mimalloc::lib_version());
2931
if let Some(quic) = QUIC_FEATURE {
3032
print!(" {quic}");
3133
}

vey-bench/src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ use anyhow::{Context, anyhow};
1212
use clap::{Arg, ArgMatches, Command, value_parser};
1313
use clap_complete::Shell;
1414

15-
#[cfg(feature = "jemalloc")]
16-
use vey_jemalloc::Jemalloc;
17-
1815
const COMMAND_VERSION: &str = "version";
1916
const COMMAND_COMPLETION: &str = "completion";
2017

2118
#[cfg(feature = "jemalloc")]
2219
#[global_allocator]
23-
static GLOBAL: Jemalloc = Jemalloc;
20+
static GLOBAL: vey_jemalloc::Jemalloc = vey_jemalloc::Jemalloc;
21+
22+
#[cfg(feature = "mimalloc")]
23+
#[global_allocator]
24+
static GLOBAL: vey_mimalloc::Mimalloc = vey_mimalloc::Mimalloc;
2425

2526
fn build_cli_args() -> Command {
2627
vey_bench::add_global_args(Command::new(vey_bench::build::PKG_NAME))

vey-dcgen/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ vey-tls-cert.workspace = true
2727
vey-cert-agent.workspace = true
2828
vey-socket.workspace = true
2929
vey-jemalloc = { workspace = true, optional = true }
30+
vey-mimalloc = { workspace = true, optional = true }
3031

3132
[build-dependencies]
3233
vey-build-env.workspace = true
3334

3435
[features]
3536
default = []
3637
jemalloc = ["dep:vey-jemalloc", "vey-daemon/jemalloc"]
38+
mimalloc = ["dep:vey-mimalloc"]
3739
vendored-openssl = ["openssl/vendored"]
3840
vendored-tongsuo = ["openssl/tongsuo", "vey-cert-agent/tongsuo"]
3941
vendored-boringssl = ["openssl/boringssl"]

vey-dcgen/src/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub fn print_version(verbose_level: u8) {
2727
if let Some(version) = vey_jemalloc::lib_version() {
2828
print!(" jemalloc({})", version.to_string_lossy());
2929
}
30+
#[cfg(feature = "mimalloc")]
31+
println!(" mimalloc({})", vey_mimalloc::lib_version());
3032
println!();
3133
if let Some(variant) = OPENSSL_VARIANT {
3234
println!("OpenSSL Variant: {variant}");

0 commit comments

Comments
 (0)