diff --git a/Cargo.lock b/Cargo.lock index 0099a6993..a4669ba80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1747,6 +1747,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "portable-atomic" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" + [[package]] name = "postcard" version = "1.0.8" @@ -2578,6 +2584,7 @@ dependencies = [ "once_cell", "palette", "phf", + "portable-atomic", "qcms", "rayon", "regex", diff --git a/Cargo.toml b/Cargo.toml index 5624eba8d..e80460163 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,6 +75,7 @@ pathdiff = "0.2" pdf-writer = "0.9.2" phf = { version = "0.11", features = ["macros"] } pixglyph = "0.3" +portable-atomic = "1.6" proc-macro2 = "1" pulldown-cmark = "0.9" quote = "1" diff --git a/crates/typst-cli/src/query.rs b/crates/typst-cli/src/query.rs index f2e52666d..0b14a8936 100644 --- a/crates/typst-cli/src/query.rs +++ b/crates/typst-cli/src/query.rs @@ -78,7 +78,6 @@ fn retrieve( .introspector .query(&selector.0) .into_iter() - .map(|x| x.into_inner()) .collect::>()) } diff --git a/crates/typst/Cargo.toml b/crates/typst/Cargo.toml index 0f33dbac2..8c3ef084d 100644 --- a/crates/typst/Cargo.toml +++ b/crates/typst/Cargo.toml @@ -44,6 +44,7 @@ once_cell = { workspace = true } palette = { workspace = true } qcms = { workspace = true } phf = { workspace = true } +portable-atomic = { workspace = true } rayon = { workspace = true } regex = { workspace = true } roxmltree = { workspace = true } diff --git a/crates/typst/src/eval/call.rs b/crates/typst/src/eval/call.rs index 3717903c3..26626b1e9 100644 --- a/crates/typst/src/eval/call.rs +++ b/crates/typst/src/eval/call.rs @@ -1,4 +1,4 @@ -use comemo::{Prehashed, Tracked, TrackedMut}; +use comemo::{Tracked, TrackedMut}; use ecow::{eco_format, EcoVec}; use crate::diag::{bail, error, At, HintedStrResult, SourceResult, Trace, Tracepoint}; @@ -14,6 +14,7 @@ use crate::symbols::Symbol; use crate::syntax::ast::{self, AstNode}; use crate::syntax::{Spanned, SyntaxNode}; use crate::text::TextElem; +use crate::util::LazyHash; use crate::World; impl Eval for ast::FuncCall<'_> { @@ -260,7 +261,7 @@ impl Eval for ast::Closure<'_> { #[allow(clippy::too_many_arguments)] pub(crate) fn call_closure( func: &Func, - closure: &Prehashed, + closure: &LazyHash, world: Tracked, introspector: Tracked, route: Tracked, diff --git a/crates/typst/src/foundations/bytes.rs b/crates/typst/src/foundations/bytes.rs index 3c6fa1fa8..605af0650 100644 --- a/crates/typst/src/foundations/bytes.rs +++ b/crates/typst/src/foundations/bytes.rs @@ -3,12 +3,12 @@ use std::fmt::{self, Debug, Formatter}; use std::ops::{Add, AddAssign, Deref}; use std::sync::Arc; -use comemo::Prehashed; use ecow::{eco_format, EcoString}; use serde::{Serialize, Serializer}; use crate::diag::{bail, StrResult}; use crate::foundations::{cast, func, scope, ty, Array, Reflect, Repr, Str, Value}; +use crate::util::LazyHash; /// A sequence of bytes. /// @@ -40,12 +40,12 @@ use crate::foundations::{cast, func, scope, ty, Array, Reflect, Repr, Str, Value /// ``` #[ty(scope, cast)] #[derive(Clone, Hash, Eq, PartialEq)] -pub struct Bytes(Arc>>); +pub struct Bytes(Arc>>); impl Bytes { /// Create a buffer from a static byte slice. pub fn from_static(slice: &'static [u8]) -> Self { - Self(Arc::new(Prehashed::new(Cow::Borrowed(slice)))) + Self(Arc::new(LazyHash::new(Cow::Borrowed(slice)))) } /// Return `true` if the length is 0. @@ -182,13 +182,13 @@ impl AsRef<[u8]> for Bytes { impl From<&[u8]> for Bytes { fn from(slice: &[u8]) -> Self { - Self(Arc::new(Prehashed::new(slice.to_vec().into()))) + Self(Arc::new(LazyHash::new(slice.to_vec().into()))) } } impl From> for Bytes { fn from(vec: Vec) -> Self { - Self(Arc::new(Prehashed::new(vec.into()))) + Self(Arc::new(LazyHash::new(vec.into()))) } } @@ -208,9 +208,7 @@ impl AddAssign for Bytes { } else if self.is_empty() { *self = rhs; } else if Arc::strong_count(&self.0) == 1 && matches!(**self.0, Cow::Owned(_)) { - Arc::make_mut(&mut self.0).update(|cow| { - cow.to_mut().extend_from_slice(&rhs); - }) + Arc::make_mut(&mut self.0).to_mut().extend_from_slice(&rhs); } else { *self = Self::from([self.as_slice(), rhs.as_slice()].concat()); } diff --git a/crates/typst/src/foundations/cast.rs b/crates/typst/src/foundations/cast.rs index 716eae9d4..8e75dc71c 100644 --- a/crates/typst/src/foundations/cast.rs +++ b/crates/typst/src/foundations/cast.rs @@ -3,7 +3,6 @@ use std::fmt::Write; use std::hash::Hash; use std::ops::Add; -use comemo::Prehashed; use ecow::{eco_format, EcoString}; use smallvec::SmallVec; use unicode_math_class::MathClass; @@ -98,20 +97,6 @@ impl Reflect for Packed { } } -impl Reflect for Prehashed { - fn input() -> CastInfo { - T::input() - } - - fn output() -> CastInfo { - T::output() - } - - fn castable(value: &Value) -> bool { - T::castable(value) - } -} - impl Reflect for StrResult { fn input() -> CastInfo { T::input() @@ -206,12 +191,6 @@ impl IntoValue for Spanned { } } -impl IntoValue for Prehashed { - fn into_value(self) -> Value { - self.into_inner().into_value() - } -} - /// Cast a Rust type or result into a [`SourceResult`]. /// /// Converts `T`, [`StrResult`], or [`SourceResult`] into @@ -278,12 +257,6 @@ impl FromValue for Packed { } } -impl FromValue for Prehashed { - fn from_value(value: Value) -> StrResult { - Ok(Self::new(T::from_value(value)?)) - } -} - impl FromValue> for T { fn from_value(value: Spanned) -> StrResult { T::from_value(value.v) diff --git a/crates/typst/src/foundations/content.rs b/crates/typst/src/foundations/content.rs index 86e75eb58..6d937dfba 100644 --- a/crates/typst/src/foundations/content.rs +++ b/crates/typst/src/foundations/content.rs @@ -6,7 +6,6 @@ use std::marker::PhantomData; use std::ops::{Add, AddAssign, Deref, DerefMut}; use std::sync::Arc; -use comemo::Prehashed; use ecow::{eco_format, EcoString}; use serde::{Serialize, Serializer}; use smallvec::smallvec; @@ -23,7 +22,7 @@ use crate::model::{Destination, EmphElem, StrongElem}; use crate::realize::{Behave, Behaviour}; use crate::syntax::Span; use crate::text::UnderlineElem; -use crate::util::{fat, BitSet}; +use crate::util::{fat, BitSet, LazyHash}; /// A piece of document content. /// @@ -80,7 +79,7 @@ pub struct Content { /// The inner representation behind the `Arc`. #[derive(Hash)] -struct Inner { +struct Inner { /// An optional label attached to the element. label: Option