Skip to content

Commit e8eb002

Browse files
giacomocavalierilpil
authored andcommitted
make the formatter faster by not counting chars of comments
1 parent b435df8 commit e8eb002

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

format/src/lib.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl<'a, 'doc> Formatter<'a> {
271271
self.doc_comments.iter().map(|comment| {
272272
DOC_COMMENT_DOCUMENT
273273
.to_doc(arena)
274-
.append(arena, EcoString::from(comment.content))
274+
.append(arena, arena.zero_width_str(comment.content))
275275
}),
276276
LINE_DOCUMENT,
277277
);
@@ -286,7 +286,7 @@ impl<'a, 'doc> Formatter<'a> {
286286
let comments = self.module_comments.iter().map(|s| {
287287
MODULE_COMMENT_DOCUMENT
288288
.to_doc(arena)
289-
.append(arena, EcoString::from(s.content))
289+
.append(arena, arena.zero_width_str(s.content))
290290
});
291291
arena
292292
.join(comments, LINE_DOCUMENT)
@@ -845,7 +845,7 @@ impl<'a, 'doc> Formatter<'a> {
845845
.join(
846846
comments.map(|comment| match comment {
847847
Some(comment) => {
848-
DOC_COMMENT_DOCUMENT.append(arena, EcoString::from(comment))
848+
DOC_COMMENT_DOCUMENT.append(arena, arena.zero_width_str(comment))
849849
}
850850
None => unreachable!("empty lines dropped by pop_doc_comments"),
851851
}),
@@ -3660,7 +3660,9 @@ impl<'a, 'doc> Formatter<'a> {
36603660
}
36613661
(_, None) => continue,
36623662
};
3663-
doc.push(COMMENT_DOCUMENT.append(arena, EcoString::from(comment)));
3663+
doc.push(
3664+
COMMENT_DOCUMENT.append(arena, arena.zero_width_string(EcoString::from(comment))),
3665+
);
36643666
match comments.peek() {
36653667
// Next line is a comment
36663668
Some((_, Some(_))) => doc.push(LINE_DOCUMENT),
@@ -4246,12 +4248,17 @@ fn printed_comments<'a, 'doc>(
42464248
let _ = comments.peek()?;
42474249

42484250
let mut doc = Vec::new();
4249-
while let Some(c) = comments.next() {
4250-
let c = match c {
4251-
Some(c) => c,
4252-
None => continue,
4253-
};
4254-
doc.push("//".to_doc(arena).append(arena, EcoString::from(c)));
4251+
while let Some(comment) = comments.next() {
4252+
let Some(comment) = comment else { continue };
4253+
4254+
// The comment is turned into a zero width string rather than a regular
4255+
// string document: comment lines are never touched by the formatter and
4256+
// we don't need to know how long each one is.
4257+
// So we can do this to avoid counting the graphemes of each one, which
4258+
// is a lot of wasted work.
4259+
let comment = arena.zero_width_str(comment);
4260+
4261+
doc.push(COMMENT_DOCUMENT.append(arena, comment));
42554262
match comments.peek() {
42564263
// Next line is a comment
42574264
Some(Some(_)) => doc.push(LINE_DOCUMENT),

pretty-arena/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,17 @@ impl<'string, 'doc> DocumentArena<'string, 'doc> {
10871087
)
10881088
}
10891089

1090+
/// Same as the `zero_width_string` but this works with string references.
1091+
/// This is useful when you already have a string reference and you want to
1092+
/// avoid allocating a further string.
1093+
///
1094+
pub fn zero_width_str(&'doc self, string: &'string str) -> Document<'string, 'doc> {
1095+
Document(
1096+
self.documents
1097+
.alloc(PrintableDocument::ZeroWidthStr { string }),
1098+
)
1099+
}
1100+
10901101
/// Joins together an iterator of documents into a single document.
10911102
/// All the documents are gonna be rendered next to each other with no
10921103
/// spaces in between.

0 commit comments

Comments
 (0)