Skip to content

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

perf: the window stops scanning on every pointer event, and the per-frame copies and graph builds go

Jareerd1de41e76c0d
diff --git a/src/crates/concat-host/src/proxy.rs b/src/crates/concat-host/src/proxy.rs
index 29b74a96..fba7c7b6 100644
--- a/src/crates/concat-host/src/proxy.rs
+++ b/src/crates/concat-host/src/proxy.rs
@@ -105,8 +105,7 @@ pub fn ensure(
}
return true;
}
- static WRITING: OnceLock<Mutex<HashSet<PathBuf>>> = OnceLock::new();
- let writing = WRITING.get_or_init(|| Mutex::new(HashSet::new()));
+ let writing = writing();
{
let mut writing = writing
.lock()
@@ -135,6 +134,41 @@ pub fn ensure(
true
}
+/// The proxies being written now, by target, so one is never queued
+/// twice and a sweep leaves it for its writer to finish.
+fn writing() -> &'static Mutex<HashSet<PathBuf>> {
+ static WRITING: OnceLock<Mutex<HashSet<PathBuf>>> = OnceLock::new();
+ WRITING.get_or_init(|| Mutex::new(HashSet::new()))
+}
+
+/// Removes the proxies under `project` that `keep` does not name: the
+/// copies of media no longer in the project, or read as a range they no
+/// longer are. Nothing else sweeps this folder, and a proxy is a
+/// quarter-size copy of every file over HD the project ever held
+/// (audit 2026-09-23, #15). One being written is left for its writer.
+/// Returns how many files went.
+pub fn sweep(project: &Path, keep: &HashSet<PathBuf>) -> usize {
+ let dir = project.join("cache").join("proxy");
+ let Ok(entries) = std::fs::read_dir(&dir) else {
+ return 0;
+ };
+ let writing = writing()
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
+ let mut removed = 0;
+ for entry in entries.flatten() {
+ let path = entry.path();
+ if path.extension().is_some_and(|extension| extension == "mp4")
+ && !keep.contains(&path)
+ && !writing.contains(&path)
+ && std::fs::remove_file(&path).is_ok()
+ {
+ removed += 1;
+ }
+ }
+ removed
+}
+
/// Writes the proxy of `source`, read as `range`, at `size` to `target`:
/// H.264, a fast preset, two threads, in software, into a temporary name
/// that is moved into place only when whole, so a copy that is there is a
@@ -311,4 +345,32 @@ mod tests {
assert!(path_for(&project, &dir.join("missing.mp4").to_string_lossy(), None).is_none());
let _ = std::fs::remove_dir_all(&dir);
}
+
+ #[test]
+ fn a_sweep_keeps_what_is_named_and_what_is_being_written() {
+ let scratch =
+ std::env::temp_dir().join(format!("concat-proxy-sweep-{}", std::process::id()));
+ let dir = scratch.join("cache").join("proxy");
+ std::fs::create_dir_all(&dir).expect("a proxy folder");
+ let kept = dir.join("1111111111111111.mp4");
+ let stale = dir.join("2222222222222222.mp4");
+ let busy = dir.join("3333333333333333.mp4");
+ let partial = dir.join("4444444444444444.mp4.part");
+ for file in [&kept, &stale, &busy, &partial] {
+ std::fs::write(file, b"x").expect("writes");
+ }
+ writing()
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .insert(busy.clone());
+ let keep: HashSet<PathBuf> = [kept.clone()].into_iter().collect();
+ assert_eq!(sweep(&scratch, &keep), 1);
+ assert!(kept.is_file() && busy.is_file() && partial.is_file());
+ assert!(!stale.is_file());
+ writing()
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .remove(&busy);
+ let _ = std::fs::remove_dir_all(&scratch);
+ }
}
diff --git a/src/crates/concat-media/src/decode.rs b/src/crates/concat-media/src/decode.rs
index c4c17c29..f033fe48 100644
--- a/src/crates/concat-media/src/decode.rs
+++ b/src/crates/concat-media/src/decode.rs
@@ -938,7 +938,7 @@ impl Decoder {
&& let Some(last) = self.current.as_ref()
{
self.position = last.pts;
- let frame = last.frame.clone();
+ let frame = share(&last.frame);
return Ok(Some(self.convert(&frame)?));
}
return Ok(None);
@@ -948,7 +948,7 @@ impl Decoder {
// decoder needs that picture again after the end: it converts a
// reference and keeps the original, as the paced path does.
let frame = if self.options.looping {
- let reference = source.frame.clone();
+ let reference = share(&source.frame);
self.convert(&reference)?
} else {
self.convert(&source.frame)?
@@ -998,7 +998,7 @@ impl Decoder {
}
self.position = Some(target);
self.tick += 1;
- let frame = current.frame.clone();
+ let frame = share(&current.frame);
Ok(Some(self.convert(&frame)?))
}
}
@@ -1042,6 +1042,22 @@ impl FrameSource for Decoder {
}
}
+/// A second reference to `frame`'s picture: the same buffers, counted,
+/// so a graph that takes the reference (`buffersrc` does) takes nothing
+/// from the frame's owner. `Video::clone` copies the pixels - fifty
+/// megabytes a frame at 8K - and the pacer clones a frame for every output
+/// frame it repeats (audit 2026-09-23, #16). A frame with nothing to share
+/// is copied the old way.
+fn share(frame: &Video) -> Video {
+ let mut shared = Video::empty();
+ // SAFETY: both pointers are the wrappers' own valid AVFrames for the
+ // duration of the call; av_frame_ref adds references to the source's
+ // buffers and copies its properties into the empty destination, and
+ // touches nothing else. On failure the destination is left unref'd.
+ let result = unsafe { ffmpeg::sys::av_frame_ref(shared.as_mut_ptr(), frame.as_ptr()) };
+ if result < 0 { frame.clone() } else { shared }
+}
+
impl SeekableSource for Decoder {
fn seek(&mut self, to: Rational) -> Result<()> {
self.jump(to)
diff --git a/src/crates/concat-media/src/treat.rs b/src/crates/concat-media/src/treat.rs
index 2e1a5f25..2510e91f 100644
--- a/src/crates/concat-media/src/treat.rs
+++ b/src/crates/concat-media/src/treat.rs
@@ -74,17 +74,58 @@ pub fn fit(
run(frame, &parts.join(","))
}
+/// How many graphs a thread keeps; see [`GRAPHS`].
+const GRAPHS_KEPT: usize = 8;
+
+/// A kept graph: what it was built for, and the graph.
+type Kept = ((String, u32, u32), filter::Graph);
+
+thread_local! {
+ /// Built graphs by their spec and input size, this thread's own: a
+ /// graph is a parse and a validation, which the monitor path paid for
+ /// every treated frame (audit 2026-09-23, #16). A worker treats one or
+ /// two clips at a time, so a few are plenty; the oldest goes when the
+ /// cap is reached. A graph is taken out while in use, so a re-entrant
+ /// call builds its own.
+ static GRAPHS: std::cell::RefCell<Vec<Kept>> = const { std::cell::RefCell::new(Vec::new()) };
+}
+
/// One RGBA picture in, `spec` - a whole filtergraph, ending in the size
/// and format the caller wants out - and one RGBA picture out.
fn run(frame: &Frame, spec: &str) -> Result<Frame> {
ffi::init();
+ let key = (spec.to_owned(), frame.width(), frame.height());
+ let cached = GRAPHS.with(|graphs| {
+ let mut graphs = graphs.borrow_mut();
+ graphs
+ .iter()
+ .position(|(known, _)| *known == key)
+ .map(|index| graphs.remove(index).1)
+ });
+ let mut graph = match cached {
+ Some(graph) => graph,
+ None => build(spec, frame.width(), frame.height())?,
+ };
+ let result = pass(&mut graph, frame);
+ if result.is_ok() {
+ GRAPHS.with(|graphs| {
+ let mut graphs = graphs.borrow_mut();
+ if graphs.len() >= GRAPHS_KEPT {
+ graphs.remove(0);
+ }
+ graphs.push((key, graph));
+ });
+ }
+ result
+}
+
+/// The graph for `spec` over RGBA pictures `width` by `height`.
+fn build(spec: &str, width: u32, height: u32) -> Result<filter::Graph> {
// The graph has no file behind it; errors name the layer instead.
let path = Path::new("layer");
- let (source_w, source_h) = (frame.width(), frame.height());
-
let mut graph = filter::Graph::new();
let args = format!(
- "video_size={source_w}x{source_h}:pix_fmt={}:time_base=1/1000:pixel_aspect=1/1",
+ "video_size={width}x{height}:pix_fmt={}:time_base=1/1000:pixel_aspect=1/1",
Into::<ffmpeg::sys::AVPixelFormat>::into(Pixel::RGBA).0
);
let missing = |name: &str| Error::Missing {
@@ -113,6 +154,13 @@ fn run(frame: &Frame, spec: &str) -> Result<Frame> {
graph
.validate()
.map_err(|error| ffi::fail("filter graph", path, error))?;
+ Ok(graph)
+}
+
+/// One picture through `graph`: in at `in`, out at `out`.
+fn pass(graph: &mut filter::Graph, frame: &Frame) -> Result<Frame> {
+ let path = Path::new("layer");
+ let (source_w, source_h) = (frame.width(), frame.height());
// The picture, as a padded FFmpeg frame.
let mut source = Video::new(Pixel::RGBA, source_w, source_h);
diff --git a/src/crates/concat/src/panes/media_bin.rs b/src/crates/concat/src/panes/media_bin.rs
index 4b55a47d..264b0120 100644
--- a/src/crates/concat/src/panes/media_bin.rs
+++ b/src/crates/concat/src/panes/media_bin.rs
@@ -68,6 +68,11 @@ pub struct MediaBin {
pub sort: usize,
/// The cards' pictures by media id.
pub thumbs: HashMap<String, slint::Image>,
+ /// A card's waveform path by media id, with the peaks it was drawn
+ /// from and the duration it spans: the rows are rebuilt on every
+ /// publish, thirty times a second in playback, and the path is the
+ /// one expensive line of a row.
+ waves: std::cell::RefCell<HashMap<String, (usize, f32, slint::SharedString)>>,
}
impl Default for MediaBin {
@@ -79,6 +84,7 @@ impl Default for MediaBin {
filter: MediaFilter::All,
sort: 0,
thumbs: HashMap::new(),
+ waves: Default::default(),
}
}
}
@@ -311,9 +317,24 @@ impl MediaBin {
// that size - fuller than a lane's candles, on purpose.
let wave = match studio.peaks.get(&item.id) {
Some(peaks) if item.kind == model::MediaKind::Audio => {
- wave_path(peaks, 0.0, item.duration.unwrap_or(0.0) as f32, 64, 0.75)
+ let duration = item.duration.unwrap_or(0.0) as f32;
+ let drawn_from = std::sync::Arc::as_ptr(peaks) as usize;
+ let mut waves = self.waves.borrow_mut();
+ match waves.get(&item.id) {
+ Some((from, span, path))
+ if *from == drawn_from && *span == duration =>
+ {
+ path.clone()
+ }
+ _ => {
+ let path: slint::SharedString =
+ wave_path(peaks, 0.0, duration, 64, 0.75).as_str().into();
+ waves.insert(item.id.clone(), (drawn_from, duration, path.clone()));
+ path
+ }
+ }
}
- _ => String::new(),
+ _ => slint::SharedString::default(),
};
MediaItemData {
id: *self.rows.get(&item.id).unwrap_or(&0),
@@ -327,7 +348,7 @@ impl MediaBin {
.unwrap_or_default()
.into(),
thumbnail: self.thumbs.get(&item.id).cloned().unwrap_or_default(),
- wave: wave.as_str().into(),
+ wave,
selected: self.selected.contains(&item.id),
}
})
diff --git a/src/crates/concat/src/studio.rs b/src/crates/concat/src/studio.rs
index 5717f5e2..f4780371 100644
--- a/src/crates/concat/src/studio.rs
+++ b/src/crates/concat/src/studio.rs
@@ -607,6 +607,17 @@ pub struct Studio {
/// more than one frame repeated. See `host::strip_window`.
pub windows: HashMap<String, Strip>,
art_pending: HashSet<String>,
+ /// Art keys a decode came back empty for: not asked again this
+ /// project, or a file that cannot be read would be decoded on every
+ /// event.
+ art_failed: HashSet<String>,
+ /// What the last art scan was for: the document's revision, the
+ /// screen, and how much was pending. The scan runs when one of them
+ /// changes and is a comparison on the pointer stream between.
+ art_stamp: Option<(u64, bool, usize)>,
+ /// Media whose proxy has been seen to, this project: `proxy::ensure`
+ /// costs two stats of the file, and once is enough.
+ proxied: HashSet<String>,
window_pending: HashSet<String>,
/// Envelopes, keyed by the things they are computed from. A move
/// changes none of them, and a publish happens on every frame of one.
@@ -1344,6 +1355,9 @@ impl Studio {
strips: HashMap::new(),
windows: HashMap::new(),
art_pending: HashSet::new(),
+ art_failed: HashSet::new(),
+ art_stamp: None,
+ proxied: HashSet::new(),
window_pending: HashSet::new(),
waves: RefCell::new(HashMap::new()),
lanes: crate::panes::timeline::TimelinePane::default(),
@@ -2014,20 +2028,33 @@ impl Studio {
let project_path = session.path().to_owned();
// A file larger than HD gets a proxy for playback and the
// filmstrips, written once on the scheduler's proxy lane; see
- // concat_host::proxy.
- for item in &self.project().media {
- if item.kind == model::MediaKind::Video
- && !item.placeholder
- && let (Some(width), Some(height)) = (item.width, item.height)
- {
- concat_host::proxy::ensure(
- std::path::Path::new(&project_path),
- &item.path,
+ // concat_host::proxy. Asked once per file per project.
+ let unproxied: Vec<(String, String, u32, u32, Option<concat_media::ColorRange>)> = self
+ .project()
+ .media
+ .iter()
+ .filter(|item| item.kind == model::MediaKind::Video && !item.placeholder)
+ .filter(|item| !self.proxied.contains(&item.id))
+ .filter_map(|item| {
+ let (width, height) = (item.width?, item.height?);
+ Some((
+ item.id.clone(),
+ item.path.clone(),
width,
height,
item.color_range.map(concat_export::engine_range),
- );
- }
+ ))
+ })
+ .collect();
+ for (id, path, width, height, range) in unproxied {
+ concat_host::proxy::ensure(
+ std::path::Path::new(&project_path),
+ &path,
+ width,
+ height,
+ range,
+ );
+ self.proxied.insert(id);
}
/// One job: the art key it fills, and what to decode.
struct Want {
@@ -2048,7 +2075,9 @@ impl Studio {
.media
.iter()
.filter(|item| !item.placeholder && !item.path.is_empty())
- .filter(|item| !self.art_pending.contains(&item.id))
+ .filter(|item| {
+ !self.art_pending.contains(&item.id) && !self.art_failed.contains(&item.id)
+ })
.filter(|item| {
let needs_thumb = item.kind != model::MediaKind::Audio
&& (!self.media.thumbs.contains_key(&item.id)
@@ -2091,6 +2120,7 @@ impl Studio {
let key = art_key(&item.id, Some(stream));
if self.peaks.contains_key(&key)
|| self.art_pending.contains(&key)
+ || self.art_failed.contains(&key)
|| !asked.insert(key.clone())
{
continue;
@@ -2153,6 +2183,9 @@ impl Studio {
|studio, _, _, art: MediaArt| {
let key = art_key(&art.id, art.stream);
studio.art_pending.remove(&key);
+ if art.thumbnail.is_none() && art.strip.is_none() && art.peaks.is_none() {
+ studio.art_failed.insert(key.clone());
+ }
if let Some(frame) = art.thumbnail {
studio.media.thumbs.insert(art.id.clone(), image_of(&frame));
}
@@ -5245,8 +5278,31 @@ impl Studio {
log::warn!("{error}");
}
self.pause();
+ // The proxies of media no longer in the project, or read
+ // as a range they no longer are, go now: nothing else
+ // sweeps cache/proxy.
+ let project_dir = std::path::Path::new(session.path());
+ let kept: HashSet<std::path::PathBuf> = session
+ .project()
+ .media
+ .iter()
+ .filter_map(|item| {
+ concat_host::proxy::path_for(
+ project_dir,
+ &item.path,
+ item.color_range.map(concat_export::engine_range),
+ )
+ })
+ .collect();
+ let swept = concat_host::proxy::sweep(project_dir, &kept);
+ if swept > 0 {
+ log::info!("{swept} stale proxies swept from {}", project_dir.display());
+ }
self.session = Some(session);
crate::host::next_project_epoch();
+ self.art_failed.clear();
+ self.art_stamp = None;
+ self.proxied.clear();
self.echo = None;
self.dirty = false;
self.project_name = info.name.clone();
@@ -5343,6 +5399,9 @@ impl Studio {
// Whatever a worker still brings back for this project is dropped
// at delivery; the sheets that were waiting on one stop waiting.
crate::host::next_project_epoch();
+ self.art_failed.clear();
+ self.art_stamp = None;
+ self.proxied.clear();
self.captions.running = false;
self.captions.progress = 0.0;
self.speech.running = false;
@@ -6687,6 +6746,25 @@ impl Studio {
/// Asks the workers for anything the launch screen or the bin is
/// missing. Separate from `publish` because it mutates.
pub fn refresh_art(&mut self) {
+ // Called after every callback, the pointer stream included. The
+ // scan over every media item - stats for proxies, JPEGs read back,
+ // decodes queued - runs when the document, the screen or what is
+ // pending changed, and is one comparison otherwise. The lanes'
+ // cell strips follow the view, which moves on that stream, so
+ // they are asked for every time; that walk is hash lookups
+ // (audit 2026-09-23, #11).
+ let stamp = (
+ self.revision,
+ self.on_start,
+ self.art_pending.len() + self.posters_pending.len(),
+ );
+ if self.art_stamp == Some(stamp) {
+ if !self.on_start {
+ self.request_window_art();
+ }
+ return;
+ }
+ self.art_stamp = Some(stamp);
if self.on_start {
self.request_posters();
} else {