From c9c90a5a72fe572f9b1daeaa09546fe66aef83b1 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 02:46:51 -0800 Subject: [PATCH] host: QueueOptions::default_lanes reads the core count once per process `QueueOptions::default_lanes` called `std::thread::available_parallelism` on every call. On Linux that call opens /proc/self/cgroup and the cpu.max file of every cgroup level, so each call costs several file opens and reads. `ThumbnailOptions::new` calls it, and the glyphs panel's `glyph_painter` builds a `ThumbnailOptions` for every visible cell in every pass, so the panel made about 5,000 such calls per frame. The lane count is now computed once and kept in a `OnceLock`. wasm32 still returns 0. --- crates/host/src/queue.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/host/src/queue.rs b/crates/host/src/queue.rs index 7197297cc..8133d17e6 100644 --- a/crates/host/src/queue.rs +++ b/crates/host/src/queue.rs @@ -19,6 +19,8 @@ use std::cmp::Reverse; use std::collections::VecDeque; use std::hash::Hash; +#[cfg(not(target_arch = "wasm32"))] +use std::sync::OnceLock; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Condvar, Mutex, MutexGuard}; use std::thread::{self, ThreadId}; @@ -94,14 +96,17 @@ pub enum QueueOrder { impl QueueOptions { /// Lanes for this machine: the number of cores less one, at least 2 and at most 8 (4 when - /// the core count is unknown); 0 on wasm32. + /// the core count is unknown); 0 on wasm32. The core count is read once per process. // lightcraft: crates/preview/src/pool.rs:104 `default_threads` (cores − 1 clamped to 2..=8; // ours is 0 on wasm32 instead of 1, because wasm runs jobs only through `run_inline`). pub fn default_lanes() -> usize { #[cfg(target_arch = "wasm32")] return 0; #[cfg(not(target_arch = "wasm32"))] - thread::available_parallelism().map_or(4, |n| n.get().saturating_sub(1).clamp(2, 8)) + { + static LANES: OnceLock = OnceLock::new(); + *LANES.get_or_init(|| thread::available_parallelism().map_or(4, |n| n.get().saturating_sub(1).clamp(2, 8))) + } } }