perf(core): use quanta::Instant for hot-path timing (#2384)

Replace std::time::Instant with quanta::Instant on per-packet, per-RPC,
and per-session paths. TSC-based, ~5ns vs ~25ns per now() call.

Reuses the existing `extern crate self as hotpath` alias so
`use hotpath::instant::Instant;` resolves to the same quanta type with
or without the hotpath feature. Leaves tokio::time::Instant and
smoltcp::time::Instant untouched.
This commit is contained in:
fanyang
2026-06-28 10:43:55 +08:00
committed by GitHub
parent be2034dd06
commit 7205517160
32 changed files with 123 additions and 80 deletions
+3 -2
View File
@@ -1,6 +1,5 @@
use std::net::{Ipv4Addr, Ipv6Addr};
use std::sync::atomic::Ordering;
use std::time::Instant;
use std::{
net::IpAddr,
sync::{Arc, atomic::AtomicBool},
@@ -8,6 +7,7 @@ use std::{
use arc_swap::ArcSwap;
use dashmap::DashMap;
use hotpath::instant::Instant;
use pnet::packet::ipv6::Ipv6Packet;
use pnet::packet::{
Packet as _, ip::IpNextHeaderProtocols, ipv4::Ipv4Packet, tcp::TcpPacket, udp::UdpPacket,
@@ -402,9 +402,10 @@ mod tests {
use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr},
sync::Arc,
time::Instant,
};
use hotpath::instant::Instant;
use crate::{
common::acl_processor::PacketInfo,
proto::acl::{Acl, ChainType, Protocol},
+2 -1
View File
@@ -6,6 +6,7 @@ use std::{
time::Duration,
};
use hotpath::instant::Instant;
use rand::{Rng, thread_rng};
use tokio::{
sync::broadcast,
@@ -177,7 +178,7 @@ impl PeerConnPinger {
sink.send(req).await?;
control_metrics.record_tx(req_len);
let now = std::time::Instant::now();
let now = Instant::now();
// wait until we get a pong packet in ctrl_resp_receiver
let resp = timeout(Duration::from_secs(2), async {
loop {
+5 -7
View File
@@ -2,12 +2,13 @@ use anyhow::Context;
use async_trait::async_trait;
use cidr::{Ipv4Cidr, Ipv6Cidr};
use dashmap::DashMap;
use hotpath::instant::Instant;
use std::collections::BTreeSet;
use std::{
fmt::Debug,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
sync::{Arc, Weak, atomic::AtomicBool},
time::{Duration, Instant, SystemTime},
time::{Duration, SystemTime},
};
#[cfg(feature = "hotpath")]
@@ -2204,12 +2205,9 @@ impl PeerManager {
#[cfg(test)]
mod tests {
use base64::Engine;
use std::{
collections::HashMap,
fmt::Debug,
sync::Arc,
time::{Duration, Instant},
};
use std::{collections::HashMap, fmt::Debug, sync::Arc, time::Duration};
use hotpath::instant::Instant;
use crate::{
common::{
+9 -8
View File
@@ -6,13 +6,14 @@ use std::{
Arc, Weak,
atomic::{AtomicBool, AtomicU32, Ordering},
},
time::{Duration, Instant, SystemTime},
time::{Duration, SystemTime},
};
use arc_swap::ArcSwap;
use cidr::{IpCidr, Ipv4Cidr, Ipv6Cidr, Ipv6Inet};
use crossbeam::atomic::AtomicCell;
use dashmap::DashMap;
use hotpath::instant::Instant;
use ordered_hash_map::OrderedHashMap;
use parking_lot::{RwLock, lock_api::RwLockUpgradableReadGuard};
use petgraph::{
@@ -2170,9 +2171,9 @@ struct PeerRouteServiceImpl {
interface_peers_generation: AtomicU64,
applied_interface_peers_generation: AtomicU64,
last_update_my_foreign_network: AtomicCell<Option<std::time::Instant>>,
last_update_my_foreign_network: AtomicCell<Option<Instant>>,
peer_info_last_update: AtomicCell<std::time::Instant>,
peer_info_last_update: AtomicCell<Instant>,
}
impl Debug for PeerRouteServiceImpl {
@@ -2239,7 +2240,7 @@ impl PeerRouteServiceImpl {
last_update_my_foreign_network: AtomicCell::new(None),
peer_info_last_update: AtomicCell::new(std::time::Instant::now()),
peer_info_last_update: AtomicCell::new(Instant::now()),
}
}
@@ -2435,7 +2436,7 @@ impl PeerRouteServiceImpl {
}
self.last_update_my_foreign_network
.store(Some(std::time::Instant::now()));
.store(Some(Instant::now()));
let foreign_networks = self
.interface
@@ -3156,12 +3157,12 @@ impl PeerRouteServiceImpl {
"update_peer_info_last_update, my_peer_id: {:?}, prev: {:?}, new: {:?}",
self.my_peer_id,
self.peer_info_last_update.load(),
std::time::Instant::now()
Instant::now()
);
self.peer_info_last_update.store(std::time::Instant::now());
self.peer_info_last_update.store(Instant::now());
}
fn get_peer_info_last_update(&self) -> std::time::Instant {
fn get_peer_info_last_update(&self) -> Instant {
self.peer_info_last_update.load()
}
+2 -1
View File
@@ -1,6 +1,7 @@
use std::{sync::Arc, time::Instant};
use std::sync::Arc;
use dashmap::DashMap;
use hotpath::instant::Instant;
use prost::Message;
use snow::params::NoiseParams;
use tokio::sync::{Mutex, OwnedMutexGuard, oneshot};
+3 -2
View File
@@ -1,6 +1,7 @@
use cidr::Ipv6Inet;
use cidr::{Ipv4Cidr, Ipv6Cidr};
use dashmap::DashMap;
use hotpath::instant::Instant;
use std::{
collections::BTreeSet,
net::{Ipv4Addr, Ipv6Addr},
@@ -157,7 +158,7 @@ pub trait Route {
async fn get_peer_info(&self, peer_id: PeerId) -> Option<RoutePeerInfo>;
async fn get_peer_info_last_update_time(&self) -> std::time::Instant;
async fn get_peer_info_last_update_time(&self) -> Instant;
fn get_peer_groups(&self, peer_id: PeerId) -> Arc<Vec<String>>;
@@ -226,7 +227,7 @@ impl Route for MockRoute {
panic!("mock route")
}
async fn get_peer_info_last_update_time(&self) -> std::time::Instant {
async fn get_peer_info_last_update_time(&self) -> Instant {
panic!("mock route")
}