forge
Explore
Log in

Don't you dare go Hollow

Retire cancelled workers off the seek path

Browse files at this commit

3adbaf7389336815204d0fa4a2f95715de43d310Ahmet Kaan GümüşParent: 4c831c1
  • perf
    • retire cancelled workers off the seek path
Show full message
⚡ perf(playback): retire cancelled workers off the seek path

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.rsmodified+9 -0
@@ -44,6 +44,15 @@ impl Worker {
44 self.cancel.store(true, Ordering::Release);44 self.cancel.store(true, Ordering::Release);
45 }45 }
46 46
47+ /// Whether the thread has already returned, i.e. dropping this would not
48+ /// wait. `true` for a worker that never had a thread. What lets a caller
49+ /// park cancelled workers and reap them without ever blocking.
50+ pub(crate) fn is_finished(&self) -> bool {
51+ self.handle
52+ .as_ref()
53+ .is_none_or(thread::JoinHandle::is_finished)
54+ }
55+
47 /// Lets the worker run on unwatched -- what the public API does, since it56 /// Lets the worker run on unwatched -- what the public API does, since it
48 /// hands out the flag alone.57 /// hands out the flag alone.
49 fn detach(mut self) -> Arc<AtomicBool> {58 fn detach(mut self) -> Arc<AtomicBool> {
crates/engine/src/playback.rsmodified+46 -19
@@ -84,10 +84,21 @@ impl Audio {
84 pub struct PlaybackSession {84 pub struct PlaybackSession {
85 meta: VideoMeta,85 meta: VideoMeta,
86 frames: Receiver<Frame>,86 frames: Receiver<Frame>,
87- /// The current decode worker. Replacing or dropping this field cancels and87+ /// The current decode worker.
88- /// joins the previous one, so no abandoned worker outlives the session --
89- /// which is what keeps a worker out of the driver's teardown at exit.
90 worker: Worker,88 worker: Worker,
89+ /// Workers cancelled by a seek or a clip change, waiting to be reaped.
90+ /// Joining one inline costs whatever VA-API init it is still inside (98 ms
91+ /// measured worst case), which is a visible hitch on the caller's thread;
92+ /// parking it here instead makes that free, and [`retire`] drops the ones
93+ /// that have already returned every time it adds another.
94+ ///
95+ /// Never a leak, and that is the invariant: everything in here is joined
96+ /// when the session drops (`Vec<Worker>`'s own drop does it, one
97+ /// [`Worker::drop`] each), so no decode worker can outlive the session and
98+ /// meet Mesa's `atexit` handlers from inside libva.
99+ ///
100+ /// [`retire`]: PlaybackSession::retire
101+ retired: Vec<Worker>,
91 clock: PlaybackClock,102 clock: PlaybackClock,
92 audio: Option<Audio>,103 audio: Option<Audio>,
93 /// The edit list. Everything a caller says in seconds is a *timeline*104 /// The edit list. Everything a caller says in seconds is a *timeline*
@@ -124,6 +135,7 @@ impl PlaybackSession {
124 meta,135 meta,
125 frames,136 frames,
126 worker,137 worker,
138+ retired: Vec::new(),
127 clock: PlaybackClock::new(source),139 clock: PlaybackClock::new(source),
128 audio,140 audio,
129 project,141 project,
@@ -186,6 +198,7 @@ impl PlaybackSession {
186 meta,198 meta,
187 frames,199 frames,
188 worker,200 worker,
201+ retired: Vec::new(),
189 clock: PlaybackClock::new(match audio {202 clock: PlaybackClock::new(match audio {
190 Some(_) => ClockSource::Audio,203 Some(_) => ClockSource::Audio,
191 None => ClockSource::Wall,204 None => ClockSource::Wall,
@@ -260,6 +273,20 @@ impl PlaybackSession {
260 self.eos273 self.eos
261 }274 }
262 275
276+ /// Installs `replacement` as the decode worker, parking the outgoing one in
277+ /// [`retired`](Self::retired) rather than joining it here, and reaping the
278+ /// parked ones that have already returned.
279+ ///
280+ /// The sweep is what bounds the list: every worker in it was cancelled
281+ /// before this ran, and a cancelled worker is out within an access unit
282+ /// (plus whatever VA-API init it was inside), so a scrub reaps last seek's
283+ /// worker on this seek and the list stays a handful long. Nothing waits.
284+ fn retire(&mut self, replacement: Worker) {
285+ self.retired
286+ .push(std::mem::replace(&mut self.worker, replacement));
287+ self.retired.retain(|w| !w.is_finished());
288+ }
289+
263 /// Starts decoding whatever follows the current range on the timeline;290 /// Starts decoding whatever follows the current range on the timeline;
264 /// `false` past the end. The next timeline frame is derived rather than291 /// `false` past the end. The next timeline frame is derived rather than
265 /// remembered as a clip index, because a `cut` while playing splits the292 /// remembered as a clip index, because a `cut` while playing splits the
@@ -272,15 +299,15 @@ impl PlaybackSession {
272 let clip = self.project.clips()[idx];299 let clip = self.project.clips()[idx];
273 let out = clip.out_frame;300 let out = clip.out_frame;
274 // We only get here on a disconnect, so the old worker has already301 // We only get here on a disconnect, so the old worker has already
275- // returned; cancel anyway, so the join below is unconditional in kind.302+ // returned; cancel anyway, so `retire` treats every path alike.
276 self.worker.cancel();303 self.worker.cancel();
277 match DecodeSession::open_worker(&self.project.sources()[clip.source], source, out) {304 match DecodeSession::open_worker(&self.project.sources()[clip.source], source, out) {
278 Ok((_, frames, worker)) => {305 Ok((_, frames, worker)) => {
279 // Receiver first, worker second: the drop of the old receiver306 // Receiver first, worker second: the drop of the old receiver
280- // is what would wake a worker parked in `send`, and only then307+ // is what wakes a worker parked in `send`, and only then can it
281- // can the old worker's `Drop` join without waiting on us.308+ // return and be reaped by a later sweep.
282 self.frames = frames;309 self.frames = frames;
283- self.worker = worker;310+ self.retire(worker);
284 }311 }
285 // Disappearing-file case, as in `seek`. The old receiver is still312 // Disappearing-file case, as in `seek`. The old receiver is still
286 // disconnected, so the next pass moves on to the clip after this.313 // disconnected, so the next pass moves on to the clip after this.
@@ -464,14 +491,9 @@ impl PlaybackSession {
464 491
465 // Cancel first, drop second: the old worker may be parked in `send` on492 // Cancel first, drop second: the old worker may be parked in `send` on
466 // the bounded channel, where only the disconnect wakes it -- and it493 // the bounded channel, where only the disconnect wakes it -- and it
467- // then finds the flag already set.494+ // then finds the flag already set. It is then parked, not joined; see
468- //495+ // [`retire`](Self::retire), which is what keeps a scrub off the price
469- // ponytail: replacing the worker below *joins* the outgoing one, so a496+ // of a VA-API init.
470- // seek can cost one VA-API init (98 ms measured worst case, 1.6 ms
471- // before this) and scrubbing is capped at ~10 seeks/s. If that shows in
472- // the UI: park cancelled workers in a retire list instead, drop the
473- // `is_finished` ones each seek and join whatever is left in `Drop` --
474- // same exit guarantee, no wait on the caller's thread.
475 self.worker.cancel();497 self.worker.cancel();
476 // `target` is inside the timeline (never empty), so this always maps;498 // `target` is inside the timeline (never empty), so this always maps;
477 // the range runs from there to the end of the clip it landed in.499 // the range runs from there to the end of the clip it landed in.
@@ -482,7 +504,7 @@ impl PlaybackSession {
482 Ok((_, frames, worker)) => {504 Ok((_, frames, worker)) => {
483 // Receiver first, worker second -- see `next_clip`.505 // Receiver first, worker second -- see `next_clip`.
484 self.frames = frames;506 self.frames = frames;
485- self.worker = worker;507+ self.retire(worker);
486 }508 }
487 // The file opened once already, so this is a disappearing-file509 // The file opened once already, so this is a disappearing-file
488 // case: the timeline still moves, there are simply no more510 // case: the timeline still moves, there are simply no more
@@ -578,9 +600,14 @@ impl PlaybackSession {
578 }600 }
579 601
580 impl Drop for PlaybackSession {602 impl Drop for PlaybackSession {
581- /// The video worker cancels and joins itself when the `worker` field drops603+ /// The video workers -- the running one and every cancelled one still in
582- /// right after this; that is what a session must not exit without. This is604+ /// [`retired`](PlaybackSession::retired) -- cancel and join themselves when
583- /// the audio half: bumping the epoch retires the feeder at its next write,605+ /// those fields drop right after this, the running one behind its receiver
606+ /// so a `send` cannot hold the join. *That* is what a session must not exit
607+ /// without: no decode worker outlives it, so none can be inside libva when
608+ /// Mesa's `atexit` handlers free the state under it.
609+ ///
610+ /// This is the audio half: bumping the epoch retires the feeder at its next write,
584 /// which drops the decode receiver with it. Neither is waited for -- no611 /// which drops the decode receiver with it. Neither is waited for -- no
585 /// driver state is torn down on the audio side.612 /// driver state is torn down on the audio side.
586 fn drop(&mut self) {613 fn drop(&mut self) {