diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-12-08 14:56:22 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-12-08 19:02:09 +0100 |
| commit | 3b104e2ba8900b815dbcc24f943a4328519b3fbc (patch) | |
| tree | 4b862aa28118be362380a20c73761d1db02978f7 /crates/typst-cli/src | |
| parent | 2ea41007e177dd8e472942e569bd2f8775044b52 (diff) | |
Move export cache
Diffstat (limited to 'crates/typst-cli/src')
| -rw-r--r-- | crates/typst-cli/src/compile.rs | 33 | ||||
| -rw-r--r-- | crates/typst-cli/src/world.rs | 35 |
2 files changed, 34 insertions, 34 deletions
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs index 3397935b..dd15c8fc 100644 --- a/crates/typst-cli/src/compile.rs +++ b/crates/typst-cli/src/compile.rs @@ -9,6 +9,7 @@ use termcolor::{ColorChoice, StandardStream}; use typst::diag::{bail, At, Severity, SourceDiagnostic, StrResult}; use typst::eval::Tracer; use typst::foundations::Datetime; +use typst::layout::Frame; use typst::model::Document; use typst::syntax::{FileId, Source, Span}; use typst::visualize::Color; @@ -250,6 +251,38 @@ fn export_image( Ok(()) } +/// Caches exported files so that we can avoid re-exporting them if they haven't +/// changed. +/// +/// This is done by having a list of size `files.len()` that contains the hashes +/// of the last rendered frame in each file. If a new frame is inserted, this +/// will invalidate the rest of the cache, this is deliberate as to decrease the +/// complexity and memory usage of such a cache. +pub struct ExportCache { + /// The hashes of last compilation's frames. + pub cache: Vec<u128>, +} + +impl ExportCache { + /// Creates a new export cache. + pub fn new() -> Self { + Self { cache: Vec::with_capacity(32) } + } + + /// Returns true if the entry is cached and appends the new hash to the + /// cache (for the next compilation). + pub fn is_cached(&mut self, i: usize, frame: &Frame) -> bool { + let hash = typst::util::hash128(frame); + + if i >= self.cache.len() { + self.cache.push(hash); + return false; + } + + std::mem::replace(&mut self.cache[i], hash) == hash + } +} + /// Opens the given file using: /// - The default file viewer if `open` is `None`. /// - The given viewer provided by `open` if it is `Some`. diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs index 99456679..8c60d93c 100644 --- a/crates/typst-cli/src/world.rs +++ b/crates/typst-cli/src/world.rs @@ -8,13 +8,12 @@ use comemo::Prehashed; use ecow::eco_format; use typst::diag::{FileError, FileResult, StrResult}; use typst::foundations::{Bytes, Datetime}; -use typst::layout::Frame; use typst::syntax::{FileId, Source, VirtualPath}; use typst::text::{Font, FontBook}; -use typst::util::hash128; use typst::{Library, World}; use crate::args::SharedArgs; +use crate::compile::ExportCache; use crate::fonts::{FontSearcher, FontSlot}; use crate::package::prepare_package; @@ -319,38 +318,6 @@ impl<T: Clone> SlotCell<T> { } } -/// Caches exported files so that we can avoid re-exporting them if they haven't -/// changed. -/// -/// This is done by having a list of size `files.len()` that contains the hashes -/// of the last rendered frame in each file. If a new frame is inserted, this -/// will invalidate the rest of the cache, this is deliberate as to decrease the -/// complexity and memory usage of such a cache. -pub struct ExportCache { - /// The hashes of last compilation's frames. - pub cache: Vec<u128>, -} - -impl ExportCache { - /// Creates a new export cache. - pub fn new() -> Self { - Self { cache: Vec::with_capacity(32) } - } - - /// Returns true if the entry is cached and appends the new hash to the - /// cache (for the next compilation). - pub fn is_cached(&mut self, i: usize, frame: &Frame) -> bool { - let hash = hash128(frame); - - if i >= self.cache.len() { - self.cache.push(hash); - return false; - } - - std::mem::replace(&mut self.cache[i], hash) == hash - } -} - /// Read a file. fn read(path: &Path) -> FileResult<Vec<u8>> { let f = |e| FileError::from_io(e, path); |
