From 58359e6001150d9e3871309d5f433c360678b11a Mon Sep 17 00:00:00 2001 From: hatoo Date: Sat, 4 Jan 2025 19:40:23 +0900 Subject: [PATCH] Use physical cores because it's more performant empirically. --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 1 + src/main.rs | 18 ++++++++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fc6d2b89..e60b5e41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1443,6 +1443,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + [[package]] name = "hickory-proto" version = "0.24.1" @@ -2276,6 +2282,16 @@ dependencies = [ "libm", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "object" version = "0.36.5" @@ -2313,6 +2329,7 @@ dependencies = [ "lazy_static", "libc", "native-tls", + "num_cpus", "pin-project-lite", "predicates", "rand", diff --git a/Cargo.toml b/Cargo.toml index 7dc5481e..4238d9f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,6 +69,7 @@ http-body-util = "0.1.2" hyper-util = { version = "0.1.6", features = ["tokio"] } tokio-vsock = { version = "0.5.0", optional = true } rusqlite = { version = "0.32.0", features = ["bundled"] } +num_cpus = "1.16.0" [target.'cfg(unix)'.dependencies] rlimit = "0.10.1" diff --git a/src/main.rs b/src/main.rs index 3261e71b..fcc2469a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -297,8 +297,7 @@ impl FromStr for VsockAddr { } } -#[tokio::main] -async fn main() -> anyhow::Result<()> { +async fn run() -> anyhow::Result<()> { let mut opts: Opts = Opts::parse(); let parse_http_version = |is_http2: bool, version: Option<&str>| match (is_http2, version) { @@ -758,3 +757,18 @@ fn system_resolv_conf() -> anyhow::Result<(ResolverConfig, ResolverOpts)> { hickory_resolver::system_conf::read_system_conf() .context("DNS: failed to load /etc/resolv.conf") } + +fn main() { + let num_workers_threads = std::env::var("TOKIO_WORKER_THREADS") + .ok() + .and_then(|s| s.parse().ok()) + // Prefer to use physical cores rather than logical one because it's more performant empirically. + .unwrap_or(num_cpus::get_physical()); + + let rt = tokio::runtime::Builder::new_multi_thread() + .worker_threads(num_workers_threads) + .enable_all() + .build() + .unwrap(); + rt.block_on(run()).unwrap(); +}