mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 21:38:37 +00:00
The heap pass keeps a slot that shows each copy in a part's spans, not a hold on the copy
Sampled (run d201, the thread that records the list ahead of each frame): 54 % of its work is heap_buffer_pass itself, and by line 81 % of that is making the copies' spans (SweepRanges::spans), 11 % setting the slots' ranges. Each part's span held its copy (an Arc), and about every one of the 16 parts is made again each pass, so some 70,000 holds on copies other threads hold too were taken and let go a pass. A part's span now keeps a slot of the part that shows the copy (a change that could leave that slot showing another copy or none makes the part stale, as before); a pass takes one hold on each copy, which goes on into what it refreshes, and setting a slot takes none of its own. AI-assisted (Claude). Tested on an RTX 4060 Ti, driver 616.64 (Windows 11): heap_range_tests (the spans kept by part against every slot's, 600 random rounds) pass; the pass's time is measured in a standing run next. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -1493,13 +1493,25 @@ const PARTS: usize = HEAP_ROUND as usize;
|
||||
/// A copy and the span of it that ranges cover.
|
||||
type Span<T> = (std::sync::Arc<T>, u64, u64);
|
||||
|
||||
/// Of a part's spans ([`SweepRanges`]): a slot of the part that shows the copy, and the span the part's
|
||||
/// views of it cover. The copy itself is held by the slots' ranges alone.
|
||||
///
|
||||
/// Leonida: a part's span held the copy (an `Arc`) as well, so every span made again took a hold on its
|
||||
/// copy and every clearing let one go: some 70,000 counts of copies the other threads hold too were
|
||||
/// changed a pass (4,400 copies in about all of the 16 parts), and making the spans was 81 % of the time
|
||||
/// the pass spent in `heap_buffer_pass` itself, 5 ms a frame (profile `ahead-d201`, the thread that
|
||||
/// records the list ahead of each frame).
|
||||
type PartSpan = (u32, u64, u64);
|
||||
|
||||
/// [`HeapRanges`] of copies, with the spans of each part's ranges kept
|
||||
/// beside them ([`sweep_spans`]).
|
||||
struct SweepRanges<T> {
|
||||
ranges: HeapRanges<std::sync::Arc<T>>,
|
||||
/// Per part and copy (by its address): the copy and the span its views
|
||||
/// in the part cover. Right for the parts that are not `stale`.
|
||||
spans: [crate::host::ByCopy<Span<T>>; PARTS],
|
||||
/// Per part and copy (by its address): a slot of the part that shows the
|
||||
/// copy, and the span its views in the part cover. Right for the parts
|
||||
/// that are not `stale`: a change that could leave the slot showing
|
||||
/// another copy or none makes its part stale ([`SweepRanges::set`]).
|
||||
spans: [crate::host::ByCopy<PartSpan>; PARTS],
|
||||
/// How many slots of each part show something.
|
||||
views: [u32; PARTS],
|
||||
/// The parts whose spans are to be made again from their slots, a bit
|
||||
@ -1530,6 +1542,13 @@ fn join_span<T>(spans: &mut crate::host::ByCopy<Span<T>>, (copy, from, to): &Spa
|
||||
span.2 = span.2.max(*to);
|
||||
}
|
||||
|
||||
/// Widens the span of the copy at `key` in a part's `spans` by `from..to`, which `slot` shows.
|
||||
fn join_part(spans: &mut crate::host::ByCopy<PartSpan>, slot: u32, key: usize, from: u64, to: u64) {
|
||||
let span = spans.entry(key).or_insert((slot, from, to));
|
||||
span.1 = span.1.min(from);
|
||||
span.2 = span.2.max(to);
|
||||
}
|
||||
|
||||
impl<T> SweepRanges<T> {
|
||||
fn clear(&mut self) {
|
||||
self.ranges.0.clear();
|
||||
@ -1559,22 +1578,24 @@ impl<T> SweepRanges<T> {
|
||||
self.stale |= 1 << p;
|
||||
return;
|
||||
}
|
||||
let old = self.ranges.replace(slot, range.clone());
|
||||
// (The new range's copy by its address: the range itself goes into the slot, with no other hold.)
|
||||
let new = range.as_ref().map(|(copy, from, to)| (copy_key(copy), *from, *to));
|
||||
let old = self.ranges.replace(slot, range);
|
||||
self.views[p] = (self.views[p] + u32::from(now)).saturating_sub(u32::from(old.is_some()));
|
||||
match (old, range) {
|
||||
match (old, new) {
|
||||
(None, None) => {}
|
||||
// One more range of the copy's.
|
||||
(None, Some(new)) => join_span(&mut self.spans[p], &new),
|
||||
(Some(old), Some(new)) if copy_key(&old.0) == copy_key(&new.0) => {
|
||||
if (old.1, old.2) == (new.1, new.2) {
|
||||
(None, Some((key, from, to))) => join_part(&mut self.spans[p], slot, key, from, to),
|
||||
(Some(old), Some((key, from, to))) if copy_key(&old.0) == key => {
|
||||
if (old.1, old.2) == (from, to) {
|
||||
return;
|
||||
}
|
||||
// The span did not end where the old range did: without it
|
||||
// the span is as it was.
|
||||
match self.spans[p].get_mut(©_key(&old.0)) {
|
||||
match self.spans[p].get_mut(&key) {
|
||||
Some(span) if span.1 < old.1 && old.2 < span.2 => {
|
||||
span.1 = span.1.min(new.1);
|
||||
span.2 = span.2.max(new.2);
|
||||
span.1 = span.1.min(from);
|
||||
span.2 = span.2.max(to);
|
||||
}
|
||||
_ => self.stale |= 1 << p,
|
||||
}
|
||||
@ -1603,18 +1624,30 @@ impl<T> SweepRanges<T> {
|
||||
let part = &mut self.spans[p];
|
||||
part.clear();
|
||||
let mut views = 0;
|
||||
for range in self.ranges.part_ranges(p as u32, PARTS as u32) {
|
||||
for (slot, (copy, from, to)) in self.ranges.part_slots(p as u32, PARTS as u32) {
|
||||
views += 1;
|
||||
join_span(part, range);
|
||||
join_part(part, slot, copy_key(copy), *from, *to);
|
||||
}
|
||||
self.views[p] = views;
|
||||
}
|
||||
self.stale = 0;
|
||||
// (As many as the part with the most, at least.)
|
||||
all.reserve(self.spans.iter().map(|part| part.len()).max().unwrap_or(0));
|
||||
for part in &self.spans {
|
||||
for span in part.values() {
|
||||
join_span(&mut all, span);
|
||||
for (p, part) in self.spans.iter().enumerate() {
|
||||
for (&key, &(slot, from, to)) in part {
|
||||
match all.entry(key) {
|
||||
std::collections::hash_map::Entry::Occupied(mut span) => {
|
||||
let span = span.get_mut();
|
||||
span.1 = span.1.min(from);
|
||||
span.2 = span.2.max(to);
|
||||
}
|
||||
std::collections::hash_map::Entry::Vacant(place) => {
|
||||
// The one hold a pass takes on each copy.
|
||||
if let Some(copy) = self.ranges.shown(slot, key, p as u32, PARTS as u32) {
|
||||
place.insert((copy.clone(), from, to));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(all, self.views.iter().sum())
|
||||
@ -1658,11 +1691,33 @@ impl<T> HeapRanges<T> {
|
||||
self.0.iter().skip(part as usize).step_by(parts as usize).flatten()
|
||||
}
|
||||
|
||||
/// [`HeapRanges::part_ranges`] with their slots.
|
||||
fn part_slots(&self, part: u32, parts: u32) -> impl Iterator<Item = (u32, &(T, u64, u64))> {
|
||||
self.0
|
||||
.iter()
|
||||
.enumerate()
|
||||
.skip(part as usize)
|
||||
.step_by(parts as usize)
|
||||
.filter_map(|(slot, range)| range.as_ref().map(|range| (slot as u32, range)))
|
||||
}
|
||||
|
||||
fn ranges(&self) -> impl Iterator<Item = &(T, u64, u64)> {
|
||||
self.0.iter().flatten()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> HeapRanges<std::sync::Arc<T>> {
|
||||
/// The copy at `key` as slot `slot` shows it; if that slot shows another copy or none (not met: a change
|
||||
/// that could leave it so makes its part stale, [`SweepRanges::set`]), as another slot of part `part`
|
||||
/// of `parts` shows it.
|
||||
fn shown(&self, slot: u32, key: usize, part: u32, parts: u32) -> Option<&std::sync::Arc<T>> {
|
||||
match self.0.get(slot as usize) {
|
||||
Some(Some((copy, _, _))) if copy_key(copy) == key => Some(copy),
|
||||
_ => self.part_ranges(part, parts).map(|(copy, _, _)| copy).find(|copy| copy_key(copy) == key),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod heap_range_tests {
|
||||
use super::{HEAP_ROUND, HeapRanges, PARTS, SweepRanges};
|
||||
@ -2185,20 +2240,23 @@ fn heap_buffer_pass(heap: &crate::descriptor::Heap, host: &crate::descriptor::ho
|
||||
let every = *EVERY.get_or_init(|| {
|
||||
std::env::var("LEONIDA_UNWATCHED_FRAMES").ok().and_then(|v| v.parse().ok()).unwrap_or(1)
|
||||
});
|
||||
let mut found = Vec::with_capacity(spans.len());
|
||||
let copies = spans.len();
|
||||
let mut found = Vec::with_capacity(copies);
|
||||
let mut next_unwatched = std::collections::HashMap::new();
|
||||
for (shadow, from, to) in spans.values() {
|
||||
// (The spans' holds on their copies go on into `found`.)
|
||||
for (shadow, from, to) in spans.into_values() {
|
||||
if every > 1 && shadow.unwatched() {
|
||||
let id = shadow.id();
|
||||
if let Some(&(last, low, high)) = kept.unwatched.get(&id)
|
||||
&& frame >= last && frame - last < every && low <= *from && *to <= high
|
||||
&& frame >= last && frame - last < every && low <= from && to <= high
|
||||
{
|
||||
next_unwatched.insert(id, (last, low, high));
|
||||
continue;
|
||||
}
|
||||
next_unwatched.insert(id, (frame, *from, *to));
|
||||
next_unwatched.insert(id, (frame, from, to));
|
||||
}
|
||||
found.push((shadow.clone(), *from, (*to).min(*from + shadow.left(*from))));
|
||||
let to = to.min(from + shadow.left(from));
|
||||
found.push((shadow, from, to));
|
||||
}
|
||||
kept.unwatched = next_unwatched;
|
||||
drop(kept);
|
||||
@ -2217,7 +2275,7 @@ fn heap_buffer_pass(heap: &crate::descriptor::Heap, host: &crate::descriptor::ho
|
||||
tracing::info!(
|
||||
slots = slots.len(),
|
||||
views,
|
||||
copies = spans.len(),
|
||||
copies,
|
||||
mb = bytes >> 20,
|
||||
ms = started.elapsed().as_millis(),
|
||||
"buffer views of the heap made ready for shaders that pick them by index"
|
||||
|
||||
Reference in New Issue
Block a user