From 664f9d4fbe2d094936dcca1b2db111c24cef0661 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 4 Dec 2023 12:37:32 +0000 Subject: [PATCH] Use specific memory allocators depending on OS (#3061) --- Cargo.lock | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 6 ++++++ src/main.rs | 1 + src/mem/mod.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 src/mem/mod.rs diff --git a/Cargo.lock b/Cargo.lock index b732e7c9..0ca0be50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1206,6 +1206,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "cmake" +version = "0.1.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31c789563b815f77f4250caee12365734369f942439b7defd71e18a48197130" +dependencies = [ + "cc", +] + [[package]] name = "colorchoice" version = "1.0.0" @@ -2635,6 +2644,26 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +[[package]] +name = "jemalloc-sys" +version = "0.5.4+5.3.0-patched" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac6c1946e1cea1788cbfde01c993b52a10e2da07f4bac608228d1bed20bfebf2" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "jemallocator" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0de374a9f8e63150e6f5e8a60cc14c668226d7a347d8aee1a45766e3c4dd3bc" +dependencies = [ + "jemalloc-sys", + "libc", +] + [[package]] name = "jobserver" version = "0.1.27" @@ -4791,6 +4820,24 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e9f0ab6ef7eb7353d9119c170a436d1bf248eea575ac42d19d12f4e34130831" +[[package]] +name = "snmalloc-rs" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038507ad9c0ff0d6901e057494abcdba49a1a44fe3236f281730a9278166710d" +dependencies = [ + "snmalloc-sys", +] + +[[package]] +name = "snmalloc-sys" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cae3f7f662ebf11afe4d6534e63fa5846ec0143892c78ddb1040cc01249f143" +dependencies = [ + "cmake", +] + [[package]] name = "socket2" version = "0.4.10" @@ -4941,6 +4988,7 @@ dependencies = [ "http-body", "hyper", "ipnet", + "jemallocator", "nix", "once_cell", "opentelemetry", @@ -4956,6 +5004,7 @@ dependencies = [ "serde_cbor", "serde_json", "serial_test", + "snmalloc-rs", "surrealdb", "temp-env", "tempfile", diff --git a/Cargo.toml b/Cargo.toml index 0659547b..7f9564d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,6 +76,12 @@ uuid = { version = "1.6.1", features = ["serde", "js", "v4", "v7"] } [target.'cfg(unix)'.dependencies] nix = "0.26.4" +[target.'cfg(any(target_os = "linux", target_os = "macos", target_os = "ios"))'.dependencies] +snmalloc-rs = "0.3.4" + +[target.'cfg(any(target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))'.dependencies] +jemallocator = "0.5.4" + [dev-dependencies] assert_fs = "1.0.13" env_logger = "0.10.1" diff --git a/src/main.rs b/src/main.rs index df896486..24364720 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,7 @@ mod cnf; mod dbs; mod env; mod err; +mod mem; #[cfg(feature = "has-storage")] mod net; #[cfg(feature = "has-storage")] diff --git a/src/mem/mod.rs b/src/mem/mod.rs new file mode 100644 index 00000000..73a05dbd --- /dev/null +++ b/src/mem/mod.rs @@ -0,0 +1,27 @@ +#[cfg(target_os = "android")] +#[global_allocator] +pub static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; + +#[cfg(target_os = "freebsd")] +#[global_allocator] +pub static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; + +#[cfg(target_os = "ios")] +#[global_allocator] +pub static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc; + +#[cfg(target_os = "linux")] +#[global_allocator] +pub static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc; + +#[cfg(target_os = "macos")] +#[global_allocator] +pub static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc; + +#[cfg(target_os = "netbsd")] +#[global_allocator] +pub static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; + +#[cfg(target_os = "openbsd")] +#[global_allocator] +pub static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;