From f3eaa288b6afbb0ea5ad60b21238e950122a1766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sun, 15 Oct 2023 13:46:57 +0900 Subject: [PATCH] perf: use mimalloc for bindings_napi (#5201) --- rust/Cargo.lock | 27 +++++++++++++++++++++++++++ rust/bindings_napi/Cargo.toml | 6 ++++++ rust/bindings_napi/src/lib.rs | 7 +++++++ 3 files changed, 40 insertions(+) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index b3b07ca1482..c79015fa9a8 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -158,6 +158,7 @@ dependencies = [ name = "bindings_napi" version = "0.0.0" dependencies = [ + "mimalloc-rust", "napi", "napi-build", "napi-derive", @@ -333,6 +334,12 @@ dependencies = [ "syn 2.0.38", ] +[[package]] +name = "cty" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" + [[package]] name = "dashmap" version = "5.5.3" @@ -681,6 +688,26 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "mimalloc-rust" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5eb726c8298efb4010b2c46d8050e4be36cf807b9d9e98cb112f830914fc9bbe" +dependencies = [ + "cty", + "mimalloc-rust-sys", +] + +[[package]] +name = "mimalloc-rust-sys" +version = "1.7.9-source" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6413e13241a9809f291568133eca6694572cf528c1a6175502d090adce5dd5db" +dependencies = [ + "cc", + "cty", +] + [[package]] name = "minimal-lexical" version = "0.2.1" diff --git a/rust/bindings_napi/Cargo.toml b/rust/bindings_napi/Cargo.toml index 4ce67cd3c0c..bf55c38d3d3 100644 --- a/rust/bindings_napi/Cargo.toml +++ b/rust/bindings_napi/Cargo.toml @@ -15,5 +15,11 @@ napi-derive = "2.13.0" parse_ast = { path = "../parse_ast" } xxhash = { path = "../xxhash" } +[target.'cfg(not(target_os = "linux"))'.dependencies] +mimalloc-rust = { version = "0.2" } + +[target.'cfg(all(target_os = "linux", not(all(target_env = "musl", target_arch = "aarch64"))))'.dependencies] +mimalloc-rust = { version = "0.2", features = ["local-dynamic-tls"] } + [build-dependencies] napi-build = "2.0.1" diff --git a/rust/bindings_napi/src/lib.rs b/rust/bindings_napi/src/lib.rs index 261e8224221..ce372cdacbc 100644 --- a/rust/bindings_napi/src/lib.rs +++ b/rust/bindings_napi/src/lib.rs @@ -2,6 +2,13 @@ use napi::bindgen_prelude::*; use napi_derive::napi; use parse_ast::parse_ast; +#[cfg(all( + not(all(target_os = "linux", target_env = "musl", target_arch = "aarch64")), + not(debug_assertions) +))] +#[global_allocator] +static ALLOC: mimalloc_rust::GlobalMiMalloc = mimalloc_rust::GlobalMiMalloc; + #[napi] pub fn parse(code: String, allow_return_outside_function: bool) -> Buffer { parse_ast(code, allow_return_outside_function).into()