More useful incremental checks

Co-Authored-By: Martin <mhaug@live.de>
This commit is contained in:
Laurenz 2021-09-27 11:40:28 +02:00
parent 1982a0639e
commit ff37a2893d
3 changed files with 35 additions and 32 deletions

View File

@ -65,26 +65,15 @@ impl LayoutCache {
hash: u64, hash: u64,
regions: &Regions, regions: &Regions,
) -> Option<Vec<Constrained<Rc<Frame>>>> { ) -> Option<Vec<Constrained<Rc<Frame>>>> {
let entries = self.frames.get_mut(&hash)?; self.frames
for entry in entries { .get_mut(&hash)?
if let Some(frames) = entry.check(regions) { .iter_mut()
return Some(frames); .find_map(|entry| entry.lookup(regions))
}
}
None
} }
/// Insert a new frame entry into the cache. /// Insert a new frame entry into the cache.
pub fn insert( pub fn insert(&mut self, hash: u64, entry: FramesEntry) {
&mut self, self.frames.entry(hash).or_default().push(entry);
hash: u64,
frames: Vec<Constrained<Rc<Frame>>>,
level: usize,
) {
self.frames
.entry(hash)
.or_default()
.push(FramesEntry::new(frames, level));
} }
/// Clear the cache. /// Clear the cache.
@ -229,17 +218,21 @@ impl FramesEntry {
/// Checks if the cached frames are valid in the given regions and returns /// Checks if the cached frames are valid in the given regions and returns
/// them if so. /// them if so.
pub fn check(&mut self, regions: &Regions) -> Option<Vec<Constrained<Rc<Frame>>>> { pub fn lookup(&mut self, regions: &Regions) -> Option<Vec<Constrained<Rc<Frame>>>> {
let mut iter = regions.iter(); self.check(regions).then(|| {
for frame in &self.frames { self.temperature[0] += 1;
let (current, base) = iter.next()?; self.frames.clone()
if !frame.constraints.check(current, base, regions.expand) { })
return None;
}
} }
self.temperature[0] += 1; /// Checks if the cached frames are valid in the given regions.
Some(self.frames.clone()) pub fn check(&self, regions: &Regions) -> bool {
let mut iter = regions.iter();
self.frames.iter().all(|frame| {
iter.next().map_or(false, |(current, base)| {
frame.constraints.check(current, base, regions.expand)
})
})
} }
/// How nested the frame was in the context is was originally appearing in. /// How nested the frame was in the context is was originally appearing in.
@ -420,7 +413,7 @@ mod tests {
let mut cache = LayoutCache::new(EvictionPolicy::None, 20); let mut cache = LayoutCache::new(EvictionPolicy::None, 20);
let regions = zero_regions(); let regions = zero_regions();
cache.policy = EvictionPolicy::None; cache.policy = EvictionPolicy::None;
cache.insert(0, empty_frames(), 0); cache.insert(0, FramesEntry::new(empty_frames(), 0));
let entry = cache.frames.get(&0).unwrap().first().unwrap(); let entry = cache.frames.get(&0).unwrap().first().unwrap();
assert_eq!(entry.age(), 1); assert_eq!(entry.age(), 1);
@ -454,7 +447,7 @@ mod tests {
fn test_incremental_properties() { fn test_incremental_properties() {
let mut cache = LayoutCache::new(EvictionPolicy::None, 20); let mut cache = LayoutCache::new(EvictionPolicy::None, 20);
cache.policy = EvictionPolicy::None; cache.policy = EvictionPolicy::None;
cache.insert(0, empty_frames(), 1); cache.insert(0, FramesEntry::new(empty_frames(), 1));
let props = cache.frames.get(&0).unwrap().first().unwrap().properties(); let props = cache.frames.get(&0).unwrap().first().unwrap().properties();
assert_eq!(props.top_level, false); assert_eq!(props.top_level, false);

View File

@ -97,7 +97,14 @@ impl Layout for LayoutNode {
ctx.level += 1; ctx.level += 1;
let frames = self.node.layout(ctx, regions); let frames = self.node.layout(ctx, regions);
ctx.level -= 1; ctx.level -= 1;
ctx.layouts.insert(self.hash, frames.clone(), ctx.level);
let entry = FramesEntry::new(frames.clone(), ctx.level);
debug_assert!(
entry.check(regions),
"constraints did not match regions they were created for",
);
ctx.layouts.insert(self.hash, entry);
frames frames
}) })
} }

View File

@ -304,8 +304,11 @@ fn test_incremental(
if misses > 0 { if misses > 0 {
println!( println!(
" Subtest {} relayout had {} cache misses on level {} ❌", " Subtest {} relayout had {} cache misses on level {} of {} ❌",
i, misses, level i,
misses,
level,
reference.levels() - 1,
); );
ok = false; ok = false;
} }