Sfoglia il codice sorgente

perf: cache `nr_cpus` in a thread_local

Vladimir Petrzhikovskii 6 mesi fa
parent
commit
d05110fd86
3 ha cambiato i file con 11 aggiunte e 1 eliminazioni
  1. 1 0
      Cargo.toml
  2. 1 0
      aya/Cargo.toml
  3. 9 1
      aya/src/util.rs

+ 1 - 0
Cargo.toml

@@ -78,6 +78,7 @@ netns-rs = { version = "0.1", default-features = false }
 nix = { version = "0.29.0", default-features = false }
 num_enum = { version = "0.7", 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-macro2 = { version = "1", default-features = false }
 public-api = { version = "0.38.0", default-features = false }

+ 1 - 0
aya/Cargo.toml

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

+ 9 - 1
aya/src/util.rs

@@ -196,7 +196,15 @@ pub fn online_cpus() -> Result<Vec<u32>, (&'static str, io::Error)> {
 ///
 /// See `/sys/devices/system/cpu/possible`.
 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)> {