From a206e557af47109ae7f907b89649da8a39fed932 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Sun, 24 May 2020 07:54:41 -0400 Subject: [PATCH] Use mimalloc What? ===== Rather than using Rust's built-in allocator, this changes to using mimalloc. In looking to squeeze out further performance, benchmarks across three private Rails repositories (of varying sizes) as well as Discourse cover Rust's built-in allocator, jemalloc, and mimalloc. In starting with jemallic, the benchmarks looked quite promising: | | Rust | jemalloc | speedup vs Rust | | Codebase 1 | 1389 | 906 | 53% | | Codebase 2 | 2172 | 1541 | 41% | | Codebase 3 | 59005 | 57340 | 3% | | discourse | 2092 | 1490 | 40% | All times are measured in milliseconds and benchmarks are run with [bench](https://hackage.haskell.org/package/bench) on release mode. mimalloc resulted in even further performance gains: | | Rust | mimalloc | speedup vs Rust | speedup vs jemalloc | | Codebase 1 | 1389 | 828 | 67% | 9% | | Codebase 2 | 2172 | 1264 | 72% | 22% | | Codebase 3 | 59005 | 40176 | 47% | 43% | | discourse | 2092 | 1403 | 49% | 6% | Across the board, mimalloc presented a ~45% to 70% speedup over the Rust allocator. In each of these samples, mimalloc also outperformed jemalloc, especially with larger codebases (either number of tokens or number of files). --- Cargo.lock | 34 ++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bin/unused.rs | 3 +++ 3 files changed, 38 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 47e5296..0a22903 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -79,6 +79,12 @@ dependencies = [ "constant_time_eq", ] +[[package]] +name = "cc" +version = "1.0.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bbb73db36c1246e9034e307d0fba23f9a2e251faa47ade70c1bd252220c8311" + [[package]] name = "cfg-if" version = "0.1.9" @@ -128,6 +134,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "cmake" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e56268c17a6248366d66d4a47a3381369d068cce8409bb1716ed77ea32163bb" +dependencies = [ + "cc", +] + [[package]] name = "codebase_files" version = "0.1.0" @@ -329,6 +344,15 @@ version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0" +[[package]] +name = "libmimalloc-sys" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a27252ec1d0c4e0dd6142cbc572da50b363ab56fc334f7aa8fadf295b2e24e74" +dependencies = [ + "cmake", +] + [[package]] name = "linked-hash-map" version = "0.5.2" @@ -356,6 +380,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "mimalloc" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c52de2069999f01bd26436564dbe7de3a87898feeb7a0d0ff9eb20a05bb7ca0" +dependencies = [ + "libmimalloc-sys", +] + [[package]] name = "nodrop" version = "0.1.14" @@ -723,6 +756,7 @@ version = "0.1.0" dependencies = [ "cli", "codebase_files", + "mimalloc", "read_ctags", "serde_json", "token_search", diff --git a/Cargo.toml b/Cargo.toml index 445888a..994c284 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ read_ctags = { path = "crates/read_ctags/" } token_search = { path = "crates/token_search/" } codebase_files = { path = "crates/codebase_files/" } cli = { path = "crates/cli/" } +mimalloc = { version = "*", default-features = false } [[bin]] name = "read-ctags-rs" diff --git a/src/bin/unused.rs b/src/bin/unused.rs index 3d2f7dd..b16dde4 100644 --- a/src/bin/unused.rs +++ b/src/bin/unused.rs @@ -1,3 +1,6 @@ +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + use cli; fn main() {