summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-07-26 00:08:08 +0200
committerLaurenz <laurmaedje@gmail.com>2021-07-26 00:08:08 +0200
commit56cbf96fe2b3b82d34b2e69a49bcb7b0c0267f6a (patch)
treed3507c4cb6bb9cc8f269293758036e7172a5ef21
parent88d3be258114d631f6e1381e756ff9bb8aba6b0e (diff)
Move incremental test into separate function
-rw-r--r--src/layout/tree.rs35
-rw-r--r--tests/typeset.rs80
2 files changed, 61 insertions, 54 deletions
diff --git a/src/layout/tree.rs b/src/layout/tree.rs
index 258f1ccc..77b01ad2 100644
--- a/src/layout/tree.rs
+++ b/src/layout/tree.rs
@@ -51,6 +51,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
where
@@ -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(not(feature = "layout-cache"))]
+ 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 = 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
- });
+ let frames = self.node.layout(ctx, regions);
ctx.level -= 1;
+ ctx.layouts.insert(self.hash, frames.clone(), ctx.level);
frames
- }
-
- #[cfg(not(feature = "layout-cache"))]
- self.node.layout(ctx, regions)
+ })
}
}
diff --git a/tests/typeset.rs b/tests/typeset.rs
index 6b2258bf..59eb008f 100644
--- a/tests/typeset.rs
+++ b/tests/typeset.rs
@@ -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,50 +291,59 @@ 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;
- }
+ (ok &= test_incremental(ctx, i, &tree.output, &frames));
- ctx.layouts.turnaround();
+ if !compare_ref {
+ frames.clear();
+ }
- let cached = layout(ctx, &tree.output);
- let misses = ctx
- .layouts
- .entries()
- .filter(|e| e.level() == level && !e.hit() && e.age() == 2)
- .count();
+ (ok, compare_ref, frames)
+}
- if misses > 0 {
- ok = false;
- println!(
- " Recompilation had {} cache misses on level {} (Subtest {}) ❌",
- misses, level, i
- );
- }
+#[cfg(feature = "layout-cache")]
+fn test_incremental(
+ ctx: &mut Context,
+ i: usize,
+ tree: &LayoutTree,
+ frames: &[Rc<Frame>],
+) -> bool {
+ let mut ok = true;
- if cached != frames {
- ok = false;
- println!(
- " Recompilation of subtest {} differs from clean pass ❌",
- i
- );
- }
+ 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 = reference;
ctx.layouts.turnaround();
- }
- if !compare_ref {
- frames.clear();
+ 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;
+ }
}
- (ok, compare_ref, frames)
+ ctx.layouts = reference;
+ ctx.layouts.turnaround();
+
+ ok
}
fn parse_metadata(src: &str, map: &LineMap) -> (Option<bool>, DiagSet) {