perf: change memory allocator to mimalloc

This commit is contained in:
xfy
2025-03-27 09:23:24 +08:00
parent b38de3a325
commit c0a4fbfc19
5 changed files with 108 additions and 50 deletions

90
Cargo.lock generated
View File

@ -93,15 +93,15 @@ dependencies = [
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.96" version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b964d184e89d9b6b67dd2715bc8e74cf3107fb2b529990c90cf517326150bf4" checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f"
[[package]] [[package]]
name = "async-compression" name = "async-compression"
version = "0.4.18" version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df895a515f70646414f4b45c0b79082783b80552b373a68283012928df56f522" checksum = "59a194f9d963d8099596278594b3107448656ba73831c9d8c783e613ce86da64"
dependencies = [ dependencies = [
"brotli", "brotli",
"flate2", "flate2",
@ -217,9 +217,9 @@ dependencies = [
[[package]] [[package]]
name = "bytes" name = "bytes"
version = "1.10.0" version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9" checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a"
[[package]] [[package]]
name = "candy" name = "candy"
@ -235,6 +235,7 @@ dependencies = [
"hyper", "hyper",
"hyper-rustls", "hyper-rustls",
"hyper-util", "hyper-util",
"mimalloc",
"rustls", "rustls",
"rustls-pemfile", "rustls-pemfile",
"serde", "serde",
@ -286,9 +287,9 @@ dependencies = [
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.5.31" version = "4.5.33"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "027bb0d98429ae334a8698531da7077bdf906419543a35a55c2cb1b66437d767" checksum = "e2c80cae4c3350dd8f1272c73e83baff9a6ba550b8bfbe651b3c45b78cd1751e"
dependencies = [ dependencies = [
"clap_builder", "clap_builder",
"clap_derive", "clap_derive",
@ -296,9 +297,9 @@ dependencies = [
[[package]] [[package]]
name = "clap_builder" name = "clap_builder"
version = "4.5.31" version = "4.5.33"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5589e0cba072e0f3d23791efac0fd8627b49c829c196a492e88168e6a669d863" checksum = "0123e386f691c90aa228219b5b1ee72d465e8e231c79e9c82324f016a62a741c"
dependencies = [ dependencies = [
"anstream", "anstream",
"anstyle", "anstyle",
@ -308,9 +309,9 @@ dependencies = [
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "4.5.28" version = "4.5.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf4ced95c6f4a675af3da73304b9ac4ed991640c36374e4b46795c49e17cf1ed" checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7"
dependencies = [ dependencies = [
"heck", "heck",
"proc-macro2", "proc-macro2",
@ -531,9 +532,9 @@ dependencies = [
[[package]] [[package]]
name = "http" name = "http"
version = "1.2.0" version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f16ca2af56261c99fba8bac40a10251ce8188205a4c448fbb745a2e4daa76fea" checksum = "f4a85d31aea989eead29a3aaf9e1115a180df8282431156e533de47660892565"
dependencies = [ dependencies = [
"bytes", "bytes",
"fnv", "fnv",
@ -552,12 +553,12 @@ dependencies = [
[[package]] [[package]]
name = "http-body-util" name = "http-body-util"
version = "0.1.2" version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a"
dependencies = [ dependencies = [
"bytes", "bytes",
"futures-util", "futures-core",
"http", "http",
"http-body", "http-body",
"pin-project-lite", "pin-project-lite",
@ -702,6 +703,16 @@ dependencies = [
"windows-targets", "windows-targets",
] ]
[[package]]
name = "libmimalloc-sys"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07d0e07885d6a754b9c7993f2625187ad694ee985d60f23355ff0e7077261502"
dependencies = [
"cc",
"libc",
]
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.15" version = "0.4.15"
@ -739,6 +750,15 @@ version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "mimalloc"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99585191385958383e13f6b822e6b6d8d9cf928e7d286ceb092da92b43c87bc1"
dependencies = [
"libmimalloc-sys",
]
[[package]] [[package]]
name = "minimal-lexical" name = "minimal-lexical"
version = "0.2.1" version = "0.2.1"
@ -981,9 +1001,9 @@ dependencies = [
[[package]] [[package]]
name = "rustls" name = "rustls"
version = "0.23.23" version = "0.23.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47796c98c480fce5406ef69d1c76378375492c3b0a0de587be0c1d9feb12f395" checksum = "822ee9188ac4ec04a2f0531e55d035fb2de73f18b41a63c70c2712503b6fb13c"
dependencies = [ dependencies = [
"aws-lc-rs", "aws-lc-rs",
"log", "log",
@ -1024,9 +1044,9 @@ checksum = "917ce264624a4b4db1c364dcc35bfca9ded014d0a958cd47ad3e960e988ea51c"
[[package]] [[package]]
name = "rustls-webpki" name = "rustls-webpki"
version = "0.102.8" version = "0.103.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9" checksum = "fef8b8769aaccf73098557a87cd1816b4f9c7c16811c9c77142aa695c16f2c03"
dependencies = [ dependencies = [
"aws-lc-rs", "aws-lc-rs",
"ring", "ring",
@ -1074,18 +1094,18 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.218" version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60" checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.218" version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b" checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1175,18 +1195,18 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "2.0.11" version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc" checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "2.0.11" version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2" checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1205,9 +1225,9 @@ dependencies = [
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.43.0" version = "1.44.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e" checksum = "f382da615b842244d4b8738c82ed1275e6c5dd90c459a30941cd07080b06c91a"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
@ -1234,9 +1254,9 @@ dependencies = [
[[package]] [[package]]
name = "tokio-rustls" name = "tokio-rustls"
version = "0.26.1" version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f6d0975eaace0cf0fcadee4e4aaa5da15b5c079146f2cffb67c113be122bf37" checksum = "8e727b36a1a0e8b74c376ac2211e40c2c8af09fb4013c60d910495810f008e9b"
dependencies = [ dependencies = [
"rustls", "rustls",
"tokio", "tokio",
@ -1244,9 +1264,9 @@ dependencies = [
[[package]] [[package]]
name = "tokio-util" name = "tokio-util"
version = "0.7.13" version = "0.7.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078" checksum = "6b9590b93e6fcc1739458317cccd391ad3955e2bde8913edf6f95f9e65a8f034"
dependencies = [ dependencies = [
"bytes", "bytes",
"futures-core", "futures-core",

View File

@ -19,27 +19,28 @@ default = ["ring"]
[dependencies] [dependencies]
# core # core
tokio = { version = "1.43.0", features = ["full"] } tokio = { version = "1.44.1", features = ["full"] }
tokio-util = "0.7.13" tokio-util = "0.7.14"
hyper = { version = "1.6.0", features = ["full"] } hyper = { version = "1.6.0", features = ["full"] }
hyper-util = { version = "0.1.10", features = ["full"] } hyper-util = { version = "0.1.10", features = ["full"] }
http = "1.2.0" http = "1.3.1"
http-body-util = "0.1.2" http-body-util = "0.1.3"
futures-util = "0.3.31" futures-util = "0.3.31"
async-compression = { version = "0.4.18", features = [ async-compression = { version = "0.4.22", features = [
"tokio", "tokio",
"zstd", "zstd",
"gzip", "gzip",
"deflate", "deflate",
"brotli", "brotli",
] } ] }
mimalloc = "0.1.44"
# ssl # ssl
rustls = { version = "0.23.23", default-features = false, features = [ rustls = { version = "0.23.25", default-features = false, features = [
"logging", "logging",
"std", "std",
"tls12", "tls12",
] } ] }
tokio-rustls = { version = "0.26.1", default-features = false, features = [ tokio-rustls = { version = "0.26.2", default-features = false, features = [
"logging", "logging",
"tls12", "tls12",
] } ] }
@ -50,15 +51,15 @@ hyper-rustls = { version = "0.27.5", default-features = false, features = [
"http1", "http1",
] } ] }
# tools # tools
thiserror = "2.0.11" thiserror = "2.0.12"
anyhow = "1.0.96" anyhow = "1.0.97"
serde = { version = "1.0.218", features = ["derive"] } serde = { version = "1.0.219", features = ["derive"] }
toml = "0.8.20" toml = "0.8.20"
bytes = "1.10.0" bytes = "1.10.1"
# logging # logging
tracing = "0.1.41" tracing = "0.1.41"
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
clap = { version = "4.5.31", features = ["derive"] } clap = { version = "4.5.33", features = ["derive"] }
rustls-pemfile = "2.2.0" rustls-pemfile = "2.2.0"
[profile.dev] [profile.dev]

View File

@ -17,8 +17,10 @@ macro_rules! set_env {
fn main() { fn main() {
rustc_info(); rustc_info();
commit_info();
} }
/// Get rustc version info
fn rustc_info() { fn rustc_info() {
let rustc_output = Command::new("rustc") let rustc_output = Command::new("rustc")
.args(["-vV"]) .args(["-vV"])
@ -30,6 +32,34 @@ fn rustc_info() {
.split('\n') .split('\n')
.filter(|info| !info.is_empty()) .filter(|info| !info.is_empty())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
set_env!("RUA_COMPILER={}", info_arr[0]); set_env!("RUA_COMPILER={}", info_arr[0]);
} }
/// Get commit info
/// If failed, set RUA_COMMIT=unknown
fn commit_info() {
let output = Command::new("git").args(["rev-parse", "HEAD"]).output();
// get commit info
let Ok(output) = output else {
warn!("get commit info failed");
set_env!("RUA_COMMIT={}", "unknown");
return;
};
// check is dirty
let dirty = Command::new("git")
.args(["diff", "--quiet"])
.output()
.is_ok();
let commit = String::from_utf8_lossy(&output.stdout)
.chars()
.take(7)
.collect::<String>();
let commit = if commit.is_empty() {
"unknown".to_string()
} else if dirty {
format!("{commit}-dirty")
} else {
commit
};
set_env!("RUA_COMMIT={}", commit);
}

View File

@ -17,6 +17,7 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const OS: &str = env::consts::OS; pub const OS: &str = env::consts::OS;
pub const ARCH: &str = env::consts::ARCH; pub const ARCH: &str = env::consts::ARCH;
pub const COMPILER: &str = env!("RUA_COMPILER"); pub const COMPILER: &str = env!("RUA_COMPILER");
pub const COMMIT: &str = env!("RUA_COMMIT");
// config defaults // config defaults
pub const HOST_INDEX: [&str; 1] = ["index.html"]; pub const HOST_INDEX: [&str; 1] = ["index.html"];

View File

@ -2,7 +2,7 @@ use anyhow::{anyhow, Context, Result};
use clap::Parser; use clap::Parser;
use config::Settings; use config::Settings;
use consts::COMPILER; use consts::{COMMIT, COMPILER};
use tokio::task::JoinSet; use tokio::task::JoinSet;
use tracing::{debug, info}; use tracing::{debug, info};
@ -11,6 +11,11 @@ use crate::{
utils::init_logger, utils::init_logger,
}; };
use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
mod cli; mod cli;
mod config; mod config;
mod consts; mod consts;
@ -31,7 +36,8 @@ async fn main() -> Result<()> {
// global config // global config
let settings = get_settings().with_context(|| "get global settings failed")?; let settings = get_settings().with_context(|| "get global settings failed")?;
debug!("settings {:?}", settings); debug!("settings {:?}", settings);
info!("{}/{} {}", NAME, VERSION, COMPILER); info!("{}/{} {}", NAME, VERSION, COMMIT);
info!("{}", COMPILER);
info!("OS: {} {}", OS, ARCH); info!("OS: {} {}", OS, ARCH);
let mut servers = settings let mut servers = settings