A project naming many tracks of one file walks it once
Browse files at this commit95fb78d690eccf802f5eb8e7a3ec9dc2783935f5Parent: 21cd97f- perf
- a project naming many tracks of one file walks it once
3 changed files +146 -24
- crates/engine/src/playback.rs +7 -7
- crates/engine/src/subtitle.rs +50 -17
- crates/engine/tests/subtitles.rs +89 -0
crates/engine/src/playback.rsmodified+7 -7
| @@ -782,13 +782,13 @@ impl PlaybackSession { | |||
| 782 | // The subtitle files are read here rather than in the parser: a | 782 | // The subtitle files are read here rather than in the parser: a |
| 783 | // `.edith` names them and holds no cues. One that has gone missing, or | 783 | // `.edith` names them and holds no cues. One that has gone missing, or |
| 784 | // whose track is a codec of pictures, comes back listed and refused by | 784 | // whose track is a codec of pictures, comes back listed and refused by |
| 785 | - // name ([`crate::subtitle::open`]) -- a project does not stop opening | 785 | + // name ([`crate::subtitle::open_all`]) -- a project does not stop |
| 786 | - // over a subtitle, and a re-save does not lose the row. | 786 | + // opening over a subtitle, and a re-save does not lose the row. |
| 787 | - let subtitles = doc | 787 | + // |
| 788 | - .subtitles | 788 | + // All of them in one call, in saved order, because a file is walked |
| 789 | - .iter() | 789 | + // whole to reach one track: a film whose project names many of its |
| 790 | - .map(|(path, track)| crate::subtitle::open(path, *track)) | 790 | + // tracks is one walk here rather than one per row. |
| 791 | - .collect(); | 791 | + let subtitles = crate::subtitle::open_all(&doc.subtitles); |
| 792 | let project = Project::from_parts(doc.sources, doc.lanes, doc.eq, doc.color)? | 792 | let project = Project::from_parts(doc.sources, doc.lanes, doc.eq, doc.color)? |
| 793 | .with_mix(&doc.gains, doc.limiter) | 793 | .with_mix(&doc.gains, doc.limiter) |
| 794 | .with_tone(doc.tone) | 794 | .with_tone(doc.tone) |
crates/engine/src/subtitle.rsmodified+50 -17
| @@ -26,6 +26,7 @@ | |||
| 26 | //! here reads, and a file carrying one opens with a row that says so instead of | 26 | //! here reads, and a file carrying one opens with a row that says so instead of |
| 27 | //! opening with an empty list. | 27 | //! opening with an empty list. |
| 28 | 28 | ||
| 29 | +use std::collections::HashMap; | ||
| 29 | use std::path::{Path, PathBuf}; | 30 | use std::path::{Path, PathBuf}; |
| 30 | use std::sync::Arc; | 31 | use std::sync::Arc; |
| 31 | 32 | ||
| @@ -201,23 +202,55 @@ impl SubtitleTrack { | |||
| 201 | /// because a project that opened yesterday has to open today -- a subtitle is | 202 | /// because a project that opened yesterday has to open today -- a subtitle is |
| 202 | /// not what a whole timeline should refuse over. | 203 | /// not what a whole timeline should refuse over. |
| 203 | pub fn open(path: &Path, track: Option<u64>) -> SubtitleTrack { | 204 | pub fn open(path: &Path, track: Option<u64>) -> SubtitleTrack { |
| 204 | - let Some(number) = track else { | 205 | + let mut one = open_all(std::slice::from_ref(&(path.to_path_buf(), track))); |
| 205 | - return external(path); | 206 | + one.remove(0) |
| 206 | - }; | 207 | +} |
| 207 | - let label = format!("track {number}"); | 208 | + |
| 208 | - let mut tracks = match of_matroska(path) { | 209 | +/// Every saved subtitle line of a project at once, back in the order they were |
| 209 | - Ok(tracks) => tracks, | 210 | +/// saved in -- which is the order they are picked by, so it is the row order |
| 210 | - Err(why) => return SubtitleTrack::refused(path, track, label, why.to_string()), | 211 | +/// this hands back whatever it had to read. |
| 211 | - }; | 212 | +/// |
| 212 | - match tracks.iter().position(|t| t.track == Some(number)) { | 213 | +/// The reason this exists beside [`open`]: a Matroska file is walked whole to |
| 213 | - Some(i) => tracks.swap_remove(i), | 214 | +/// reach *one* of its tracks, and a film whose project names thirty-five of them |
| 214 | - None => SubtitleTrack::refused( | 215 | +/// was thirty-five walks of a 25 GB file -- seven seconds with the window |
| 215 | - path, | 216 | +/// stopped, since a project opens on the UI thread. Each distinct file is walked |
| 216 | - track, | 217 | +/// once here and every row that named it is picked out of that one walk. |
| 217 | - label, | 218 | +pub fn open_all(rows: &[(PathBuf, Option<u64>)]) -> Vec<SubtitleTrack> { |
| 218 | - format!("no subtitle track {number} in {} any more", path.display()), | 219 | + // Keyed by path, so a second row naming the same file finds the walk rather |
| 219 | - ), | 220 | + // than doing it again. The whole walk is kept until the last row is served: |
| 220 | - } | 221 | + // it is the tracks nobody asked for that are dropped at the end. |
| 222 | + // | ||
| 223 | + // ponytail: a track is *moved* out of its walk, so two rows naming the same | ||
| 224 | + // file and the same track leave the second refused by name rather than | ||
| 225 | + // holding the cues twice. No project edith saves has one -- a row is added | ||
| 226 | + // by (file, track) and only once ([`crate::Project::add_subtitles`]) -- and | ||
| 227 | + // a hand-written `.edith` with a doubled row still opens, with that row | ||
| 228 | + // saying so. Clone the picked track instead if that ever has to hold. | ||
| 229 | + let mut walked: HashMap<&Path, crate::Result<Vec<SubtitleTrack>>> = HashMap::new(); | ||
| 230 | + rows.iter() | ||
| 231 | + .map(|(path, track)| { | ||
| 232 | + let Some(number) = *track else { | ||
| 233 | + return external(path); | ||
| 234 | + }; | ||
| 235 | + let label = format!("track {number}"); | ||
| 236 | + let tracks = walked | ||
| 237 | + .entry(path.as_path()) | ||
| 238 | + .or_insert_with(|| of_matroska(path)); | ||
| 239 | + let tracks = match tracks { | ||
| 240 | + Ok(tracks) => tracks, | ||
| 241 | + Err(why) => return SubtitleTrack::refused(path, *track, label, why.to_string()), | ||
| 242 | + }; | ||
| 243 | + match tracks.iter().position(|t| t.track == Some(number)) { | ||
| 244 | + Some(i) => tracks.swap_remove(i), | ||
| 245 | + None => SubtitleTrack::refused( | ||
| 246 | + path, | ||
| 247 | + *track, | ||
| 248 | + label, | ||
| 249 | + format!("no subtitle track {number} in {} any more", path.display()), | ||
| 250 | + ), | ||
| 251 | + } | ||
| 252 | + }) | ||
| 253 | + .collect() | ||
| 221 | } | 254 | } |
| 222 | 255 | ||
| 223 | /// A standalone subtitle file, parsed by its extension. | 256 | /// A standalone subtitle file, parsed by its extension. |
crates/engine/tests/subtitles.rsmodified+89 -0
| @@ -338,6 +338,95 @@ fn a_project_keeps_a_bitmap_track_as_a_reference_and_reads_its_pictures_back() { | |||
| 338 | let image = track.cues[0].image.as_ref().expect("the picture is back"); | 338 | let image = track.cues[0].image.as_ref().expect("the picture is back"); |
| 339 | assert_eq!((image.width, image.height), (8, 4)); | 339 | assert_eq!((image.width, image.height), (8, 4)); |
| 340 | assert_eq!(image.rgba().expect("it decodes").len(), 8 * 4 * 4); | 340 | assert_eq!(image.rgba().expect("it decodes").len(), 8 * 4 * 4); |
| 341 | + | ||
| 342 | + // Each row is the track it was saved as, in the order it was saved in: what | ||
| 343 | + // a click picks is a place in this list, so a load that reordered the rows | ||
| 344 | + // would repoint the picked track without saying anything. A re-save writing | ||
| 345 | + // the same bytes is that order proved from both ends. | ||
| 346 | + assert_eq!( | ||
| 347 | + back.subtitles().iter().map(|t| t.track).collect::<Vec<_>>(), | ||
| 348 | + vec![Some(1), Some(2), Some(3), Some(4)] | ||
| 349 | + ); | ||
| 350 | + let again = dir.join("cut2.edith"); | ||
| 351 | + back.save_project(&again).expect("save again"); | ||
| 352 | + assert_eq!( | ||
| 353 | + std::fs::read(&again).expect("read"), | ||
| 354 | + std::fs::read(&project).expect("read"), | ||
| 355 | + "a save, a load and a save again are the same file" | ||
| 356 | + ); | ||
| 357 | +} | ||
| 358 | + | ||
| 359 | +/// Bytes this process has asked the kernel for so far, which is what a walk of | ||
| 360 | +/// a Matroska file costs and a page cache cannot hide: `rchar` counts what | ||
| 361 | +/// `read` handed back, warm or cold. | ||
| 362 | +fn read_bytes() -> u64 { | ||
| 363 | + let io = std::fs::read_to_string("/proc/self/io").expect("Linux, like the rest of this suite"); | ||
| 364 | + io.lines() | ||
| 365 | + .find_map(|l| l.strip_prefix("rchar: ")) | ||
| 366 | + .expect("rchar") | ||
| 367 | + .trim() | ||
| 368 | + .parse() | ||
| 369 | + .expect("a number") | ||
| 370 | +} | ||
| 371 | + | ||
| 372 | +/// A project naming four tracks of one file walks that file *once*, and hands | ||
| 373 | +/// the rows back in the order they were saved in. | ||
| 374 | +/// | ||
| 375 | +/// The order is the invariant with teeth: a subtitle is picked by its place in | ||
| 376 | +/// this list, so a reader that grouped the rows by file and handed them back | ||
| 377 | +/// grouped would silently repoint what the user had picked. And the walk count | ||
| 378 | +/// is the reason the grouping exists at all -- a saved film with thirty-five | ||
| 379 | +/// tracks was thirty-five walks of a 25 GB file with the window stopped. | ||
| 380 | +#[test] | ||
| 381 | +fn a_project_naming_many_tracks_of_one_file_walks_it_once_and_keeps_its_order() { | ||
| 382 | + let dir = scratch("one_walk"); | ||
| 383 | + let media = dir.join("test_av.mp4"); | ||
| 384 | + std::fs::copy(asset("test_av.mp4"), &media).expect("copy the media fixture"); | ||
| 385 | + let remux = dir.join("remux.mkv"); | ||
| 386 | + std::fs::write(&remux, pgs_matroska(pgs_display_set())).expect("write the hand-made mkv"); | ||
| 387 | + let subs = dir.join("test_subs.srt"); | ||
| 388 | + std::fs::copy(data("test_subs.srt"), &subs).expect("copy the subtitle fixture"); | ||
| 389 | + | ||
| 390 | + // One walk, for the measurement below to be compared against. | ||
| 391 | + let before = read_bytes(); | ||
| 392 | + let all = subtitle::of_matroska(&remux).expect("the walk"); | ||
| 393 | + let one_walk = read_bytes() - before; | ||
| 394 | + assert_eq!(all.len(), 4); | ||
| 395 | + assert!(one_walk > 0, "a walk reads something"); | ||
| 396 | + | ||
| 397 | + // The rows a `.edith` holds, deliberately out of file order and with the | ||
| 398 | + // standalone file in the middle of them. | ||
| 399 | + let rows: Vec<(PathBuf, Option<u64>)> = vec![ | ||
| 400 | + (remux.clone(), Some(3)), | ||
| 401 | + (remux.clone(), Some(1)), | ||
| 402 | + (subs.clone(), None), | ||
| 403 | + (remux.clone(), Some(4)), | ||
| 404 | + (remux.clone(), Some(2)), | ||
| 405 | + (remux.clone(), Some(9)), // gone: refused by name, still a row | ||
| 406 | + ]; | ||
| 407 | + let before = read_bytes(); | ||
| 408 | + let opened = subtitle::open_all(&rows); | ||
| 409 | + let cost = read_bytes() - before; | ||
| 410 | + assert_eq!( | ||
| 411 | + opened.iter().map(|t| (&t.path, t.track)).collect::<Vec<_>>(), | ||
| 412 | + rows.iter().map(|(p, n)| (p, *n)).collect::<Vec<_>>(), | ||
| 413 | + "every row comes back where it was saved" | ||
| 414 | + ); | ||
| 415 | + assert_eq!(opened[1].label, "eng", "and it is that file's track 1"); | ||
| 416 | + assert_eq!(opened[2].cues, expected(), "the standalone row is untouched"); | ||
| 417 | + assert!( | ||
| 418 | + opened[5] | ||
| 419 | + .refused | ||
| 420 | + .as_deref() | ||
| 421 | + .unwrap_or_default() | ||
| 422 | + .contains("no subtitle track 9"), | ||
| 423 | + "{:?}", | ||
| 424 | + opened[5].refused | ||
| 425 | + ); | ||
| 426 | + assert!( | ||
| 427 | + cost < one_walk * 2, | ||
| 428 | + "four rows of one file is one walk of it: {cost} bytes read against {one_walk} for a walk" | ||
| 429 | + ); | ||
| 341 | } | 430 | } |
| 342 | 431 | ||
| 343 | /// A refusal a front-end shows at the moment of the import, rather than a row | 432 | /// A refusal a front-end shows at the moment of the import, rather than a row |