From 9085261a85e1020a2003a283a4f45fbd804d4dab Mon Sep 17 00:00:00 2001 From: Stephan Vedder Date: Mon, 21 Sep 2026 06:55:24 +0200 Subject: [PATCH] feat(mimalloc): add mimalloc as global allocator This is necessary to reduce memory-fragmentation and overalll ram usage in long running deployments --- CHANGELOG.md | 2 ++ Cargo.lock | 19 +++++++++++++++++++ Cargo.toml | 3 +++ Dockerfile | 4 ++-- Dockerfile-s3 | 2 +- src/main.rs | 4 ++++ 6 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84e52ef..b9365d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Optional `mimalloc` feature that uses mimalloc as the global allocator. Enabled by default in the Docker images to reduce memory growth in long-running deployments (#68) + ### Fixed ### Changed diff --git a/Cargo.lock b/Cargo.lock index af6d549..a0db6fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1720,6 +1720,7 @@ dependencies = [ "futures", "http-body-util", "image", + "mimalloc", "mime", "multer", "pin-project", @@ -3379,6 +3380,15 @@ dependencies = [ "cc", ] +[[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.11.0" @@ -3500,6 +3510,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 = "mime" version = "0.3.17" diff --git a/Cargo.toml b/Cargo.toml index 814dc4b..635c746 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,8 @@ readme = "README.md" [features] default = [] s3 = ["dep:aws-config", "dep:aws-sdk-s3", "dep:aws-credential-types"] +# Use mimalloc as the global allocator (reduces memory fragmentation in long-running deployments) +mimalloc = ["dep:mimalloc"] [dependencies] # DICOM processing @@ -53,6 +55,7 @@ multer = "3.1.0" pin-project = "1.1.10" image = { version = "0.25.8", features = ["png", "jpeg", "gif"] } http-body-util = "0.1.3" +mimalloc = { version = "0.1", optional = true } # S3 backend aws-config = { version = "1.8.14", features = ["behavior-version-latest"], optional = true } diff --git a/Dockerfile b/Dockerfile index b3f015c..63f7ac4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,10 +8,10 @@ RUN cargo chef prepare --recipe-path recipe.json FROM chef AS builder COPY --from=planner /app/recipe.json recipe.json -RUN cargo chef cook --release --recipe-path recipe.json +RUN cargo chef cook --release --features mimalloc --recipe-path recipe.json COPY . . -RUN cargo build --release --bin dicom-rst +RUN cargo build --release --features mimalloc --bin dicom-rst FROM gcr.io/distroless/cc-debian12 AS runtime COPY --from=builder /app/target/release/dicom-rst / diff --git a/Dockerfile-s3 b/Dockerfile-s3 index a793b32..bb1f4f5 100644 --- a/Dockerfile-s3 +++ b/Dockerfile-s3 @@ -5,7 +5,7 @@ WORKDIR /app COPY . . -RUN cargo build --release --locked --features s3 --bin dicom-rst +RUN cargo build --release --locked --features s3,mimalloc --bin dicom-rst FROM gcr.io/distroless/cc-debian12 AS runtime COPY --from=builder /app/target/release/dicom-rst / diff --git a/src/main.rs b/src/main.rs index 6c23b68..5d9b0cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,10 @@ use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::EnvFilter; +#[cfg(feature = "mimalloc")] +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + /// The implementation class UID for DICOM-RST. /// The UID is a randomly generated UUID represented as a single integer value under the 2.25 root. pub const IMPLEMENTATION_CLASS_UID: &str = "2.25.94508551356620097453554517680708411706";