Part of #4516: Windows renders start workers only for the cores the process may use

The renderer sized its worker pool from available_parallelism, which on
Windows counts every processor in the machine, also when the process's
affinity mask allows fewer. A process allowed one core started 8 workers,
and the renderer's flush spun on that core while the workers got no time,
so renders crawled or stalled.

The desktop app and vectorcraft-cli now read the affinity mask at startup
(src/windows_cores.rs, shared) and pass the count to the renderer with
vectorcraft_render::set_usable_cores. With one usable core the renderer
renders on the calling thread.
This commit is contained in:
dotCooCoo
2026-10-31 18:44:55 +07:00
committed by bflatastic
parent 663e691908
commit 4e785b40ce
8 changed files with 121 additions and 8 deletions
+1 -1
View File
@@ -227,6 +227,6 @@ pub(crate) use text_geom::is_opentype_alternate;
#[cfg(test)]
use text_geom::text_geom;
pub(crate) use threads::single_threaded_context;
pub use threads::{default_threads, set_default_threads};
pub use threads::{default_threads, set_default_threads, set_usable_cores};
#[cfg(test)]
mod tests;
+40 -4
View File
@@ -9,14 +9,25 @@ pub(crate) fn single_threaded_context(w: u16, h: u16) -> RenderContext {
/// Preferred rasterizer thread count set by the app (Preferences → Performance); negative = automatic.
static THREADS_OVERRIDE: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(-1);
/// The cores the process may run on, as the app read them from the system; 0 = not known.
static USABLE_CORES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Override the worker-thread count used by renderers created from now on (`None` = automatic).
pub fn set_default_threads(n: Option<u16>) {
THREADS_OVERRIDE.store(n.map_or(-1, i32::from), std::sync::atomic::Ordering::Relaxed);
}
/// Rasterizer worker threads: 0 on wasm; otherwise one per core up to 8, leaving a core free. On a
/// 10-core machine 8 workers render a 50,000-path page 1.4× faster than 4 (9 are slower again);
/// the pixels don't depend on the count (`pixels_do_not_depend_on_the_thread_count`).
/// The number of cores the process may run on, when the app knows a smaller number than
/// `available_parallelism` gives (`None` = not known). On Windows `available_parallelism` counts
/// every processor in the machine, also when the process's affinity mask allows fewer.
pub fn set_usable_cores(n: Option<usize>) {
USABLE_CORES.store(n.unwrap_or(0), std::sync::atomic::Ordering::Relaxed);
}
/// Rasterizer worker threads: 0 on wasm; otherwise one per core the process may run on, up to 8,
/// leaving a core free. On a 10-core machine 8 workers render a 50,000-path page 1.4× faster than 4
/// (9 are slower again); the pixels don't depend on the count
/// (`pixels_do_not_depend_on_the_thread_count`).
pub fn default_threads() -> u16 {
#[cfg(target_arch = "wasm32")]
{
@@ -31,6 +42,31 @@ pub fn default_threads() -> u16 {
if std::env::var_os("VECTORCRAFT_RENDER_THREADS").is_some() {
return std::env::var("VECTORCRAFT_RENDER_THREADS").ok().and_then(|v| v.parse().ok()).unwrap_or(0);
}
std::thread::available_parallelism().map(|n| (n.get().saturating_sub(1)).min(8) as u16).unwrap_or(0)
let usable = USABLE_CORES.load(std::sync::atomic::Ordering::Relaxed);
std::thread::available_parallelism().map(|n| automatic_threads(n.get(), usable)).unwrap_or(0)
}
}
/// [`default_threads`] when automatic, on `cpus` processors of which the process may use `usable`
/// (0 = all).
fn automatic_threads(cpus: usize, usable: usize) -> u16 {
let cores = if usable == 0 { cpus } else { cpus.min(usable) };
(cores.saturating_sub(1)).min(8) as u16
}
#[cfg(test)]
mod tests {
use super::automatic_threads;
/// A process that may run on one core renders on the calling thread. With workers, vello_cpu's
/// flush spins on that core while the workers it waits for get no time, and a render crawls or
/// stalls.
#[test]
fn workers_follow_the_cores_the_process_may_use() {
assert_eq!(automatic_threads(24, 0), 8);
assert_eq!(automatic_threads(24, 1), 0);
assert_eq!(automatic_threads(24, 4), 3);
assert_eq!(automatic_threads(4, 16), 3);
assert_eq!(automatic_threads(1, 0), 0);
}
}