Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(editor): reuse the zoom timeline across paused scrub frames

qflen916d918defb2
diff --git a/apps/desktop-gpui/examples/scrub-benchmark.rs b/apps/desktop-gpui/examples/scrub-benchmark.rs
new file mode 100644
index 000000000..b9bf6e719
--- /dev/null
+++ b/apps/desktop-gpui/examples/scrub-benchmark.rs
@@ -0,0 +1,163 @@
+//! Paused-scrub benchmark: drags the playhead through an `EditorInstance` the
+//! way the editor's ruler drag does (`preview_tx` pushes at the 60 fps preview
+//! clock and the default Half preview size), on the app's two-worker runtime,
+//! and reports per-frame request-to-frame latency at several depths into the
+//! recording plus the process CPU time spent dragging.
+//!
+//! The editor can write `project-config.json` back into the project, so point
+//! it at a copy rather than a recording in the library.
+//!
+//! Run: cargo run --release --example scrub-benchmark -- <project.cap> [STEPS] [STEP_FRAMES]
+
+use std::{path::PathBuf, sync::Arc, time::Duration, time::Instant};
+
+use cap_editor::{AudioOutput, EditorFrameFormat, EditorFrameOutput, EditorInstance};
+use cap_project::XY;
+
+const PREVIEW_FPS: u32 = 60;
+const PREVIEW_RESOLUTION: XY<u32> = XY { x: 1248, y: 702 };
+const REGION_FRACTIONS: [f64; 5] = [0.02, 0.17, 0.5, 0.83, 0.98];
+
+fn cpu_secs() -> f64 {
+ let mut usage = std::mem::MaybeUninit::<libc::rusage>::zeroed();
+ unsafe { libc::getrusage(libc::RUSAGE_SELF, usage.as_mut_ptr()) };
+ let usage = unsafe { usage.assume_init() };
+ let secs = |time: libc::timeval| time.tv_sec as f64 + time.tv_usec as f64 / 1_000_000.0;
+ secs(usage.ru_utime) + secs(usage.ru_stime)
+}
+
+fn percentile(sorted: &[f64], fraction: f64) -> f64 {
+ sorted[((sorted.len() - 1) as f64 * fraction).round() as usize]
+}
+
+fn frame_number(output: &EditorFrameOutput) -> u32 {
+ match output {
+ EditorFrameOutput::Rgba(frame) => frame.frame_number,
+ EditorFrameOutput::Nv12(frame) => frame.frame_number,
+ #[cfg(target_os = "macos")]
+ EditorFrameOutput::Surface(frame) => frame.frame_number,
+ }
+}
+
+async fn render(
+ instance: &EditorInstance,
+ frames: &flume::Receiver<u32>,
+ frame: u32,
+) -> anyhow::Result<f64> {
+ while frames.try_recv().is_ok() {}
+ let requested = Instant::now();
+ instance
+ .preview_tx
+ .send_modify(|value| *value = Some((frame, PREVIEW_FPS, PREVIEW_RESOLUTION)));
+ loop {
+ let rendered = tokio::time::timeout(Duration::from_secs(30), frames.recv_async())
+ .await
+ .map_err(|_| anyhow::anyhow!("timed out waiting for frame {frame}"))??;
+ if rendered == frame {
+ return Ok(requested.elapsed().as_secs_f64() * 1000.0);
+ }
+ }
+}
+
+async fn run(project_path: PathBuf, steps: u32, step_frames: u32) -> anyhow::Result<()> {
+ #[cfg(target_os = "macos")]
+ let format = EditorFrameFormat::BgraSurface;
+ #[cfg(not(target_os = "macos"))]
+ let format = EditorFrameFormat::Rgba;
+
+ let (frame_tx, frame_rx) = flume::unbounded::<u32>();
+ let audio_output = Arc::new(AudioOutput::new_headless(Box::new(|_samples, _at| {})));
+ let instance = EditorInstance::new_with_audio_output_and_frame_format(
+ project_path.clone(),
+ |_state| {},
+ Box::new(move |output, _layout| {
+ let _ = frame_tx.send(frame_number(&output));
+ }),
+ None,
+ format,
+ audio_output,
+ )
+ .await
+ .map_err(anyhow::Error::msg)?;
+
+ let duration = instance
+ .project_config
+ .1
+ .borrow()
+ .timeline
+ .as_ref()
+ .map_or(0.0, |timeline| timeline.duration());
+ let last_frame = (duration * PREVIEW_FPS as f64) as u32;
+ render(&instance, &frame_rx, 0).await?;
+
+ let mut regions = Vec::new();
+ let mut drag = Vec::new();
+ let mut drag_cpu_secs = 0.0;
+ for fraction in REGION_FRACTIONS {
+ let start = ((last_frame as f64 * fraction) as u32)
+ .min(last_frame.saturating_sub(steps * step_frames));
+ let seek_ms = render(&instance, &frame_rx, start).await?;
+ let cpu_started = cpu_secs();
+ let mut latencies = Vec::with_capacity(steps as usize);
+ for step in 1..=steps {
+ latencies.push(render(&instance, &frame_rx, start + step * step_frames).await?);
+ }
+ drag_cpu_secs += cpu_secs() - cpu_started;
+ drag.extend_from_slice(&latencies);
+ latencies.sort_by(f64::total_cmp);
+ regions.push(serde_json::json!({
+ "atSecs": start as f64 / PREVIEW_FPS as f64,
+ "seekMs": seek_ms,
+ "dragMedianMs": percentile(&latencies, 0.5),
+ "dragP95Ms": percentile(&latencies, 0.95),
+ }));
+ }
+ drag.sort_by(f64::total_cmp);
+ println!(
+ "{}",
+ serde_json::json!({
+ "project": project_path,
+ "durationSecs": duration,
+ "steps": steps,
+ "stepFrames": step_frames,
+ "regions": regions,
+ "dragMedianMs": percentile(&drag, 0.5),
+ "dragP95Ms": percentile(&drag, 0.95),
+ "dragCpuSecs": drag_cpu_secs,
+ })
+ );
+ instance.dispose().await;
+ Ok(())
+}
+
+fn main() -> anyhow::Result<()> {
+ tracing_subscriber::fmt()
+ .with_env_filter(
+ tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "warn".into()),
+ )
+ .with_writer(std::io::stderr)
+ .init();
+
+ let mut arguments = std::env::args().skip(1);
+ let project_path = arguments
+ .next()
+ .map(PathBuf::from)
+ .ok_or_else(|| anyhow::anyhow!("Pass a studio .cap project path"))?;
+ let steps = arguments
+ .next()
+ .and_then(|value| value.parse().ok())
+ .unwrap_or(30u32)
+ .max(1);
+ let step_frames = arguments
+ .next()
+ .and_then(|value| value.parse().ok())
+ .unwrap_or(4u32)
+ .max(1);
+
+ tokio::runtime::Builder::new_multi_thread()
+ .worker_threads(2)
+ .thread_stack_size(16 * 1024 * 1024)
+ .enable_all()
+ .build()?
+ .block_on(run(project_path, steps, step_frames))
+}
diff --git a/crates/editor/src/editor_instance.rs b/crates/editor/src/editor_instance.rs
index f670c27a4..ea0569daa 100644
--- a/crates/editor/src/editor_instance.rs
+++ b/crates/editor/src/editor_instance.rs
@@ -12,6 +12,7 @@ use cap_rendering::{
get_duration, spring_mass_damper::SpringMassDamperSimulationConfig,
};
use std::{
+ collections::HashMap,
path::{Path, PathBuf},
sync::{
Arc,
@@ -983,6 +984,9 @@ impl EditorInstance {
tokio::spawn(async move {
let mut prefetch_cancel_token: Option<CancellationToken> = None;
let mut cursor_cache = PreviewCursorCache::default();
+ let mut project_rx = self.project_config.1.clone();
+ let mut zoom_timelines = HashMap::<u32, ZoomTransformTimeline>::new();
+ let mut outgoing_zoom_timelines = HashMap::<u32, ZoomTransformTimeline>::new();
loop {
preview_rx.changed().await.unwrap();
@@ -1007,7 +1011,14 @@ impl EditorInstance {
break;
}
- let project = self.project_config.1.borrow().clone();
+ let project = {
+ let project = project_rx.borrow_and_update();
+ if project.has_changed() {
+ zoom_timelines.clear();
+ outgoing_zoom_timelines.clear();
+ }
+ project.clone()
+ };
let frame_time = frame_number as f64 / fps as f64;
let transition_mapping = project.timeline.as_ref().and_then(|timeline| {
if timeline.transitions.is_empty() {
@@ -1073,15 +1084,21 @@ impl EditorInstance {
// Scrub renders sample the same precomputed spring
// timeline playback and export use (the old focus
// interpolator was never precomputed here and fell
- // back to a divergent direct interpolation).
- let mut zoom_timeline =
- ZoomTransformTimeline::from_project_for_clip(
- &project,
- &segment_medias.cursor,
- total_duration,
- self.render_constants.options.screen_size,
- segment.recording_clip,
- );
+ // back to a divergent direct interpolation). The
+ // timeline is kept until the project changes: it
+ // precomputes from t=0, so rebuilding it per frame
+ // made each scrub frame cost O(playhead).
+ let zoom_timeline = zoom_timelines
+ .entry(segment.recording_clip)
+ .or_insert_with(|| {
+ ZoomTransformTimeline::from_project_for_clip(
+ &project,
+ &segment_medias.cursor,
+ total_duration,
+ self.render_constants.options.screen_size,
+ segment.recording_clip,
+ )
+ });
zoom_timeline
.ensure_precomputed_until((frame_number as f32 + 1.0) / fps as f32);
@@ -1109,14 +1126,17 @@ impl EditorInstance {
) => frames,
};
if let Some(outgoing_frames) = outgoing_frames {
- let mut outgoing_zoom =
- ZoomTransformTimeline::from_project_for_outgoing_clip(
- &project,
- &outgoing_media.cursor,
- total_duration,
- self.render_constants.options.screen_size,
- outgoing.segment.recording_clip,
- );
+ let outgoing_zoom = outgoing_zoom_timelines
+ .entry(outgoing.segment.recording_clip)
+ .or_insert_with(|| {
+ ZoomTransformTimeline::from_project_for_outgoing_clip(
+ &project,
+ &outgoing_media.cursor,
+ total_duration,
+ self.render_constants.options.screen_size,
+ outgoing.segment.recording_clip,
+ )
+ });
outgoing_zoom.ensure_precomputed_until(
(frame_number as f32 + 1.0) / fps as f32,
);
@@ -1140,7 +1160,7 @@ impl EditorInstance {
&outgoing_media.cursor,
&outgoing_frames,
total_duration,
- &outgoing_zoom,
+ outgoing_zoom,
cursor_timeline,
)
} else {
@@ -1153,7 +1173,7 @@ impl EditorInstance {
&outgoing_media.cursor,
&outgoing_frames,
total_duration,
- &outgoing_zoom,
+ outgoing_zoom,
)
};
Some((
@@ -1201,7 +1221,7 @@ impl EditorInstance {
&segment_medias.cursor,
&segment_frames,
total_duration,
- &zoom_timeline,
+ zoom_timeline,
cursor_timeline,
)
} else {
@@ -1214,7 +1234,7 @@ impl EditorInstance {
&segment_medias.cursor,
&segment_frames,
total_duration,
- &zoom_timeline,
+ zoom_timeline,
)
};