From 2d32ac73b63c81ea2754916feb7758c042f4ed3d Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 13 May 2024 19:54:34 +0200 Subject: [PATCH] Replace all `Prehashed` with `LazyHash` (#4127) --- Cargo.lock | 2 +- crates/typst-cli/src/world.rs | 14 +++++++------- crates/typst-ide/src/lib.rs | 14 +++++++------- crates/typst-syntax/Cargo.toml | 2 +- crates/typst-syntax/src/source.rs | 16 +++++++--------- crates/typst/src/lib.rs | 11 ++++++----- crates/typst/src/visualize/pattern.rs | 7 +++---- docs/src/html.rs | 6 +++--- docs/src/lib.rs | 10 +++++----- tests/fuzz/src/compile.rs | 14 +++++++------- tests/src/world.rs | 14 +++++++------- 11 files changed, 54 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8ea562e3f..d7edaf311 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2759,10 +2759,10 @@ dependencies = [ name = "typst-syntax" version = "0.11.0" dependencies = [ - "comemo", "ecow", "once_cell", "serde", + "typst-utils", "unicode-ident", "unicode-math-class", "unicode-script", diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs index 57aecd429..81fece2a2 100644 --- a/crates/typst-cli/src/world.rs +++ b/crates/typst-cli/src/world.rs @@ -5,7 +5,6 @@ use std::sync::OnceLock; use std::{fmt, fs, io, mem}; use chrono::{DateTime, Datelike, FixedOffset, Local, Utc}; -use comemo::Prehashed; use ecow::{eco_format, EcoString}; use once_cell::sync::Lazy; use parking_lot::Mutex; @@ -13,6 +12,7 @@ use typst::diag::{FileError, FileResult}; use typst::foundations::{Bytes, Datetime, Dict, IntoValue}; use typst::syntax::{FileId, Source, VirtualPath}; use typst::text::{Font, FontBook}; +use typst::utils::LazyHash; use typst::{Library, World}; use typst_timing::{timed, TimingScope}; @@ -34,9 +34,9 @@ pub struct SystemWorld { /// The input path. main: FileId, /// Typst's standard library. - library: Prehashed, + library: LazyHash, /// Metadata about discovered fonts. - book: Prehashed, + book: LazyHash, /// Locations of and storage for lazily loaded fonts. fonts: Vec, /// Maps file ids to source files and buffers. @@ -114,8 +114,8 @@ impl SystemWorld { workdir: std::env::current_dir().ok(), root, main, - library: Prehashed::new(library), - book: Prehashed::new(searcher.book), + library: LazyHash::new(library), + book: LazyHash::new(searcher.book), fonts: searcher.fonts, slots: Mutex::new(HashMap::new()), now, @@ -170,11 +170,11 @@ impl SystemWorld { } impl World for SystemWorld { - fn library(&self) -> &Prehashed { + fn library(&self) -> &LazyHash { &self.library } - fn book(&self) -> &Prehashed { + fn book(&self) -> &LazyHash { &self.book } diff --git a/crates/typst-ide/src/lib.rs b/crates/typst-ide/src/lib.rs index 3967aaad4..117e75ab6 100644 --- a/crates/typst-ide/src/lib.rs +++ b/crates/typst-ide/src/lib.rs @@ -93,12 +93,12 @@ fn summarize_font_family<'a>(variants: impl Iterator) -> Ec #[cfg(test)] mod tests { - use comemo::Prehashed; use once_cell::sync::Lazy; use typst::diag::{FileError, FileResult}; use typst::foundations::{Bytes, Datetime}; use typst::syntax::{FileId, Source}; use typst::text::{Font, FontBook}; + use typst::utils::LazyHash; use typst::{Library, World}; /// A world for IDE testing. @@ -120,11 +120,11 @@ mod tests { } impl World for TestWorld { - fn library(&self) -> &Prehashed { + fn library(&self) -> &LazyHash { &self.base.library } - fn book(&self) -> &Prehashed { + fn book(&self) -> &LazyHash { &self.base.book } @@ -155,8 +155,8 @@ mod tests { /// Shared foundation of all test worlds. struct TestBase { - library: Prehashed, - book: Prehashed, + library: LazyHash, + book: LazyHash, fonts: Vec, } @@ -168,8 +168,8 @@ mod tests { .collect(); Self { - library: Prehashed::new(Library::default()), - book: Prehashed::new(FontBook::from_fonts(&fonts)), + library: LazyHash::new(Library::default()), + book: LazyHash::new(FontBook::from_fonts(&fonts)), fonts, } } diff --git a/crates/typst-syntax/Cargo.toml b/crates/typst-syntax/Cargo.toml index 001d405c4..816f0d340 100644 --- a/crates/typst-syntax/Cargo.toml +++ b/crates/typst-syntax/Cargo.toml @@ -13,7 +13,7 @@ keywords = { workspace = true } readme = { workspace = true } [dependencies] -comemo = { workspace = true } +typst-utils = { workspace = true } ecow = { workspace = true } once_cell = { workspace = true } serde = { workspace = true } diff --git a/crates/typst-syntax/src/source.rs b/crates/typst-syntax/src/source.rs index a68a53da3..a2ccb5bbc 100644 --- a/crates/typst-syntax/src/source.rs +++ b/crates/typst-syntax/src/source.rs @@ -6,7 +6,7 @@ use std::iter::zip; use std::ops::Range; use std::sync::Arc; -use comemo::Prehashed; +use typst_utils::LazyHash; use crate::reparser::reparse; use crate::{is_newline, parse, FileId, LinkedNode, Span, SyntaxNode, VirtualPath}; @@ -24,8 +24,8 @@ pub struct Source(Arc); #[derive(Clone)] struct Repr { id: FileId, - text: Prehashed, - root: Prehashed, + text: LazyHash, + root: LazyHash, lines: Vec, } @@ -37,8 +37,8 @@ impl Source { Self(Arc::new(Repr { id, lines: lines(&text), - text: Prehashed::new(text), - root: Prehashed::new(root), + text: LazyHash::new(text), + root: LazyHash::new(root), })) } @@ -117,7 +117,7 @@ impl Source { let inner = Arc::make_mut(&mut self.0); // Update the text itself. - inner.text.update(|text| text.replace_range(replace.clone(), with)); + inner.text.replace_range(replace.clone(), with); // Remove invalidated line starts. inner.lines.truncate(line + 1); @@ -135,9 +135,7 @@ impl Source { )); // Incrementally reparse the replaced range. - inner - .root - .update(|root| reparse(root, &inner.text, replace, with.len())) + reparse(&mut inner.root, &inner.text, replace, with.len()) } /// Get the length of the file in UTF-8 encoded bytes. diff --git a/crates/typst/src/lib.rs b/crates/typst/src/lib.rs index bb869bfd2..aad474a1b 100644 --- a/crates/typst/src/lib.rs +++ b/crates/typst/src/lib.rs @@ -59,7 +59,7 @@ pub use typst_utils as utils; use std::collections::HashSet; use std::ops::{Deref, Range}; -use comemo::{Prehashed, Track, Tracked, Validate}; +use comemo::{Track, Tracked, Validate}; use ecow::{EcoString, EcoVec}; use typst_timing::{timed, TimingScope}; @@ -75,6 +75,7 @@ use crate::model::Document; use crate::syntax::package::PackageSpec; use crate::syntax::{FileId, Source, Span}; use crate::text::{Font, FontBook}; +use crate::utils::LazyHash; use crate::visualize::Color; /// Compile a source file into a fully layouted document. @@ -194,10 +195,10 @@ pub trait World { /// The standard library. /// /// Can be created through `Library::build()`. - fn library(&self) -> &Prehashed; + fn library(&self) -> &LazyHash; /// Metadata about all known fonts. - fn book(&self) -> &Prehashed; + fn book(&self) -> &LazyHash; /// Access the main source file. fn main(&self) -> Source; @@ -234,11 +235,11 @@ pub trait World { macro_rules! delegate_for_ptr { ($W:ident for $ptr:ty) => { impl<$W: World> World for $ptr { - fn library(&self) -> &Prehashed { + fn library(&self) -> &LazyHash { self.deref().library() } - fn book(&self) -> &Prehashed { + fn book(&self) -> &LazyHash { self.deref().book() } diff --git a/crates/typst/src/visualize/pattern.rs b/crates/typst/src/visualize/pattern.rs index ed4d25ac1..0f12b5d59 100644 --- a/crates/typst/src/visualize/pattern.rs +++ b/crates/typst/src/visualize/pattern.rs @@ -1,7 +1,6 @@ use std::hash::Hash; use std::sync::Arc; -use comemo::Prehashed; use ecow::{eco_format, EcoString}; use crate::diag::{bail, SourceResult}; @@ -9,7 +8,7 @@ use crate::engine::Engine; use crate::foundations::{func, repr, scope, ty, Content, Smart, StyleChain}; use crate::layout::{Abs, Axes, Frame, LayoutMultiple, Length, Regions, Size}; use crate::syntax::{Span, Spanned}; -use crate::utils::Numeric; +use crate::utils::{LazyHash, Numeric}; use crate::visualize::RelativeTo; use crate::World; @@ -102,7 +101,7 @@ pub struct Pattern(Arc); #[derive(Debug, Clone, Eq, PartialEq, Hash)] struct Repr { /// The pattern's rendered content. - frame: Prehashed, + frame: LazyHash, /// The pattern's tile size. size: Size, /// The pattern's tile spacing. @@ -209,7 +208,7 @@ impl Pattern { Ok(Self(Arc::new(Repr { size: frame.size(), - frame: Prehashed::new(frame), + frame: LazyHash::new(frame), spacing: spacing.v.map(|l| l.abs), relative, }))) diff --git a/docs/src/html.rs b/docs/src/html.rs index e88f0d3da..b5c83895f 100644 --- a/docs/src/html.rs +++ b/docs/src/html.rs @@ -1,7 +1,6 @@ use std::fmt::{self, Debug, Formatter}; use std::ops::Range; -use comemo::Prehashed; use ecow::EcoString; use heck::{ToKebabCase, ToTitleCase}; use pulldown_cmark as md; @@ -13,6 +12,7 @@ use typst::foundations::{Bytes, Datetime}; use typst::layout::{Abs, Point, Size}; use typst::syntax::{FileId, Source, VirtualPath}; use typst::text::{Font, FontBook}; +use typst::utils::LazyHash; use typst::{Library, World}; use unscanny::Scanner; use yaml_front_matter::YamlFrontMatter; @@ -457,11 +457,11 @@ fn nest_heading(level: &mut md::HeadingLevel, nesting: usize) { struct DocWorld(Source); impl World for DocWorld { - fn library(&self) -> &Prehashed { + fn library(&self) -> &LazyHash { &LIBRARY } - fn book(&self) -> &Prehashed { + fn book(&self) -> &LazyHash { &FONTS.0 } diff --git a/docs/src/lib.rs b/docs/src/lib.rs index 765e7c7ce..66242cbbe 100644 --- a/docs/src/lib.rs +++ b/docs/src/lib.rs @@ -9,7 +9,6 @@ pub use self::contribs::*; pub use self::html::*; pub use self::model::*; -use comemo::Prehashed; use ecow::{eco_format, EcoString}; use once_cell::sync::Lazy; use serde::Deserialize; @@ -28,6 +27,7 @@ use typst::model::Document; use typst::model::MODEL; use typst::symbols::SYMBOLS; use typst::text::{Font, FontBook, TEXT}; +use typst::utils::LazyHash; use typst::visualize::VISUALIZE; use typst::Library; @@ -54,7 +54,7 @@ static GROUPS: Lazy> = Lazy::new(|| { groups }); -static LIBRARY: Lazy> = Lazy::new(|| { +static LIBRARY: Lazy> = Lazy::new(|| { let mut lib = Library::default(); lib.styles .set(PageElem::set_width(Smart::Custom(Abs::pt(240.0).into()))); @@ -62,16 +62,16 @@ static LIBRARY: Lazy> = Lazy::new(|| { lib.styles.set(PageElem::set_margin(Margin::splat(Some(Smart::Custom( Abs::pt(15.0).into(), ))))); - Prehashed::new(lib) + LazyHash::new(lib) }); -static FONTS: Lazy<(Prehashed, Vec)> = Lazy::new(|| { +static FONTS: Lazy<(LazyHash, Vec)> = Lazy::new(|| { let fonts: Vec<_> = typst_assets::fonts() .chain(typst_dev_assets::fonts()) .flat_map(|data| Font::iter(Bytes::from_static(data))) .collect(); let book = FontBook::from_fonts(&fonts); - (Prehashed::new(book), fonts) + (LazyHash::new(book), fonts) }); /// Build documentation pages. diff --git a/tests/fuzz/src/compile.rs b/tests/fuzz/src/compile.rs index 2d445bf53..4dbea4100 100644 --- a/tests/fuzz/src/compile.rs +++ b/tests/fuzz/src/compile.rs @@ -1,18 +1,18 @@ #![no_main] -use comemo::Prehashed; use libfuzzer_sys::fuzz_target; use typst::diag::{FileError, FileResult}; use typst::eval::Tracer; use typst::foundations::{Bytes, Datetime}; use typst::syntax::{FileId, Source}; use typst::text::{Font, FontBook}; +use typst::utils::LazyHash; use typst::visualize::Color; use typst::{Library, World}; struct FuzzWorld { - library: Prehashed, - book: Prehashed, + library: LazyHash, + book: LazyHash, font: Font, source: Source, } @@ -23,8 +23,8 @@ impl FuzzWorld { let font = Font::new(Bytes::from_static(data), 0).unwrap(); let book = FontBook::from_fonts([&font]); Self { - library: Prehashed::new(Library::default()), - book: Prehashed::new(book), + library: LazyHash::new(Library::default()), + book: LazyHash::new(book), font, source: Source::detached(text), } @@ -32,11 +32,11 @@ impl FuzzWorld { } impl World for FuzzWorld { - fn library(&self) -> &Prehashed { + fn library(&self) -> &LazyHash { &self.library } - fn book(&self) -> &Prehashed { + fn book(&self) -> &LazyHash { &self.book } diff --git a/tests/src/world.rs b/tests/src/world.rs index 86ee8da6a..ad925a538 100644 --- a/tests/src/world.rs +++ b/tests/src/world.rs @@ -5,7 +5,6 @@ use std::io::Write; use std::path::{Path, PathBuf}; use std::sync::OnceLock; -use comemo::Prehashed; use once_cell::sync::Lazy; use parking_lot::Mutex; use typst::diag::{bail, FileError, FileResult, StrResult}; @@ -13,6 +12,7 @@ use typst::foundations::{func, Bytes, Datetime, NoneValue, Repr, Smart, Value}; use typst::layout::{Abs, Margin, PageElem}; use typst::syntax::{FileId, Source}; use typst::text::{Font, FontBook, TextElem, TextSize}; +use typst::utils::LazyHash; use typst::visualize::Color; use typst::{Library, World}; @@ -35,11 +35,11 @@ impl TestWorld { } impl World for TestWorld { - fn library(&self) -> &Prehashed { + fn library(&self) -> &LazyHash { &self.base.library } - fn book(&self) -> &Prehashed { + fn book(&self) -> &LazyHash { &self.base.book } @@ -81,8 +81,8 @@ impl TestWorld { /// Shared foundation of all test worlds. struct TestBase { - library: Prehashed, - book: Prehashed, + library: LazyHash, + book: LazyHash, fonts: Vec, slots: Mutex>, } @@ -95,8 +95,8 @@ impl Default for TestBase { .collect(); Self { - library: Prehashed::new(library()), - book: Prehashed::new(FontBook::from_fonts(&fonts)), + library: LazyHash::new(library()), + book: LazyHash::new(FontBook::from_fonts(&fonts)), fonts, slots: Mutex::new(HashMap::new()), }