diff options
| author | Martin Haug <mhaug@live.de> | 2021-05-26 23:36:03 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-05-26 23:50:40 +0200 |
| commit | 8e700606bb64c4ffda87cec333f7c76eae244911 (patch) | |
| tree | 8d0c40b04bc61cc3e3a9cad2f7b5f2eb4a691892 /src/layout | |
| parent | e27f6c10146240a6c8b92930b27948083f08c9b5 (diff) | |
Add a cache for unchanged layouts
Co-Authored-By: Laurenz <laurmaedje@gmail.com>
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/mod.rs | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 207d5bed..51b9bc64 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -23,12 +23,13 @@ use std::hash::{Hash, Hasher}; use decorum::NotNan; use fxhash::FxHasher64; +use crate::cache::{Cache, FramesEntry}; use crate::env::Env; use crate::geom::*; /// Layout a tree into a collection of frames. -pub fn layout(env: &mut Env, tree: &Tree) -> Vec<Frame> { - tree.layout(&mut LayoutContext { env }) +pub fn layout(env: &mut Env, cache: &mut Cache, tree: &Tree) -> Vec<Frame> { + tree.layout(&mut LayoutContext { env, cache }) } /// A tree of layout nodes. @@ -96,7 +97,19 @@ impl AnyNode { impl Layout for AnyNode { fn layout(&self, ctx: &mut LayoutContext, regions: &Regions) -> Vec<Frame> { - self.node.layout(ctx, regions) + if let Some(hit) = ctx.cache.frames.get(&self.hash()) { + if &hit.regions == regions { + return hit.frames.clone(); + } + } + + let frames = self.node.layout(ctx, regions); + ctx.cache.frames.insert(self.hash(), FramesEntry { + regions: regions.clone(), + frames: frames.clone(), + }); + + frames } } @@ -164,6 +177,9 @@ pub trait Layout { pub struct LayoutContext<'a> { /// The environment from which fonts are gathered. pub env: &'a mut Env, + /// A cache which enables reuse of layout artifacts from past compilation + /// cycles. + pub cache: &'a mut Cache, } /// A sequence of regions to layout into. |
