Move export cache

This commit is contained in:
Laurenz 2023-12-08 14:56:22 +01:00
parent 2ea41007e1
commit 3b104e2ba8
2 changed files with 34 additions and 34 deletions

View File

@ -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`.

View File

@ -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);