perf: cache `nr_cpus` in a thread_local

reviewable/pr1038/r1
Vladimir Petrzhikovskii 4 months ago committed by Tamir Duberstein
parent afd777b705
commit d05110fd86

@ -78,6 +78,7 @@ netns-rs = { version = "0.1", default-features = false }
nix = { version = "0.29.0", default-features = false } nix = { version = "0.29.0", default-features = false }
num_enum = { version = "0.7", default-features = false } num_enum = { version = "0.7", default-features = false }
object = { version = "0.36", default-features = false } object = { version = "0.36", default-features = false }
once_cell = { version = "1.20.1", default-features = false }
proc-macro-error = { version = "1.0", default-features = false } proc-macro-error = { version = "1.0", default-features = false }
proc-macro2 = { version = "1", default-features = false } proc-macro2 = { version = "1", default-features = false }
public-api = { version = "0.38.0", default-features = false } public-api = { version = "0.38.0", default-features = false }

@ -21,6 +21,7 @@ bytes = { workspace = true }
libc = { workspace = true } libc = { workspace = true }
log = { workspace = true } log = { workspace = true }
object = { workspace = true, features = ["elf", "read_core", "std", "write"] } object = { workspace = true, features = ["elf", "read_core", "std", "write"] }
once_cell = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
tokio = { workspace = true, features = ["rt"], optional = true } tokio = { workspace = true, features = ["rt"], optional = true }

@ -196,7 +196,15 @@ pub fn online_cpus() -> Result<Vec<u32>, (&'static str, io::Error)> {
/// ///
/// See `/sys/devices/system/cpu/possible`. /// See `/sys/devices/system/cpu/possible`.
pub fn nr_cpus() -> Result<usize, (&'static str, io::Error)> { pub fn nr_cpus() -> Result<usize, (&'static str, io::Error)> {
read_cpu_ranges(POSSIBLE_CPUS).map(|cpus| cpus.len()) thread_local! {
// TODO(https://github.com/rust-lang/rust/issues/109737): Use
// `std::cell::OnceCell` when `get_or_try_init` is stabilized.
static CACHE: once_cell::unsync::OnceCell<usize> = const { once_cell::unsync::OnceCell::new() };
}
CACHE.with(|cell| {
cell.get_or_try_init(|| read_cpu_ranges(POSSIBLE_CPUS).map(|cpus| cpus.len()))
.copied()
})
} }
fn read_cpu_ranges(path: &'static str) -> Result<Vec<u32>, (&'static str, io::Error)> { fn read_cpu_ranges(path: &'static str) -> Result<Vec<u32>, (&'static str, io::Error)> {

Loading…
Cancel
Save