Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux: replace default allocator with jemalloc #2882

Merged
merged 14 commits into from
Apr 18, 2023
9 changes: 9 additions & 0 deletions .changesets/maint_garypen_jemalloc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### use jemalloc on linux

Detailed memory investigations of the router in use have revealed that there is a significant amount of memory fragmentation when using the default allocator, glibc, on linux. Performance testing and flamegraph analysis suggests that jemalloc on linux can yield significant performance improvements. In our tests, this figure shows performance to be about 35% faster than the default allocator. The improvement in performance being due to less time spent managing memory fragmentation.

Not everyone will see a 35% performance improvement in this release of the router. Depending on your usage pattern, you may see more or less than this. If you see a regression, please file an issue with details.

We have no reason to believe that there are allocation problems on other platforms, so this change is confined to linux.

By [@garypen](https://github.com/garypen) in https://github.com/apollographql/router/pull/2882
21 changes: 21 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ dependencies = [
"test-log",
"test-span",
"thiserror",
"tikv-jemallocator",
"tokio",
"tokio-rustls",
"tokio-stream",
Expand Down Expand Up @@ -5871,6 +5872,26 @@ dependencies = [
"tower",
]

[[package]]
name = "tikv-jemalloc-sys"
version = "0.5.3+5.3.0-patched"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a678df20055b43e57ef8cddde41cdfda9a3c1a060b67f4c5836dfb1d78543ba8"
dependencies = [
"cc",
"libc",
]

[[package]]
name = "tikv-jemallocator"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20612db8a13a6c06d57ec83953694185a367e16945f66565e8028d2c0bd76979"
dependencies = [
"libc",
"tikv-jemalloc-sys",
]

[[package]]
name = "time"
version = "0.3.20"
Expand Down
2 changes: 2 additions & 0 deletions apollo-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ uname = "0.1.1"

[target.'cfg(unix)'.dependencies]
uname = "0.1.1"
tikv-jemallocator = "0.5"

[dev-dependencies]
ecdsa = { version = "0.15.1", features = ["signing", "pem", "pkcs8"] }
Expand Down Expand Up @@ -256,3 +257,4 @@ tonic-build = "0.8.4"
[[test]]
name = "integration_tests"
path = "tests/integration_tests.rs"

10 changes: 10 additions & 0 deletions apollo-router/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
//! Main entry point for CLI command to start server.
// Note: We want to use jemalloc on linux, but we don't enable it if dhat-heap is in use because we
// can only have one global allocator
#[cfg(target_os = "linux")]
#[cfg(not(feature = "dhat-heap"))]
use tikv_jemallocator::Jemalloc;

#[cfg(target_os = "linux")]
#[cfg(not(feature = "dhat-heap"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

fn main() {
match apollo_router::main() {
Expand Down