Skip to content

Commit 89cf405

Browse files
committed
Reduce amount of hashing
1 parent f2732bb commit 89cf405

File tree

10 files changed

+47
-58
lines changed

10 files changed

+47
-58
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ bench = false
2323
typst-macros = { path = "macros" }
2424
bitflags = "1"
2525
bytemuck = "1"
26-
comemo = "0.2"
26+
comemo = "0.2.1"
2727
ecow = "0.1"
2828
flate2 = "1"
2929
if_chain = "1"

cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ typst = { path = ".." }
2323
typst-library = { path = "../library" }
2424
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }
2525
codespan-reporting = "0.11"
26-
comemo = "0.2"
26+
comemo = "0.2.1"
2727
dirs = "4"
2828
elsa = "1.7"
2929
memmap2 = "0.5"

docs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ typst-library = { path = "../library" }
1515
unscanny = "0.1"
1616
include_dir = "0.7"
1717
pulldown-cmark = "0.9"
18-
comemo = "0.2"
18+
comemo = "0.2.1"
1919
serde = "1"
2020
serde_yaml = "0.8"
2121
heck = "0.4"

library/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ bench = false
1616

1717
[dependencies]
1818
typst = { path = ".." }
19-
comemo = "0.2"
19+
comemo = "0.2.1"
2020
csv = "1"
2121
ecow = "0.1"
2222
hayagriva = "0.3"

src/model/content.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt::{self, Debug, Formatter, Write};
33
use std::iter::Sum;
44
use std::ops::{Add, AddAssign};
55

6-
use comemo::Tracked;
6+
use comemo::{Prehashed, Tracked};
77
use ecow::{eco_format, EcoString, EcoVec};
88

