diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-05-11 11:27:00 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-05-11 11:27:00 +0200 |
| commit | 2f0b5eeae09bd880e4552bb83e44d9cd32571c58 (patch) | |
| tree | 6989d423e6bb614b3224be53140e4d649033aeac /src/model/mod.rs | |
| parent | 47dff3765de863554ca296448555599fc50d4a8a (diff) | |
More efficient introspection
Switches from a mutable locator to one based on tracked chains and optimizes query performance.
Diffstat (limited to 'src/model/mod.rs')
| -rw-r--r-- | src/model/mod.rs | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/src/model/mod.rs b/src/model/mod.rs index 92c4293b..e541cd01 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -6,13 +6,15 @@ mod introspect; mod realize; mod styles; +pub use typst_macros::element; + pub use self::content::*; pub use self::element::*; pub use self::introspect::*; pub use self::realize::*; pub use self::styles::*; -pub use typst_macros::element; +use std::mem::ManuallyDrop; use comemo::{Track, Tracked, TrackedMut, Validate}; @@ -29,38 +31,51 @@ pub fn typeset( mut tracer: TrackedMut<Tracer>, content: &Content, ) -> SourceResult<Document> { - tracing::info!("Starting layout"); + tracing::info!("Starting typesetting"); + let library = world.library(); let styles = StyleChain::new(&library.styles); let mut document; let mut iter = 0; - let mut introspector = Introspector::new(&[]); + + // We need `ManuallyDrop` until this lands in stable: + // https://github.com/rust-lang/rust/issues/70919 + let mut introspector = ManuallyDrop::new(Introspector::new(&[])); // Relayout until all introspections stabilize. // If that doesn't happen within five attempts, we give up. loop { tracing::info!("Layout iteration {iter}"); - let mut provider = StabilityProvider::new(); let constraint = <Introspector as Validate>::Constraint::new(); + let mut locator = Locator::new(); let mut vt = Vt { world, tracer: TrackedMut::reborrow_mut(&mut tracer), - provider: provider.track_mut(), + locator: &mut locator, introspector: introspector.track_with(&constraint), }; - document = (library.items.layout)(&mut vt, content, styles)?; - iter += 1; + // Layout! + let result = (library.items.layout)(&mut vt, content, styles)?; - introspector = Introspector::new(&document.pages); + // Drop the old introspector. + ManuallyDrop::into_inner(introspector); + + // Only now assign the document and construct the new introspector. + document = result; + introspector = ManuallyDrop::new(Introspector::new(&document.pages)); + iter += 1; if iter >= 5 || introspector.validate(&constraint) { break; } } + // Drop the introspector. + ManuallyDrop::into_inner(introspector); + Ok(document) } @@ -73,19 +88,7 @@ pub struct Vt<'a> { /// The tracer for inspection of the values an expression produces. pub tracer: TrackedMut<'a, Tracer>, /// Provides stable identities to elements. - pub provider: TrackedMut<'a, StabilityProvider>, + pub locator: &'a mut Locator<'a>, /// Provides access to information about the document. pub introspector: Tracked<'a, Introspector>, } - -impl Vt<'_> { - /// Mutably reborrow with a shorter lifetime. - pub fn reborrow_mut(&mut self) -> Vt<'_> { - Vt { - world: self.world, - tracer: TrackedMut::reborrow_mut(&mut self.tracer), - provider: TrackedMut::reborrow_mut(&mut self.provider), - introspector: self.introspector, - } - } -} |
