Skip to content

Commit 3ec6a22

Browse files
committed
feat: 支持通过mimalloc feature开启使用第三方内存分配器mimalloc; #275
1 parent 99188b1 commit 3ec6a22

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ path = "src/main.rs"
4242
[features]
4343
default = []
4444
debug=[]
45+
mimalloc = ["dep:mimalloc"]
4546

4647
[lints.clippy]
4748
uninlined_format_args = "allow"
4849

4950
[dependencies]
50-
mimalloc = { version = "0.1", features = ["secure"] }
51+
mimalloc = { version = "0.1", features = ["secure"], optional = true }
5152
serde = { version = "1", features = ["derive", "rc"] }
5253
serde_json = "1"
5354
serde_urlencoded = "0.7"

src/main.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ use std::sync::Arc;
3131
use std::time::Duration;
3232
use tonic::transport::Server;
3333

34+
use crate::cli::{Cli, Commands};
3435
use actix_web::{middleware, HttpServer};
3536
use clap::Parser;
3637
use env_logger::TimestampPrecision;
3738
use env_logger_timezone_fmt::{TimeZoneFormat, TimeZoneFormatEnv};
38-
//use mimalloc::MiMalloc;
39-
use crate::cli::{Cli, Commands};
39+
#[cfg(feature = "mimalloc")]
40+
use mimalloc::MiMalloc;
4041
use rnacos::common::appdata::AppShareData;
4142
use rnacos::openapi::middle::auth_middle::ApiCheckAuth;
4243
use rnacos::raft::NacosRaft;
@@ -45,8 +46,9 @@ use rnacos::transfer::mysql_to_data::mysql_to_data;
4546
use rnacos::transfer::openapi_to_data::openapi_to_data;
4647
use rnacos::transfer::sqlite_to_data::sqlite_to_data;
4748
use rnacos::web_config::{app_config, console_config};
48-
//#[global_allocator]
49-
//static GLOBAL: MiMalloc = MiMalloc;
49+
#[cfg(feature = "mimalloc")]
50+
#[global_allocator]
51+
static GLOBAL: MiMalloc = MiMalloc;
5052

5153
#[actix_web::main]
5254
async fn main() -> Result<(), Box<dyn Error>> {
@@ -68,6 +70,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
6870
// 这里不使用log:info避免日志等级高于info时不打印
6971
println!("version:{}, RUST_LOG:{}", get_app_version(), &rust_log);
7072
println!("data dir:{}", sys_config.local_db_dir);
73+
74+
// 输出内存分配器信息
75+
let allocator_name = if cfg!(feature = "mimalloc") {
76+
"mimalloc"
77+
} else {
78+
"system default"
79+
};
80+
println!("allocator: {}", allocator_name);
7181
let factory_data = config_factory(sys_config.clone()).await?;
7282
let app_data = build_share_data(factory_data.clone())?;
7383
let http_addr = sys_config.get_http_addr();

0 commit comments

Comments
 (0)