sol_parser_sdk/logs/perf_hints.rs
1//! 性能优化提示和内联函数
2
3/// likely - 告诉编译器条件大概率为真
4#[inline(always)]
5pub fn likely(condition: bool) -> bool {
6 #[cold]
7 fn cold() {}
8
9 if !condition {
10 cold();
11 }
12 condition
13}
14
15/// unlikely - 告诉编译器条件大概率为假
16#[inline(always)]
17pub fn unlikely(condition: bool) -> bool {
18 #[cold]
19 fn cold() {}
20
21 if condition {
22 cold();
23 }
24 condition
25}
26
27/// 预取数据到 CPU 缓存(读优化)
28///
29/// # Safety
30///
31/// `ptr` must be a valid pointer for the target platform to prefetch. The
32/// intrinsic does not dereference it, but callers must still only pass pointers
33/// derived from live allocations or documented platform-safe addresses.
34#[inline(always)]
35pub unsafe fn prefetch_read<T>(ptr: *const T) {
36 #[cfg(target_arch = "x86_64")]
37 {
38 use std::arch::x86_64::{_mm_prefetch, _MM_HINT_T0};
39 _mm_prefetch(ptr as *const i8, _MM_HINT_T0);
40 }
41}
42
43/// 预取数据到 CPU 缓存(写优化)
44///
45/// # Safety
46///
47/// `ptr` must be a valid pointer for the target platform to prefetch. The
48/// intrinsic does not dereference it, but callers must still only pass pointers
49/// derived from live allocations or documented platform-safe addresses.
50#[inline(always)]
51pub unsafe fn prefetch_write<T>(ptr: *const T) {
52 #[cfg(target_arch = "x86_64")]
53 {
54 use std::arch::x86_64::{_mm_prefetch, _MM_HINT_T1};
55 _mm_prefetch(ptr as *const i8, _MM_HINT_T1);
56 }
57}