From 05a7927efdd8401b50f6bf5b3db72fad914ebcfe Mon Sep 17 00:00:00 2001 From: Nghia Date: Tue, 3 Dec 2024 15:50:37 +0100 Subject: [PATCH] backend: use mimalloc and set nodelay to increase performance (#542) --- Cargo.lock | 20 ++++++++++++++++++++ nghe-backend/Cargo.toml | 1 + nghe-backend/src/lib.rs | 4 ++++ nghe-backend/src/main.rs | 2 +- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 757c12721..f2dc3ef88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2411,6 +2411,16 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "libmimalloc-sys" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23aa6811d3bd4deb8a84dde645f943476d13b248d818edcf8ce0b2f37f036b44" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "libz-sys" version = "1.1.20" @@ -2565,6 +2575,15 @@ dependencies = [ "quote", ] +[[package]] +name = "mimalloc" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68914350ae34959d83f732418d51e2427a794055d0b9529f48259ac07af65633" +dependencies = [ + "libmimalloc-sys", +] + [[package]] name = "mime" version = "0.3.17" @@ -2702,6 +2721,7 @@ dependencies = [ "libaes", "lofty", "loole", + "mimalloc", "nghe_api", "nghe_proc_macro", "o2o", diff --git a/nghe-backend/Cargo.toml b/nghe-backend/Cargo.toml index 457b6be2f..87a2713ce 100644 --- a/nghe-backend/Cargo.toml +++ b/nghe-backend/Cargo.toml @@ -64,6 +64,7 @@ indexmap = { version = "2.6.0" } libaes = { version = "0.7.0" } lofty = { version = "0.21.1" } loole = { version = "0.4.0" } +mimalloc = { version = "0.1.43" } o2o = { version = "0.5.1-beta1", default-features = false, features = ["syn2"] } rsmpeg = { version = "0.15.1", default-features = false, features = [ "ffmpeg7", diff --git a/nghe-backend/src/lib.rs b/nghe-backend/src/lib.rs index f2743adda..57a877136 100644 --- a/nghe-backend/src/lib.rs +++ b/nghe-backend/src/lib.rs @@ -34,10 +34,14 @@ use axum::body::Body; use axum::http::Request; use axum::Router; use error::Error; +use mimalloc::MiMalloc; use tower_http::compression::CompressionLayer; use tower_http::cors::CorsLayer; use tower_http::trace::TraceLayer; +#[global_allocator] +static GLOBAL: MiMalloc = MiMalloc; + pub async fn build(config: config::Config) -> Router { let filesystem = filesystem::Filesystem::new(&config.filesystem.tls, &config.filesystem.s3).await; diff --git a/nghe-backend/src/main.rs b/nghe-backend/src/main.rs index 708cee7b8..9b58b06a7 100644 --- a/nghe-backend/src/main.rs +++ b/nghe-backend/src/main.rs @@ -22,5 +22,5 @@ async fn main() { migration::run(&config.database.url).await; let listener = tokio::net::TcpListener::bind(config.server.to_socket_addr()).await.unwrap(); - axum::serve(listener, build(config).await).await.unwrap(); + axum::serve(listener, build(config).await).tcp_nodelay(true).await.unwrap(); }