From 807f77fb851c8323e3a3a80825514fd5c6a93cfb Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Mon, 12 Aug 2024 21:54:06 +0300 Subject: [PATCH] cpufeatures: use `#[cold]` for initialization code (#1096) --- Cargo.lock | 21 ++++++++++++++------- cpufeatures/Cargo.toml | 2 +- cpufeatures/src/lib.rs | 21 ++++++++++++--------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 35562a76..308907a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,7 +35,7 @@ version = "0.0.2" [[package]] name = "cpufeatures" -version = "0.2.12" +version = "0.2.13" dependencies = [ "libc", ] @@ -111,6 +111,12 @@ version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + [[package]] name = "opaque-debug" version = "0.3.1" @@ -151,18 +157,18 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "serde" -version = "1.0.203" +version = "1.0.206" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +checksum = "5b3e4cd94123dd520a128bcd11e34d9e9e423e7e3e50425cb1b4b1e3549d0284" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.203" +version = "1.0.206" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +checksum = "fabfb6138d2383ea8208cf98ccf69cdfb1aff4088460681d84189aa259762f97" dependencies = [ "proc-macro2", "quote", @@ -171,11 +177,12 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.117" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" +checksum = "66ad62847a56b3dba58cc891acd13884b9c61138d330c0d7b6181713d4fce38d" dependencies = [ "itoa", + "memchr", "ryu", "serde", ] diff --git a/cpufeatures/Cargo.toml b/cpufeatures/Cargo.toml index ff2c2329..a8af9ffa 100644 --- a/cpufeatures/Cargo.toml +++ b/cpufeatures/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cpufeatures" -version = "0.2.12" +version = "0.2.13" description = """ Lightweight runtime CPU feature detection for aarch64, loongarch64, and x86/x86_64 targets, with no_std support and support for mobile targets including Android and iOS diff --git a/cpufeatures/src/lib.rs b/cpufeatures/src/lib.rs index ba9aa25d..bc4afd9a 100644 --- a/cpufeatures/src/lib.rs +++ b/cpufeatures/src/lib.rs @@ -177,19 +177,24 @@ macro_rules! new { } } - /// Initialize underlying storage if needed and get - /// stored value and initialization token. + /// Get stored value and initialization token, + /// initializing underlying storage if needed. #[inline] pub fn init_get() -> (InitToken, bool) { let res = $crate::__unless_target_features! { $($tf),+ => { + #[cold] + fn init_inner() -> bool { + let res = $crate::__detect_target_features!($($tf),+); + STORAGE.store(res as u8, Relaxed); + res + } + // Relaxed ordering is fine, as we only have a single atomic variable. let val = STORAGE.load(Relaxed); if val == UNINIT { - let res = $crate::__detect_target_features!($($tf),+); - STORAGE.store(res as u8, Relaxed); - res + init_inner() } else { val == 1 } @@ -199,15 +204,13 @@ macro_rules! new { (InitToken(()), res) } - /// Initialize underlying storage if needed and get - /// initialization token. + /// Initialize underlying storage if needed and get initialization token. #[inline] pub fn init() -> InitToken { init_get().0 } - /// Initialize underlying storage if needed and get - /// stored value. + /// Initialize underlying storage if needed and get stored value. #[inline] pub fn get() -> bool { init_get().1