From eceb7f092e001a6dbe5bcf925b749d1e1424be12 Mon Sep 17 00:00:00 2001 From: Mostafa Abdelraouf Date: Thu, 13 Oct 2022 11:13:45 -0500 Subject: [PATCH] Use Jemalloc (#189) Jemalloc performs better than the standard allocator in various metrics (http://ithare.com/testing-memory-allocators-ptmalloc2-tcmalloc-hoard-jemalloc-while-trying-to-simulate-real-world-loads/). This PR makes changes to use Jemalloc as the global allocator for Pgcat. Windows is not officially supported by Pgcat but it should still compile but without Jemalloc as the allocator. --- Cargo.lock | 28 ++++++++++++++++++++++++++++ Cargo.toml | 3 +++ src/main.rs | 7 +++++++ 3 files changed, 38 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 2bced06..746cf32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -171,6 +171,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "fs_extra" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" + [[package]] name = "futures-channel" version = "0.3.19" @@ -365,6 +371,27 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" +[[package]] +name = "jemalloc-sys" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d3b9f3f5c9b31aa0f5ed3260385ac205db665baa41d49bb8338008ae94ede45" +dependencies = [ + "cc", + "fs_extra", + "libc", +] + +[[package]] +name = "jemallocator" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43ae63fcfc45e99ab3d1b29a46782ad679e98436c3169d15a167a1108a724b69" +dependencies = [ + "jemalloc-sys", + "libc", +] + [[package]] name = "js-sys" version = "0.3.58" @@ -547,6 +574,7 @@ dependencies = [ "exitcode", "hmac", "hyper", + "jemallocator", "log", "md-5", "num_cpus", diff --git a/Cargo.toml b/Cargo.toml index 48ba966..8284d2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,3 +34,6 @@ rustls-pemfile = "1" hyper = { version = "0.14", features = ["full"] } phf = { version = "0.11.1", features = ["macros"] } exitcode = "1.1.2" + +[target.'cfg(not(target_env = "msvc"))'.dependencies] +jemallocator = "0.3.2" diff --git a/src/main.rs b/src/main.rs index 467527c..75aab69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,13 @@ extern crate tokio; extern crate tokio_rustls; extern crate toml; +#[cfg(not(target_env = "msvc"))] +use jemallocator::Jemalloc; + +#[cfg(not(target_env = "msvc"))] +#[global_allocator] +static GLOBAL: Jemalloc = Jemalloc; + use log::{error, info, warn}; use parking_lot::Mutex; use pgcat::format_duration;