Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(project): skip caption-track copies without a timeline

vedanth76c009fd9b6e
diff --git a/crates/project/examples/caption-loading-benchmark.rs b/crates/project/examples/caption-loading-benchmark.rs
new file mode 100644
index 000000000..3ed45cba7
--- /dev/null
+++ b/crates/project/examples/caption-loading-benchmark.rs
@@ -0,0 +1,92 @@
+use cap_project::{CaptionSegment, CaptionWord, CaptionsData, ProjectConfiguration, RecordingMeta};
+use std::{
+ collections::hash_map::DefaultHasher,
+ hash::{Hash, Hasher},
+ hint::black_box,
+ time::Instant,
+};
+
+fn main() {
+ for (name, count, timeline, sidecar) in [
+ ("empty", 0, 0, false),
+ ("30_no_timeline", 30, 0, false),
+ ("150_no_timeline", 150, 0, false),
+ ("900_no_timeline", 900, 0, false),
+ ("900_sidecar_no_timeline", 900, 0, true),
+ ("900_empty_timeline", 900, 1, false),
+ ("900_existing_track", 900, 2, false),
+ ] {
+ let captions = CaptionsData {
+ source_timed: true,
+ segments: (0..count)
+ .map(|index| CaptionSegment {
+ id: format!("source-{index}"),
+ start: index as f32 * 4.0,
+ end: index as f32 * 4.0 + 4.0,
+ text: "A caption with café and Unicode 字 to preserve".into(),
+ words: (0..10)
+ .map(|word| CaptionWord {
+ text: format!("word{word} café 字"),
+ start: index as f32 * 4.0 + word as f32 * 0.4,
+ end: index as f32 * 4.0 + (word + 1) as f32 * 0.4,
+ })
+ .collect(),
+ })
+ .collect(),
+ ..Default::default()
+ };
+ let directory = tempfile::tempdir().unwrap();
+ let mut meta: RecordingMeta = serde_json::from_value(serde_json::json!({
+ "pretty_name": "Caption loading benchmark",
+ "display": {"path": "unused.mp4", "fps": 30},
+ "camera": null, "audio": null, "cursor": null,
+ }))
+ .unwrap();
+ meta.project_path = directory.path().to_path_buf();
+ let mut config = ProjectConfiguration {
+ captions: (!sidecar).then(|| captions.clone()),
+ ..Default::default()
+ };
+ if timeline > 0 {
+ config.timeline = Some(serde_json::from_value(serde_json::json!({
+ "segments": [{"start": 0.0, "end": 3600.0, "timescale": 1.0}], "zoomSegments": [],
+ "captionSegments": if timeline == 2 { serde_json::json!([{"id": "saved", "start": 0.0, "end": 1.0, "text": "Saved override", "words": []}]) } else { serde_json::json!([]) },
+ })).unwrap());
+ }
+ config.write(directory.path()).unwrap();
+ if sidecar {
+ std::fs::write(
+ directory.path().join("captions.json"),
+ serde_json::to_vec(&captions).unwrap(),
+ )
+ .unwrap();
+ }
+ for _ in 0..3 {
+ black_box(meta.project_config());
+ }
+ let output = meta.project_config();
+ assert_eq!(output.captions.as_ref().unwrap().segments.len(), count);
+ if timeline == 0 {
+ assert!(output.timeline.is_none());
+ } else {
+ assert_eq!(
+ output.timeline.as_ref().unwrap().caption_segments.len(),
+ if timeline == 2 { 1 } else { count }
+ );
+ }
+ let mut hash = DefaultHasher::new();
+ serde_json::to_vec(&output).unwrap().hash(&mut hash);
+ let fingerprint = hash.finish();
+ for sample in 0..15 {
+ let start = Instant::now();
+ for _ in 0..10 {
+ black_box(black_box(&meta).project_config());
+ }
+ println!(
+ "LOAD_BENCH {}",
+ serde_json::json!({"case": name, "sample": sample,
+ "ns_per_call": start.elapsed().as_nanos() as f64 / 10.0, "fingerprint": fingerprint})
+ );
+ }
+ }
+}
diff --git a/crates/project/src/meta.rs b/crates/project/src/meta.rs
index f5388250f..6c5eb7599 100644
--- a/crates/project/src/meta.rs
+++ b/crates/project/src/meta.rs
@@ -276,13 +276,12 @@ impl RecordingMeta {
}
if let Some(ref captions) = config.captions {
- let timeline_has_captions = config
+ let timeline_needs_captions = config
.timeline
.as_ref()
- .map(|t| !t.caption_segments.is_empty())
- .unwrap_or(false);
+ .is_some_and(|timeline| timeline.caption_segments.is_empty());
- if !timeline_has_captions && !captions.segments.is_empty() {
+ if timeline_needs_captions && !captions.segments.is_empty() {
let caption_track_segments: Vec<crate::CaptionTrackSegment> = captions
.segments
.iter()
@@ -824,6 +823,80 @@ mod metadata_save_tests {
}));
}
+ #[test]
+ fn caption_loading_preserves_missing_empty_and_populated_timelines() {
+ let captions = CaptionsData {
+ source_timed: true,
+ segments: vec![crate::CaptionSegment {
+ id: "source-caption".into(),
+ start: 1.25,
+ end: 2.75,
+ text: "Café 字".into(),
+ words: vec![crate::CaptionWord {
+ text: "Café".into(),
+ start: 1.25,
+ end: 2.0,
+ }],
+ }],
+ ..Default::default()
+ };
+ let saved: crate::CaptionTrackSegment = serde_json::from_value(serde_json::json!({
+ "id": "saved", "start": 5.0, "end": 6.0, "text": "Saved", "words": [],
+ "colorOverride": "#123456", "fadeDurationOverride": 0.75,
+ }))
+ .unwrap();
+ let generated = serde_json::json!([{
+ "id": "source-caption", "start": 1.25, "end": 2.75, "text": "Café 字",
+ "words": [{"text": "Café", "start": 1.25, "end": 2.0}],
+ "fadeDurationOverride": null, "lingerDurationOverride": null,
+ "positionOverride": null, "colorOverride": null,
+ "backgroundColorOverride": null, "fontSizeOverride": null,
+ }]);
+ for sidecar in [false, true] {
+ for timeline in 0..3 {
+ let directory = tempfile::tempdir().unwrap();
+ let meta = recording(directory.path());
+ let mut config = ProjectConfiguration {
+ captions: (!sidecar).then(|| captions.clone()),
+ ..Default::default()
+ };
+ if timeline > 0 {
+ config.timeline = Some(serde_json::from_value(serde_json::json!({
+ "segments": [], "zoomSegments": [],
+ "captionSegments": if timeline == 2 { serde_json::to_value([&saved]).unwrap() } else { serde_json::json!([]) },
+ })).unwrap());
+ }
+ if !sidecar || timeline > 0 {
+ config.write(directory.path()).unwrap();
+ }
+ if sidecar {
+ std::fs::write(
+ directory.path().join("captions.json"),
+ serde_json::to_vec(&captions).unwrap(),
+ )
+ .unwrap();
+ }
+ let loaded = meta.project_config();
+ assert_eq!(
+ serde_json::to_value(loaded.captions.unwrap()).unwrap(),
+ serde_json::to_value(&captions).unwrap()
+ );
+ if timeline == 0 {
+ assert!(loaded.timeline.is_none());
+ } else {
+ assert_eq!(
+ serde_json::to_value(loaded.timeline.unwrap().caption_segments).unwrap(),
+ if timeline == 1 {
+ generated.clone()
+ } else {
+ serde_json::to_value([&saved]).unwrap()
+ }
+ );
+ }
+ }
+ }
+ }
+
#[test]
fn saved_caption_master_takes_precedence_over_legacy_sidecar() {
let directory = tempfile::tempdir().unwrap();