Move incremental test into separate function

This commit is contained in:
Laurenz 2021-07-26 00:08:08 +02:00
parent 88d3be2581
commit 56cbf96fe2
2 changed files with 68 additions and 61 deletions

View File

@ -50,6 +50,15 @@ pub struct LayoutNode {
}
impl LayoutNode {
/// Create a new instance from any node that satisifies the required bounds.
#[cfg(not(feature = "layout-cache"))]
pub fn new<T>(node: T) -> Self
where
T: Layout + Debug + Clone + Eq + PartialEq + 'static,
{
Self { node: Box::new(node) }
}
/// Create a new instance from any node that satisifies the required bounds.
#[cfg(feature = "layout-cache")]
pub fn new<T>(node: T) -> Self
@ -65,15 +74,6 @@ impl LayoutNode {
Self { node: Box::new(node), hash }
}
/// Create a new instance from any node that satisifies the required bounds.
#[cfg(not(feature = "layout-cache"))]
pub fn new<T>(node: T) -> Self
where
T: Layout + Debug + Clone + Eq + PartialEq + 'static,
{
Self { node: Box::new(node) }
}
}
impl Layout for LayoutNode {
@ -82,20 +82,17 @@ impl Layout for LayoutNode {
ctx: &mut LayoutContext,
regions: &Regions,
) -> Vec<Constrained<Rc<Frame>>> {
#[cfg(feature = "layout-cache")]
{
ctx.level += 1;
let frames = ctx.layouts.get(self.hash, regions.clone()).unwrap_or_else(|| {
let frames = self.node.layout(ctx, regions);
ctx.layouts.insert(self.hash, frames.clone(), ctx.level - 1);
frames
});
ctx.level -= 1;
frames
}
#[cfg(not(feature = "layout-cache"))]
self.node.layout(ctx, regions)
return self.node.layout(ctx, regions);
#[cfg(feature = "layout-cache")]
ctx.layouts.get(self.hash, regions.clone()).unwrap_or_else(|| {
ctx.level += 1;
let frames = self.node.layout(ctx, regions);
ctx.level -= 1;
ctx.layouts.insert(self.hash, frames.clone(), ctx.level);
frames
})
}
}

View File

@ -16,7 +16,7 @@ use typst::eval::{eval, Scope, Value};
use typst::exec::{exec, State};
use typst::geom::{self, Length, PathElement, Point, Sides, Size};
use typst::image::ImageId;
use typst::layout::{layout, Element, Frame, Geometry, Paint, Text};
use typst::layout::{layout, Element, Frame, Geometry, LayoutTree, Paint, Text};
use typst::loading::{FileId, FsLoader};
use typst::parse::{parse, LineMap, Scanner};
use typst::syntax::{Location, Pos};
@ -256,6 +256,7 @@ fn test_part(
diags.extend(tree.diags);
let mut ok = true;
for panic in panics.borrow().iter() {
let line = map.location(panic.pos).unwrap().line;
println!(" Assertion failed in line {}", lines + line);
@ -290,44 +291,7 @@ fn test_part(
}
#[cfg(feature = "layout-cache")]
{
let reference = ctx.layouts.clone();
for level in 0 .. reference.levels() {
ctx.layouts = reference.clone();
ctx.layouts.retain(|x| x == level);
if ctx.layouts.is_empty() {
continue;
}
ctx.layouts.turnaround();
let cached = layout(ctx, &tree.output);
let misses = ctx
.layouts
.entries()
.filter(|e| e.level() == level && !e.hit() && e.age() == 2)
.count();
if misses > 0 {
ok = false;
println!(
" Recompilation had {} cache misses on level {} (Subtest {}) ❌",
misses, level, i
);
}
if cached != frames {
ok = false;
println!(
" Recompilation of subtest {} differs from clean pass ❌",
i
);
}
}
ctx.layouts = reference;
ctx.layouts.turnaround();
}
(ok &= test_incremental(ctx, i, &tree.output, &frames));
if !compare_ref {
frames.clear();
@ -336,6 +300,52 @@ fn test_part(
(ok, compare_ref, frames)
}
#[cfg(feature = "layout-cache")]
fn test_incremental(
ctx: &mut Context,
i: usize,
tree: &LayoutTree,
frames: &[Rc<Frame>],
) -> bool {
let mut ok = true;
let reference = ctx.layouts.clone();
for level in 0 .. reference.levels() {
ctx.layouts = reference.clone();
ctx.layouts.retain(|x| x == level);
if ctx.layouts.is_empty() {
continue;
}
ctx.layouts.turnaround();
let cached = layout(ctx, tree);
let misses = ctx
.layouts
.entries()
.filter(|e| e.level() == level && !e.hit() && e.age() == 2)
.count();
if misses > 0 {
println!(
" Subtest {} relayout had {} cache misses on level {} ❌",
i, misses, level
);
ok = false;
}
if cached != frames {
println!(" Subtest {} relayout differs from clean pass ❌", i);
ok = false;
}
}
ctx.layouts = reference;
ctx.layouts.turnaround();
ok
}
fn parse_metadata(src: &str, map: &LineMap) -> (Option<bool>, DiagSet) {
let mut diags = DiagSet::new();
let mut compare_ref = None;