From 3adbaf7389336815204d0fa4a2f95715de43d310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 10 Aug 2026 00:26:55 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20perf(playback):=20retire=20cancelle?= =?UTF-8?q?d=20workers=20off=20the=20seek=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Joining the outgoing decode worker inline made a seek pay whatever VA-API init that worker was still inside -- 98.4 ms worst case against 1.6 ms before the join landed, i.e. a scrub capped at ~10 seeks/s with a visible hitch per step. Cancelled workers now go into a retire list instead. Every seek sweeps it, dropping only the ones whose thread has already returned (`is_finished`, never a wait), and the session's drop joins whatever is left along with the running worker -- so the exit guarantee is untouched: no decode worker outlives its session, and none can be inside libva when Mesa's atexit handlers run. Worst seek back to 4.7-6.1 ms over three drill runs. Open+drop stays at ~13.5 ms, which is the same join as before and a session drops rarely. Retire list peaked at 16 under a back-to-back 20-seek loop -- a UI scrub leaves far more time between steps. --- crates/engine/src/decode.rs | 9 +++++ crates/engine/src/playback.rs | 65 +++++++++++++++++++++++++---------- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/crates/engine/src/decode.rs b/crates/engine/src/decode.rs index 3a8a3cd..6fe674b 100644 --- a/crates/engine/src/decode.rs +++ b/crates/engine/src/decode.rs @@ -44,6 +44,15 @@ impl Worker { self.cancel.store(true, Ordering::Release); } + /// Whether the thread has already returned, i.e. dropping this would not + /// wait. `true` for a worker that never had a thread. What lets a caller + /// park cancelled workers and reap them without ever blocking. + pub(crate) fn is_finished(&self) -> bool { + self.handle + .as_ref() + .is_none_or(thread::JoinHandle::is_finished) + } + /// Lets the worker run on unwatched -- what the public API does, since it /// hands out the flag alone. fn detach(mut self) -> Arc { diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 36e503b..390309d 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -84,10 +84,21 @@ impl Audio { pub struct PlaybackSession { meta: VideoMeta, frames: Receiver, - /// The current decode worker. Replacing or dropping this field cancels and - /// joins the previous one, so no abandoned worker outlives the session -- - /// which is what keeps a worker out of the driver's teardown at exit. + /// The current decode worker. worker: Worker, + /// Workers cancelled by a seek or a clip change, waiting to be reaped. + /// Joining one inline costs whatever VA-API init it is still inside (98 ms + /// measured worst case), which is a visible hitch on the caller's thread; + /// parking it here instead makes that free, and [`retire`] drops the ones + /// that have already returned every time it adds another. + /// + /// Never a leak, and that is the invariant: everything in here is joined + /// when the session drops (`Vec`'s own drop does it, one + /// [`Worker::drop`] each), so no decode worker can outlive the session and + /// meet Mesa's `atexit` handlers from inside libva. + /// + /// [`retire`]: PlaybackSession::retire + retired: Vec, clock: PlaybackClock, audio: Option