diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index c01e08b..aaa8e95 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -3164,6 +3164,14 @@ fn stale_under_flights(runs: &[&HostList]) -> Vec<(std::sync::Arc, usize, usize)>, +) -> Vec<(std::sync::Arc, usize, usize)> { if late_exact() { // The title's bytes are put in beside the flights' writes; a // settle is still due where that could not be done. At most this @@ -5717,7 +5725,7 @@ impl Host { let mut lists: Vec> = Vec::with_capacity(2 * runs.len()); // Where each run's lists start in `lists`. let mut firsts = Vec::with_capacity(runs.len()); - let mut batch = Batch::default(); + let mut batch = Batch::reused(); let split = Split::begin(); // The time a settle of flights takes inside this preparing: the // wait for the card and the copy-back have their own places on the @@ -5726,23 +5734,13 @@ impl Host { // One write-watch asking for everything the submission refreshes // (`VICTORY_ASK_ONCE`): per copy, the span of its refreshed ranges. if crate::watch::once() { - let mut spans: ByCopy<(usize, usize)> = ByCopy::default(); - for list in runs { - for step in &list.deferred().copies { - if let Step::Refresh(shadow, address, len) = step - && shadow.asked_ahead() - { - let span = spans - .entry(std::sync::Arc::as_ptr(shadow) as usize) - .or_insert((*address, *address + *len)); - *span = (span.0.min(*address), span.1.max(*address + *len)); - } - } - } - let ranges: Vec<(usize, usize)> = spans.into_values().collect(); + batch.copy_spans(runs); + let Batch { spans, ranges, .. } = &mut batch; + ranges.clear(); + ranges.extend(spans.iter().filter(|s| s.0.asked_ahead()).map(|s| (s.1, s.2))); SPLIT[8].fetch_add(split.so_far(), Ordering::Relaxed); stage.next("submit: asking the write watch"); - crate::watch::ask_once(&ranges); + crate::watch::ask_once(&batch.ranges); split.part(0); stage.next("submit: title bytes under flights' writes"); // Behind flights: the title wrote memory that a flight's shaders @@ -5755,10 +5753,10 @@ impl Host { upcheck::reads(runs); } } - let stale = if behind { stale_under_flights(runs) } else { Vec::new() }; + let stale = if behind { batch.stale_under_flights() } else { Vec::new() }; if !stale.is_empty() { let settling = std::time::Instant::now(); - self.settle_stale(&mut submitted, runs, stale, &ranges); + self.settle_stale(&mut submitted, runs, stale, &batch.ranges); settled += settling.elapsed(); } } else if behind && !stale_under_flights(runs).is_empty() { @@ -5896,6 +5894,7 @@ impl Host { texture_back_ranges, writes, }; + batch.give_back(); working(was_working); return Some((submitted, sent)); } @@ -6045,6 +6044,7 @@ impl Host { if relay() { crate::shadow::relay_fetched(*QUEUE_ORDER.lock().unwrap_or_else(|e| e.into_inner())); } + batch.give_back(); working(was_working); None } @@ -7949,7 +7949,7 @@ struct Batch { index: ByCopy, /// Staging buffers (by address) refreshed since the last title write, /// or due to be. - textures: std::collections::HashSet, + textures: std::collections::HashSet>, /// Those due ([`Batch::refresh_textures`]). due: Vec>, /// What copies at their place in the submission's lists put into title @@ -7963,6 +7963,23 @@ struct Batch { /// `ahead` holds everything the submission refreshes /// ([`Batch::refresh_front`]). front: bool, + /// Per copy the submission refreshes, the span of all its refreshes, in + /// the order the copies first come ([`Batch::copy_spans`]); where each + /// copy (by address) is in it. + spans: Vec<(std::sync::Arc, usize, usize)>, + spans_at: ByCopy, + /// Room for the submission's one asking's ranges, and for + /// [`Batch::refresh_front`]'s walk over the steps. + ranges: Vec<(usize, usize)>, + touches: Vec, +} + +thread_local! { + /// The last submission's [`Batch`] on this thread, emptied, with the + /// room its tables and lists grew to: some 28 submissions a frame + /// each made them anew, grown from nothing (hash tables rehashed as + /// they grew, about 2% of the submitting thread in profile-y-prof60). + static SPARE_BATCH: std::cell::Cell> = const { std::cell::Cell::new(None) }; } /// Whether the copies that a submission's shaders write are brought up to @@ -8184,6 +8201,72 @@ fn wide_textures() -> bool { } impl Batch { + /// This thread's last one, emptied ([`SPARE_BATCH`]), or a new one. + fn reused() -> Batch { + SPARE_BATCH.take().unwrap_or_default() + } + + /// Emptied (the room kept) for the thread's next submission. + fn give_back(mut self) { + self.refreshes.clear(); + self.index.clear(); + self.textures.clear(); + self.due.clear(); + self.placed.clear(); + self.fills.clear(); + self.ahead.clear(); + self.front = false; + self.spans.clear(); + self.spans_at.clear(); + self.ranges.clear(); + self.touches.clear(); + SPARE_BATCH.set(Some(self)); + } + + /// Per copy that `runs` refresh, the span of all its refreshes, into + /// [`Batch::spans`]: the submission's one asking asks about those + /// ([`crate::watch::ask_once`]), and behind flights those of copies + /// shaders may be writing are looked at ([`Batch::stale_under_flights`]). + /// One walk over the steps for both, where there were two, each with a + /// table of its own. + fn copy_spans(&mut self, runs: &[&HostList]) { + self.spans.clear(); + self.spans_at.clear(); + // (Most come several times on end.) + let mut last = (0, 0); + for list in runs { + for step in &list.deferred().copies { + let Step::Refresh(shadow, address, len) = step else { + continue; + }; + let (from, to) = (*address, *address + *len); + let key = std::sync::Arc::as_ptr(shadow) as usize; + let at = if last.0 == key && key != 0 { + last.1 + } else { + match self.spans_at.entry(key) { + std::collections::hash_map::Entry::Occupied(at) => *at.get(), + std::collections::hash_map::Entry::Vacant(place) => { + place.insert(self.spans.len()); + self.spans.push((shadow.clone(), from, to)); + self.spans.len() - 1 + } + } + }; + last = (key, at); + let span = &mut self.spans[at]; + (span.1, span.2) = (span.1.min(from), span.2.max(to)); + } + } + } + + /// [`stale_under_flights`] of the submission's refreshes, from + /// [`Batch::copy_spans`]: the copies shaders may be writing now, in the + /// order they first come, with the span of all their refreshes. + fn stale_under_flights(&self) -> Vec<(std::sync::Arc, usize, usize)> { + stale_of(self.spans.iter().filter(|s| s.0.writes_pending()).cloned().collect()) + } + fn add(&mut self, shadow: &std::sync::Arc, address: usize, len: usize) { let end = address + len; let key = std::sync::Arc::as_ptr(shadow) as usize; @@ -8257,25 +8340,23 @@ impl Batch { /// Before the steps of `runs` ([`refresh_front`]), a new part of the /// submission beginning at each of `parts`. fn refresh_front(&mut self, runs: &[&HostList], parts: &[usize]) { - let mut copies: ByCopy> = ByCopy::default(); + // The copies refreshed, by address: as the submission's one asking + // found them, or found here. + if self.spans.is_empty() { + self.copy_spans(runs); + } let mut due = Vec::new(); - let mut touches = Vec::new(); + self.touches.clear(); for (n, list) in runs.iter().enumerate() { if parts.contains(&n) { - touches.push(Touch::Part); + self.touches.push(Touch::Part); } - let mut last = 0; for step in &list.deferred().copies { match step { Step::Refresh(shadow, at, len) => { - let key = std::sync::Arc::as_ptr(shadow) as usize; - if key != last { - copies.entry(key).or_insert_with(|| shadow.clone()); - last = key; - } - touches.push(Touch::Refresh(key, *at, *at + *len)); + self.touches.push(Touch::Refresh(std::sync::Arc::as_ptr(shadow) as usize, *at, *at + *len)); } - Step::GpuWrite(shadow, ..) => touches.push(Touch::Write(std::sync::Arc::as_ptr(shadow) as usize)), + Step::GpuWrite(shadow, ..) => self.touches.push(Touch::Write(std::sync::Arc::as_ptr(shadow) as usize)), Step::Texture(staging) if wide_textures() => { if self.texture_due(staging) { due.push(staging.clone()); @@ -8285,9 +8366,10 @@ impl Batch { } } } - let spans: Vec<_> = front_spans(touches.into_iter()) + let found = front_spans(self.touches.drain(..)); + let spans: Vec<_> = found .into_iter() - .filter_map(|(key, from, to)| Some((copies.remove(&key)?, from, to))) + .filter_map(|(key, from, to)| Some((self.spans.get(*self.spans_at.get(&key)?)?.0.clone(), from, to))) .collect(); self.front = true; for (shadow, from, to) in &spans {