polars_utils/tick_counter.rs
1/// **Unsynchronized** tick counter.
2///
3/// This is intended to be used to *roughly* organize events across threads in
4/// their received order but without any actual guarantees around monotonicity
5/// or synchronization, for maximum speed.
6///
7/// This does not guarantee monotonicity or any particular (fixed) time unit.
8/// If correctness matters use Instant::now().
9#[inline]
10pub fn tick_counter() -> u64 {
11 cfg_select! {
12 target_arch = "x86_64" => unsafe { core::arch::x86_64::_rdtsc() },
13
14 target_arch = "aarch64" => {
15 let cnt: u64;
16 unsafe {
17 core::arch::asm!(
18 "mrs {cnt}, cntvct_el0",
19 cnt = out(reg) cnt,
20 options(nomem, nostack, preserves_flags),
21 );
22 }
23 cnt
24 },
25
26 _ => {
27 use std::sync::LazyLock;
28 use std::time::Instant;
29
30 static REFERENCE_INSTANT: LazyLock<Instant> = LazyLock::new(Instant::now);
31 REFERENCE_INSTANT.elapsed().as_nanos() as u64
32 },
33 }
34}