forge
Explore
Log in

Don't you dare go Hollow

A header the session has already read is not read again

Browse files at this commit

739bec4513f0f81b34ec90dd32f77bdf2dc89457Ahmet Kaan GümüşParent: edbb3e3
  • perf
    • a header the session has already read is not read again
Show full message
⚡ perf(engine): a header the session has already read is not read again

`place_stream_at` and `import_audio` each open two containers on the thread
that draws -- the file's own audio header and the timeline's first source's
([`audio_matches`], [`first_audio_of`]) -- and a drop is exactly when that
thread is being watched. Measured with `AudioSession::probe`: 10.4 ms warm on a
4.3 GB 1080p H.264 mp4, 6.7 ms on a 12.9 GB 2160p AV1 mkv, and 1701.7 ms on a
3.1 GB 1080p HEVC mkv with its pages cold, against 5.7 ms for that same file
warm.

A header does not change while a session holds the file, so the session keeps
the ones it has read and both doors ask it instead. The free functions stay for
`probe_import`, which runs on a worker and is supposed to pay.

Proved by answering rather than by timing: the memo is filled with a layout the
file does not have and the place is refused in that layout's words, which only a
door reading the memo can do.
crates/engine/src/playback.rsmodified+113 -5
@@ -270,6 +270,17 @@ pub struct PlaybackSession {
270 /// not -- with the single exception `sources` itself has,270 /// not -- with the single exception `sources` itself has,
271 /// [`Self::remove_source`], which takes the same entry out of both.271 /// [`Self::remove_source`], which takes the same entry out of both.
272 counts: Vec<u32>,272 counts: Vec<u32>,
273+ /// Audio headers already read, by the `(path, stream)` they were read for.
274+ /// Every door that checks a file against the timeline asks the same two
275+ /// questions -- what this file's sound is, and what the timeline's first
276+ /// source's is ([`audio_matches`], [`first_audio_of`]) -- and each answer is
277+ /// a container open. Warm that is 5-10 ms; on a film whose pages have been
278+ /// evicted it is **1.7 s** (measured, 3.1 GB HEVC mkv), and both of those
279+ /// are spent on the thread that draws, because placing and importing are
280+ /// what a drag lands on. A header does not change while a session holds the
281+ /// file, so it is read once and remembered. Errors are not kept: a file that
282+ /// failed to open is asked again next time.
283+ probes: std::collections::HashMap<(std::path::PathBuf, usize), Option<crate::AudioProbe>>,
273 /// What each source's own frame rate is against this timeline's, indexed284 /// What each source's own frame rate is against this timeline's, indexed
274 /// and grown exactly as [`Self::counts`] is (and shortened with it by285 /// and grown exactly as [`Self::counts`] is (and shortened with it by
275 /// [`Self::remove_source`]). [`Rate::REAL_TIME`] for a file shot at the286 /// [`Self::remove_source`]). [`Rate::REAL_TIME`] for a file shot at the
@@ -416,6 +427,7 @@ impl PlaybackSession {
416 audio_disabled,427 audio_disabled,
417 project,428 project,
418 counts: vec![meta.frame_count],429 counts: vec![meta.frame_count],
430+ probes: std::collections::HashMap::new(),
419 // Source 0 *is* the timeline's rate: it defined it.431 // Source 0 *is* the timeline's rate: it defined it.
420 rates: vec![Rate::REAL_TIME],432 rates: vec![Rate::REAL_TIME],
421 span_rate: Rate::REAL_TIME,433 span_rate: Rate::REAL_TIME,
@@ -526,6 +538,7 @@ impl PlaybackSession {
526 audio_disabled,538 audio_disabled,
527 project,539 project,
528 counts: vec![meta.frame_count],540 counts: vec![meta.frame_count],
541+ probes: std::collections::HashMap::new(),
529 // Source 0 *is* the timeline's rate: it defined it.542 // Source 0 *is* the timeline's rate: it defined it.
530 rates: vec![Rate::REAL_TIME],543 rates: vec![Rate::REAL_TIME],
531 span_rate: Rate::REAL_TIME,544 span_rate: Rate::REAL_TIME,
@@ -600,6 +613,7 @@ impl PlaybackSession {
600 audio_disabled: None,613 audio_disabled: None,
601 project,614 project,
602 counts: vec![meta.frame_count],615 counts: vec![meta.frame_count],
616+ probes: std::collections::HashMap::new(),
603 // Source 0 *is* the timeline's rate: it defined it.617 // Source 0 *is* the timeline's rate: it defined it.
604 rates: vec![Rate::REAL_TIME],618 rates: vec![Rate::REAL_TIME],
605 span_rate: Rate::REAL_TIME,619 span_rate: Rate::REAL_TIME,
@@ -892,6 +906,7 @@ impl PlaybackSession {
892 audio_disabled,906 audio_disabled,
893 project,907 project,
894 counts,908 counts,
909+ probes: std::collections::HashMap::new(),
895 rates,910 rates,
896 span_rate: Rate::REAL_TIME,911 span_rate: Rate::REAL_TIME,
897 span,912 span,
@@ -2024,7 +2039,7 @@ impl PlaybackSession {
2024 let image = crate::is_image(path);2039 let image = crate::is_image(path);
2025 if !image {2040 if !image {
2026 let first = self.first_audio()?;2041 let first = self.first_audio()?;
2027- audio_matches(&wanted, &first)?;2042+ self.audio_matches_cached(&wanted, &first)?;
2028 }2043 }
2029 let rate = self.file_rate(&wanted.path);2044 let rate = self.file_rate(&wanted.path);
2030 let source = self.project.import(path, stream);2045 let source = self.project.import(path, stream);
@@ -2136,8 +2151,44 @@ impl PlaybackSession {
2136 /// What the timeline's audio *is*: the chosen stream of the first source2151 /// What the timeline's audio *is*: the chosen stream of the first source
2137 /// that could have any ([`audio_source_of`]), probed. Every other source is2152 /// that could have any ([`audio_source_of`]), probed. Every other source is
2138 /// held to it.2153 /// held to it.
2139- fn first_audio(&self) -> crate::Result<Option<crate::AudioProbe>> {2154+ ///
2140- first_audio_of(self.project.sources())2155+ /// Through the session's own memo ([`probes`](Self::probes)), because this
2156+ /// is asked on the render thread: the free [`first_audio_of`] is the one a
2157+ /// worker with no session to reach for calls.
2158+ fn first_audio(&mut self) -> crate::Result<Option<crate::AudioProbe>> {
2159+ match audio_source_of(self.project.sources()) {
2160+ Some(first) => {
2161+ let (path, stream) = (first.path.clone(), first.audio_stream);
2162+ self.probe_cached(&path, stream)
2163+ }
2164+ None => Ok(None),
2165+ }
2166+ }
2167+
2168+ /// One audio header, read at most once per session per `(path, stream)`.
2169+ fn probe_cached(
2170+ &mut self,
2171+ path: &Path,
2172+ stream: usize,
2173+ ) -> crate::Result<Option<crate::AudioProbe>> {
2174+ let key = (path.to_path_buf(), stream);
2175+ if let Some(probe) = self.probes.get(&key) {
2176+ return Ok(*probe);
2177+ }
2178+ let probe = AudioSession::probe(path, stream)?;
2179+ self.probes.insert(key, probe);
2180+ Ok(probe)
2181+ }
2182+
2183+ /// [`audio_matches`] with this session's memo behind the new file's own
2184+ /// header too -- the second of the two opens a place or an import pays.
2185+ fn audio_matches_cached(
2186+ &mut self,
2187+ source: &Source,
2188+ first: &Option<crate::AudioProbe>,
2189+ ) -> crate::Result<()> {
2190+ let probe = self.probe_cached(&source.path, source.audio_stream)?;
2191+ audio_matches_probed(probe, first)
2141 }2192 }
2142 2193
2143 /// Places `clip` on `lane` alone at the playhead, overwriting what it lands2194 /// Places `clip` on `lane` alone at the playhead, overwriting what it lands
@@ -2289,7 +2340,7 @@ impl PlaybackSession {
2289 // Stream 0, and there is no other: a standalone audio file carries one2340 // Stream 0, and there is no other: a standalone audio file carries one
2290 // track (`AudioSession::probe_streams`), so nothing here has a stream2341 // track (`AudioSession::probe_streams`), so nothing here has a stream
2291 // to pick the way `place_stream_at` does for an mp4.2342 // to pick the way `place_stream_at` does for an mp4.
2292- audio_matches(&Source::new(path, 0), &first)?;2343+ self.audio_matches_cached(&Source::new(path, 0), &first)?;
2293 let frames = audio_frames(path, self.meta.frame_rate)?;2344 let frames = audio_frames(path, self.meta.frame_rate)?;
2294 let source = self.project.import(path, 0);2345 let source = self.project.import(path, 0);
2295 self.note_frames(source, frames, Rate::REAL_TIME);2346 self.note_frames(source, frames, Rate::REAL_TIME);
@@ -2881,12 +2932,22 @@ fn audio_source_of(sources: &[Source]) -> Option<&Source> {
2881 }2932 }
2882 2933
2883 fn audio_matches(source: &Source, first: &Option<crate::AudioProbe>) -> crate::Result<()> {2934 fn audio_matches(source: &Source, first: &Option<crate::AudioProbe>) -> crate::Result<()> {
2935+ let probe = AudioSession::probe(&source.path, source.audio_stream)?;
2936+ audio_matches_probed(probe, first)
2937+}
2938+
2939+/// The verdict alone, once the file's own header is in hand: the half a session
2940+/// with a memo behind it ([`PlaybackSession::audio_matches_cached`]) does not
2941+/// have to open the file for.
2942+fn audio_matches_probed(
2943+ probe: Option<crate::AudioProbe>,
2944+ first: &Option<crate::AudioProbe>,
2945+) -> crate::Result<()> {
2884 // The layout, which the audio worker holds every source to anyway -- and not2946 // The layout, which the audio worker holds every source to anyway -- and not
2885 // the rate any more: a file written at another one is resampled to the2947 // the rate any more: a file written at another one is resampled to the
2886 // timeline's at the decoder's door ([`crate::audio::Resample`]), exactly as2948 // timeline's at the decoder's door ([`crate::audio::Resample`]), exactly as
2887 // a file shot at another frame rate is read through [`Rate`]. Both silent is2949 // a file shot at another frame rate is read through [`Rate`]. Both silent is
2888 // a match.2950 // a match.
2889- let probe = AudioSession::probe(&source.path, source.audio_stream)?;
2890 if probe.map(|p| p.channels) == first.map(|p| p.channels) {2951 if probe.map(|p| p.channels) == first.map(|p| p.channels) {
2891 return Ok(());2952 return Ok(());
2892 }2953 }
@@ -3156,6 +3217,53 @@ mod tests {
3156 assert_eq!(session.sources().len(), session.counts.len());3217 assert_eq!(session.sources().len(), session.counts.len());
3157 }3218 }
3158 3219
3220+ /// The two headers a place reads are read *once*: an mp4 dropped on a lane
3221+ /// used to open its own container and the timeline's first source every
3222+ /// time, both on the thread that draws, and a film whose pages have gone
3223+ /// cold is 1.7 s of that.
3224+ ///
3225+ /// Proved by answering, not by timing: the memo is filled with a layout the
3226+ /// file does not have, and the place is refused. Only a door that read the
3227+ /// remembered header rather than the disk can refuse it.
3228+ #[test]
3229+ fn a_place_reads_each_audio_header_once() {
3230+ let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
3231+ session.set_gain(0.0);
3232+ session.import(&asset("test_av2.mp4")).expect("av2 matches");
3233+ let end = session.timeline_duration();
3234+ assert!(
3235+ session
3236+ .place_stream_at(end, &asset("test_av2.mp4"), 0, None)
3237+ .expect("a file just imported is on this timeline")
3238+ );
3239+ assert_eq!(
3240+ session.probes.len(),
3241+ 2,
3242+ "the timeline's first source and the file placed, once each"
3243+ );
3244+
3245+ // Now say av2 is 5.1. Nothing on disk changed, so a door that opens the
3246+ // file still matches; one that trusts the memo refuses by layout.
3247+ // Keyed exactly as a [`Source`] names a file -- canonical, symlinks
3248+ // resolved -- which is the key the doors look up.
3249+ let av2 = Source::new(asset("test_av2.mp4"), 0).path;
3250+ session.probes.insert(
3251+ (av2, 0),
3252+ Some(crate::AudioProbe {
3253+ sample_rate: 48_000,
3254+ channels: 6,
3255+ }),
3256+ );
3257+ let refused = session
3258+ .place_stream_at(end, &asset("test_av2.mp4"), 0, None)
3259+ .expect_err("the remembered layout disagrees with the timeline's");
3260+ assert!(
3261+ refused.to_string().contains("6 ch"),
3262+ "refused in the layout's own words: {refused}"
3263+ );
3264+ assert_eq!(session.probes.len(), 2, "and nothing was opened to say so");
3265+ }
3266+
3159 #[test]3267 #[test]
3160 fn boundary_seconds_round_trip_to_their_own_frame() {3268 fn boundary_seconds_round_trip_to_their_own_frame() {
3161 for f in 0u32..300 {3269 for f in 0u32..300 {