Encoders use all CPUs but one by default

libvpx's worker threads wait on each other by spinning. With one thread
per core, an encode slows down many times over as soon as anything else
wants a core: the capture and conversion of the next frame, another
layer, the desktop. The bench on a 4-core machine showed single frames
at 1080p and 1440p taking 130-290 ms with 4 threads under load, where the
same frames took 9-17 ms with 3 threads next to two other layers. The
default (`codec::encoder_cpus`) is now all CPUs but one, for single
encoders and for the streamer's split across layers; an explicit
`EncoderConfig::threads` still wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
This commit is contained in:
Claude committed 2026-09-27 18:38:33 +00:00
1 parent ffc79e6835
commit f6a4ae35e7
3 files changed
+23 -9

No files matched your search

+3 -1
View File
@@ -530,8 +530,10 @@ fn thread_split(layers: &[LayerSpec], cores: u32) -> Vec<u32> {
layers.iter().map(|l| ((f64::from(cores) * area(l) / total).round() as u32).max(1)).collect()
}
/// CPUs split between the layers' encoders (all but one, see
/// `voelin_media::codec::encoder_cpus`).
fn cores() -> u32 {
std::thread::available_parallelism().map_or(1, |n| n.get() as u32)
voelin_media::codec::encoder_cpus()
}
fn new_encoder(
+15 -4
View File
@@ -127,7 +127,7 @@ pub struct EncoderConfig {
pub keyframe_interval: Option<u32>,
pub content: ContentHint,
/// Most encoder threads (still no more than the frame size can use); 0:
/// all CPUs.
/// all CPUs but one ([`encoder_cpus`]).
pub threads: u32,
/// A fixed speed / quality trade-off in the backend's own terms (libvpx
/// `cpu-used`); `None` lets the encoder adapt it to how long frames take
@@ -148,13 +148,24 @@ impl Default for EncoderConfig {
}
}
/// CPUs encoders use by default: all but one (at least one). libvpx's
/// threads wait on each other by spinning, so an encode that wants every
/// core slows down many times over as soon as anything else runs (the
/// capture and conversion of the next frame, another layer, the desktop);
/// measured on a 4-core machine: 130-290 ms per 1080p frame with 4 threads
/// under load, against 9-17 ms with 3.
pub fn encoder_cpus() -> u32 {
let cpus = std::thread::available_parallelism().map_or(1, |n| n.get() as u32);
cpus.saturating_sub(1).max(1)
}
impl EncoderConfig {
/// Threads for a `width` x `height` encode: [`threads`](Self::threads)
/// (or all CPUs), but no more than one per 320x240 pixels: more would
/// idle on small frames and cost quality (VP8 token partitions).
/// (or [`encoder_cpus`]), but no more than one per 320x240 pixels: more
/// would idle on small frames and cost quality (VP8 token partitions).
pub fn threads_for(&self, width: u32, height: u32) -> u32 {
let cpus = match self.threads {
0 => std::thread::available_parallelism().map_or(1, |n| n.get() as u32),
0 => encoder_cpus(),
n => n,
};
let useful = (u64::from(width) * u64::from(height) / (320 * 240)).clamp(1, 64) as u32;