99
use super::{
@@ -29,8 +29,8 @@ pub struct Content {
2929
enum Attr {
3030
Span(Span),
3131
Field(EcoString),
32-
Value(Value),
33-
Child(Content),
32+
Value(Prehashed<Value>),
33+
Child(Prehashed<Content>),
3434
Styles(Styles),
3535
Prepared,
3636
Guard(Guard),
@@ -54,9 +54,11 @@ impl Content {
5454
let Some(first) = iter.next() else { return Self::empty() };
5555
let Some(second) = iter.next() else { return first };
5656
let mut content = Content::empty();
57-
content.attrs.push(Attr::Child(first));
58-
content.attrs.push(Attr::Child(second));
59-
content.attrs.extend(iter.map(Attr::Child));
57+
content.attrs.push(Attr::Child(Prehashed::new(first)));
58+
content.attrs.push(Attr::Child(Prehashed::new(second)));
59+
content
60+
.attrs
61+
.extend(iter.map(|child| Attr::Child(Prehashed::new(child))));
6062
content
6163
}
6264

@@ -164,10 +166,10 @@ impl Content {
164166
Attr::Field(field) => *field == name,
165167
_ => false,
166168
}) {
167-
self.attrs.make_mut()[i + 1] = Attr::Value(value.into());
169+
self.attrs.make_mut()[i + 1] = Attr::Value(Prehashed::new(value.into()));
168170
} else {
169171
self.attrs.push(Attr::Field(name));
170-
self.attrs.push(Attr::Value(value.into()));
172+
self.attrs.push(Attr::Value(Prehashed::new(value.into())));
171173
}
172174
}
173175

@@ -285,7 +287,7 @@ impl Content {
285287
self
286288
} else {
287289
let mut content = Content::new(StyledElem::func());
288-
content.attrs.push(Attr::Child(self));
290+
content.attrs.push(Attr::Child(Prehashed::new(self)));
289291
content.attrs.push(Attr::Styles(styles));
290292
content
291293
}
@@ -466,11 +468,11 @@ impl Add for Content {
466468
lhs
467469
}
468470
(true, false) => {
469-
lhs.attrs.push(Attr::Child(rhs));
471+
lhs.attrs.push(Attr::Child(Prehashed::new(rhs)));
470472
lhs
471473
}
472474
(false, true) => {
473-
rhs.attrs.insert(0, Attr::Child(lhs));
475+
rhs.attrs.insert(0, Attr::Child(Prehashed::new(lhs)));
474476
rhs
475477
}
476478
(false, false) => Self::sequence([lhs, rhs]),

src/model/styles.rs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use std::any::{Any, TypeId};
22
use std::fmt::{self, Debug, Formatter, Write};
33
use std::iter;
44
use std::mem;
5+
use std::ptr;
56
use std::sync::Arc;
67

8+
use comemo::Prehashed;
79
use ecow::{eco_format, eco_vec, EcoString, EcoVec};
810

911
use super::{Content, ElemFunc, Element, Introspector, Label, Location, Vt};
@@ -15,7 +17,7 @@ use crate::util::pretty_array_like;
1517

1618
/// A list of style properties.
1719
#[derive(Default, PartialEq, Clone, Hash)]
18-
pub struct Styles(EcoVec<Style>);
20+
pub struct Styles(EcoVec<Prehashed<Style>>);
1921

2022
impl Styles {
2123
/// Create a new, empty style list.
@@ -34,7 +36,7 @@ impl Styles {
3436
/// style map, `self` contributes the outer values and `value` is the inner
3537
/// one.
3638
pub fn set(&mut self, style: impl Into<Style>) {
37-
self.0.push(style.into());
39+
self.0.push(Prehashed::new(style.into()));
3840
}
3941

4042
/// Remove the style that was last set.
@@ -51,20 +53,22 @@ impl Styles {
5153
/// Apply one outer styles. Like [`chain_one`](StyleChain::chain_one), but
5254
/// in-place.
5355
pub fn apply_one(&mut self, outer: Style) {
54-
self.0.insert(0, outer);
56+
self.0.insert(0, Prehashed::new(outer));
5557
}
5658

5759
/// Apply a slice of outer styles.
58-
pub fn apply_slice(&mut self, outer: &[Style]) {
60+
pub fn apply_slice(&mut self, outer: &[Prehashed<Style>]) {
5961
self.0 = outer.iter().cloned().chain(mem::take(self).0.into_iter()).collect();
6062
}
6163

6264
/// Add an origin span to all contained properties.
6365
pub fn spanned(mut self, span: Span) -> Self {
6466
for entry in self.0.make_mut() {
65-
if let Style::Property(property) = entry {
66-
property.span = Some(span);
67-
}
67+
entry.update(|entry| {
68+
if let Style::Property(property) = entry {
69+
property.span = Some(span);
70+
}
71+
});
6872
}
6973
self
7074
}
@@ -73,7 +77,7 @@ impl Styles {
7377
/// styles for the given element.
7478
pub fn interruption<T: Element>(&self) -> Option<Option<Span>> {
7579
let func = T::func();
76-
self.0.iter().find_map(|entry| match entry {
80+
self.0.iter().find_map(|entry| match &**entry {
7781
Style::Property(property) => property.is_of(func).then_some(property.span),
7882
Style::Recipe(recipe) => recipe.is_of(func).then_some(Some(recipe.span)),
7983
})
@@ -82,7 +86,7 @@ impl Styles {
8286

8387
impl From<Style> for Styles {
8488
fn from(entry: Style) -> Self {
85-
Self(eco_vec![entry])
89+
Self(eco_vec![Prehashed::new(entry)])
8690
}
8791
}
8892

@@ -566,7 +570,7 @@ cast_from_value! {
566570
#[derive(Default, Clone, Copy, Hash)]
567571
pub struct StyleChain<'a> {
568572
/// The first link of this chain.
569-
head: &'a [Style],
573+
head: &'a [Prehashed<Style>],
570574
/// The remaining links in the chain.
571575
tail: Option<&'a Self>,
572576
}
@@ -590,14 +594,6 @@ impl<'a> StyleChain<'a> {
590594
}
591595
}
592596

593-
/// Make the given style the first link of the this chain.
594-
pub fn chain_one<'b>(&'b self, style: &'b Style) -> StyleChain<'b> {
595-
StyleChain {
596-
head: std::slice::from_ref(style),
597-
tail: Some(self),
598-
}
599-
}
600-
601597
/// Cast the first value for the given property in the chain.
602598
pub fn get<T: Cast>(
603599
self,
@@ -733,16 +729,6 @@ impl<'a> StyleChain<'a> {
733729
fn pop(&mut self) {
734730
*self = self.tail.copied().unwrap_or_default();
735731
}
736-
737-
/// Whether two style chains contain the same pointers.
738-
fn ptr_eq(self, other: Self) -> bool {
739-
std::ptr::eq(self.head, other.head)
740-
&& match (self.tail, other.tail) {
741-
(Some(a), Some(b)) => std::ptr::eq(a, b),
742-
(None, None) => true,
743-
_ => false,
744-
}
745-
}
746732
}
747733

748734
impl Debug for StyleChain<'_> {
@@ -756,13 +742,18 @@ impl Debug for StyleChain<'_> {
756742

757743
impl PartialEq for StyleChain<'_> {
758744
fn eq(&self, other: &Self) -> bool {
759-
self.ptr_eq(*other) || crate::util::hash128(self) == crate::util::hash128(other)
745+
ptr::eq(self.head, other.head)
746+
&& match (self.tail, other.tail) {
747+
(Some(a), Some(b)) => ptr::eq(a, b),
748+
(None, None) => true,
749+
_ => false,
750+
}
760751
}
761752
}
762753

763754
/// An iterator over the entries in a style chain.
764755
struct Entries<'a> {
765-
inner: std::slice::Iter<'a, Style>,
756+
inner: std::slice::Iter<'a, Prehashed<Style>>,
766757
links: Links<'a>,
767758
}
768759

@@ -787,7 +778,7 @@ impl<'a> Iterator for Entries<'a> {
787778
struct Links<'a>(Option<StyleChain<'a>>);
788779

789780
impl<'a> Iterator for Links<'a> {
790-
type Item = &'a [Style];
781+
type Item = &'a [Prehashed<Style>];
791782

792783
fn next(&mut self) -> Option<Self::Item> {
793784
let StyleChain { head, tail } = self.0?;

src/syntax/source.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ impl Source {
110110
pub fn edit(&mut self, replace: Range<usize>, with: &str) -> Range<usize> {
111111
let start_byte = replace.start;
112112
let start_utf16 = self.byte_to_utf16(replace.start).unwrap();
113-
let mut text = std::mem::take(&mut self.text).into_inner();
114-
text.replace_range(replace.clone(), with);
115-
self.text = Prehashed::new(text);
113+
self.text.update(|text| text.replace_range(replace.clone(), with));
116114

117115
// Remove invalidated line starts.
118116
let line = self.byte_to_line(start_byte).unwrap();
@@ -128,10 +126,8 @@ impl Source {
128126
.extend(lines_from(start_byte, start_utf16, &self.text[start_byte..]));
129127

130128
// Incrementally reparse the replaced range.
131-
let mut root = std::mem::take(&mut self.root).into_inner();
132-
let range = reparse(&mut root, &self.text, replace, with.len());
133-
self.root = Prehashed::new(root);
134-
range
129+
self.root
130+
.update(|root| reparse(root, &self.text, replace, with.len()))
135131
}
136132

137133
/// Get the length of the file in UTF-8 encoded bytes.

tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ publish = false
88
[dev-dependencies]
99
typst = { path = ".." }
1010
typst-library = { path = "../library" }
11-
comemo = "0.2"
11+
comemo = "0.2.1"
1212
elsa = "1.7"
1313
iai = { git = "https://github.com/reknih/iai" }
1414
once_cell = "1"

tests/ref/text/raw-code.png

13 Bytes
Loading

0 commit comments

Comments
 (0)