use std::sync::LazyLock;
pub static BACKGROUND_THREAD_POOL: LazyLock<rayon::ThreadPool> = LazyLock::new(|| {
let total = std::thread::available_parallelism()
.map(|p| p.get())
.unwrap_or(4);
let bg_threads = (total / 2).max(2);
rayon::ThreadPoolBuilder::new()
.num_threads(bg_threads)
.thread_name(|i| format!("fff-bg-{i}"))
.start_handler(|_| {
#[cfg(target_os = "macos")]
unsafe {
let _ = libc::pthread_set_qos_class_self_np(
libc::qos_class_t::QOS_CLASS_USER_INITIATED,
0,
);
}
})
.build()
.expect("failed to create background rayon pool")
});
#[cfg(target_os = "macos")]
fn performance_core_count() -> usize {
let mut count: libc::c_int = 0;
let mut size = std::mem::size_of::<libc::c_int>();
let name = c"hw.perflevel0.physicalcpu";
let ok = unsafe {
libc::sysctlbyname(
name.as_ptr(),
&mut count as *mut _ as *mut libc::c_void,
&mut size,
std::ptr::null_mut(),
0,
)
};
if ok == 0 && count > 0 {
count as usize
} else {
std::thread::available_parallelism()
.map(|p| p.get())
.unwrap_or(4)
}
}
pub static SEARCH_THREAD_POOL: LazyLock<rayon::ThreadPool> = LazyLock::new(|| {
#[cfg(target_os = "macos")]
let threads = performance_core_count();
#[cfg(not(target_os = "macos"))]
let threads = std::thread::available_parallelism()
.map(|p| p.get())
.unwrap_or(4);
rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.thread_name(|i| format!("fff-search-{i}"))
.start_handler(|_| {
#[cfg(target_os = "macos")]
unsafe {
let _ = libc::pthread_set_qos_class_self_np(
libc::qos_class_t::QOS_CLASS_USER_INITIATED,
0,
);
}
})
.build()
.expect("failed to create search rayon pool")
});