Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

improve: merge fullscreen hold windows in place

Ehsan-hash6456ae044c39
diff --git a/crates/project/examples/hold-windows-benchmark.rs b/crates/project/examples/hold-windows-benchmark.rs
new file mode 100644
index 000000000..d93658a51
--- /dev/null
+++ b/crates/project/examples/hold-windows-benchmark.rs
@@ -0,0 +1,61 @@
+use cap_project::TimelineConfiguration;
+use serde_json::json;
+use std::{error::Error, hint::black_box, time::Instant};
+
+fn main() -> Result<(), Box<dyn Error>> {
+ let iterations = std::env::args()
+ .nth(1)
+ .map(|value| value.parse::<usize>())
+ .transpose()?
+ .unwrap_or(100_000);
+ if iterations == 0 {
+ return Err("iterations must be positive".into());
+ }
+ println!("segments,overlapping,operation,sample,ns_per_call");
+ for count in [0_usize, 1, 8, 32, 128] {
+ for overlapping in [false, true] {
+ let text_segments: Vec<_> = (0..count)
+ .map(|index| {
+ let start = if overlapping {
+ (index / 2) as f64 * 3.0 + (index % 2) as f64 * 0.25
+ } else {
+ index as f64 * 3.0
+ };
+ json!({"start": start, "end": start + 0.5, "layout": "fullscreen"})
+ })
+ .collect();
+ let timeline: TimelineConfiguration = serde_json::from_value(json!({
+ "segments": [{"start": 0.0, "end": 3600.0, "timescale": 1.0, "recordingClip": 0}],
+ "zoomSegments": [],
+ "textSegments": text_segments
+ }))?;
+ let expected_count = if overlapping {
+ count.div_ceil(2)
+ } else {
+ count
+ };
+ assert_eq!(timeline.hold_windows().len(), expected_count);
+ let frame_time = count as f64 * 3.0 + 1.0;
+ assert!(timeline.get_frame_mapping(frame_time).is_some());
+ for sample in 0..5 {
+ for _ in 0..1000 {
+ black_box(black_box(&timeline).hold_windows());
+ black_box(black_box(&timeline).get_frame_mapping(black_box(frame_time)));
+ }
+ let start = Instant::now();
+ for _ in 0..iterations {
+ black_box(black_box(&timeline).hold_windows());
+ }
+ let elapsed = start.elapsed().as_nanos() as f64 / iterations as f64;
+ println!("{count},{overlapping},hold_windows,{sample},{elapsed:.3}");
+ let start = Instant::now();
+ for _ in 0..iterations {
+ black_box(black_box(&timeline).get_frame_mapping(black_box(frame_time)));
+ }
+ let elapsed = start.elapsed().as_nanos() as f64 / iterations as f64;
+ println!("{count},{overlapping},frame_mapping,{sample},{elapsed:.3}");
+ }
+ }
+ }
+ Ok(())
+}
diff --git a/crates/project/src/configuration.rs b/crates/project/src/configuration.rs
index a519723fe..98127f6c3 100644
--- a/crates/project/src/configuration.rs
+++ b/crates/project/src/configuration.rs
@@ -1927,14 +1927,15 @@ impl TimelineConfiguration {
return windows;
}
windows.sort_by(|a, b| a.0.total_cmp(&b.0));
- let mut merged: Vec<(f64, f64)> = Vec::with_capacity(windows.len());
- for window in windows {
- match merged.last_mut() {
- Some(last) if window.0 <= last.1 => last.1 = last.1.max(window.1),
- _ => merged.push(window),
+ windows.dedup_by(|next, previous| {
+ if next.0 <= previous.1 {
+ previous.1 = previous.1.max(next.1);
+ true
+ } else {
+ false
}
- }
- merged
+ });
+ windows
}
/// Total output seconds inserted by fullscreen text holds.
@@ -3701,6 +3702,25 @@ mod tests {
assert_eq!(after, 2.5);
}
+ #[test]
+ fn fullscreen_holds_merge_unsorted_nested_and_touching_windows() {
+ let mut timeline = timeline_with_transitions(Vec::new());
+ timeline.text_segments = vec![
+ fullscreen_text(8.0, 9.0),
+ fullscreen_text(4.0, 6.0),
+ fullscreen_text(3.0, 4.0),
+ fullscreen_text(2.0, 5.0),
+ fullscreen_text(6.0, 7.0),
+ fullscreen_text(1.0, 1.0),
+ fullscreen_text(10.0, 9.0),
+ ];
+
+ assert_eq!(timeline.hold_windows(), vec![(2.0, 7.0), (8.0, 9.0)]);
+ assert_eq!(timeline.held_duration(), 6.0);
+ assert_eq!(timeline.text_segments[0].start, 8.0);
+ assert_eq!(timeline.text_segments[3].end, 5.0);
+ }
+
#[test]
fn holds_compose_with_transitions() {
let mut timeline = timeline_with_transitions(vec![ClipTransition {