From a1d77233ae77f9f91183bd4b315b6e40d2dc9ed9 Mon Sep 17 00:00:00 2001 From: Evgeny Safronov Date: Mon, 13 Jul 2026 11:55:11 +0300 Subject: [PATCH] refactor: swap core::hint::cold_path for a stable #[cold] shim MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `core::hint::cold_path()` is stable only since Rust 1.95, which made it the single reason the library could not build on any earlier toolchain. It is replaced by a private empty `#[cold] #[inline(never)]` function: LLVM proves the empty body side-effect free and deletes the call, so all that survives is the block-placement hint the attribute carries — exactly what the intrinsic provides. The three call sites sit in the hot loops of the difference iterators, where the hint keeps the rare rescue scan off the loop-carried dependency chain, so the swap was verified by disassembly rather than by inspection: the emitted code for `Ipv4NetworkDiff::next`/`next_back` and `Ipv6NetworkDiff::next`/ `next_back` is byte-identical before and after, under both baseline x86-64 and `-C target-cpu=native`. The only change to the object file is the (never called) stub itself. With this the library compiles on Rust 1.88. --- src/net.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/net.rs b/src/net.rs index 04e6a24..f2052ff 100644 --- a/src/net.rs +++ b/src/net.rs @@ -2235,6 +2235,16 @@ impl Ipv4NetworkDiff { } } +/// Marks the calling branch as unlikely, standing in for +/// `core::hint::cold_path()`, which needs Rust 1.95. +/// +/// An empty `#[cold]` body is provably side-effect free, so the call itself is +/// optimized away and only the attribute's block-placement hint survives at the +/// call site: the cold branch costs nothing on the steps that never take it. +#[cold] +#[inline(never)] +fn cold_path() {} + impl Iterator for Ipv4NetworkDiff { type Item = Ipv4Network; @@ -2264,7 +2274,7 @@ impl Iterator for Ipv4NetworkDiff { // guesses. The call keeps the rescue a real branch: a // conditional move would chain the scan back into every // step's dependencies. - core::hint::cold_path(); + cold_path(); b = 0x8000_0000u32 >> self.remaining.leading_zeros(); } } @@ -3957,7 +3967,7 @@ impl Iterator for Ipv6NetworkDiff { // drained `remaining` down past the guesses. The call // keeps the rescue a real branch: a conditional move would // chain the scan back into every step's dependencies. - core::hint::cold_path(); + cold_path(); b = 0x8000_0000_0000_0000u64 >> remaining.leading_zeros(); } } @@ -3991,7 +4001,7 @@ impl Iterator for Ipv6NetworkDiff { return None; } - core::hint::cold_path(); + cold_path(); b = 0x8000_0000_0000_0000u64 >> remaining.leading_zeros(); } }