Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

perf(monitor): decode the full window ahead and open the next cut early

marcinx98xe5931a6e4742
diff --git a/src/crates/concat-host/src/preview.rs b/src/crates/concat-host/src/preview.rs
index 98a9578e..d0c08978 100644
--- a/src/crates/concat-host/src/preview.rs
+++ b/src/crates/concat-host/src/preview.rs
@@ -23,6 +23,10 @@ use concat_export::ExportClip;
use concat_project::DocumentSettings;
use concat_project::model::ColorSpace;
+/// How far ahead of the playhead playback opens the file a cut leads to;
+/// see [`Monitor::prefetch`].
+const NEXT_CUT: f64 = 1.0;
+
/// A frame request: the instant and the size, with the clips coming from
/// the session that owns them.
#[derive(Clone, Copy, Debug)]
@@ -309,8 +313,41 @@ impl Monitor {
frames: u32,
) {
let plan = self.plan_for(clips, settings, spec);
- let moments = concat_export::preview_moments(&plan, spec.time, frames.min(8), spec.proxy);
+ let moments = concat_export::preview_moments(
+ &plan,
+ spec.time,
+ frames.min(concat_media::prefetch::AHEAD),
+ spec.proxy,
+ );
let fps = (settings.rate_num as f64 / settings.rate_den.max(1) as f64).max(1.0);
+ // The file a cut leads into, read a second early: opening a reader
+ // and seeking it costs more than the quarter second the frames
+ // ahead cover, so a cut would otherwise wait on it. Only files the
+ // frames ahead do not read already, and not pinned - this is to
+ // have the reader open and its first frames cached by the cut.
+ if spec.moving
+ && let Some(far) =
+ concat_export::preview_moments(&plan, spec.time + NEXT_CUT, 1, spec.proxy).pop()
+ {
+ let fresh: Vec<_> = far
+ .frames
+ .into_iter()
+ .filter(|request| {
+ !request.still
+ && !moments
+ .iter()
+ .any(|near| near.frames.iter().any(|read| read.path == request.path))
+ })
+ .collect();
+ if !fresh.is_empty() {
+ let pool = Arc::clone(&self.pool);
+ crate::scheduler().submit(concat_media::Priority::Filmstrip, move || {
+ for request in &fresh {
+ let _ = pool.frame(request);
+ }
+ });
+ }
+ }
crate::scheduler().advance(
concat_media::Cursor {
time: spec.time,
diff --git a/src/crates/concat/src/panes/monitor.rs b/src/crates/concat/src/panes/monitor.rs
index 4787d97a..4f126c6d 100644
--- a/src/crates/concat/src/panes/monitor.rs
+++ b/src/crates/concat/src/panes/monitor.rs
@@ -330,11 +330,20 @@ impl MonitorPane {
};
// Decode-ahead for whatever comes next, on a worker of its
// own, so the frame goes to the window without waiting for
- // it and the next frame can start meanwhile.
+ // it and the next frame can start meanwhile. Playing, the
+ // full window ahead: the scheduler adds only the frames it
+ // has not queued yet, so asking for all of them each frame
+ // costs nothing and keeps a slow decode from catching up
+ // with the playhead.
{
let monitor = monitor.clone();
let settings = settings.clone();
- spawn_detached(move || monitor.prefetch(clips, &settings, spec, 2));
+ let ahead = if spec.moving {
+ concat_media::prefetch::AHEAD
+ } else {
+ 2
+ };
+ spawn_detached(move || monitor.prefetch(clips, &settings, spec, ahead));
}
frame
},