···271271 self.doc_comments.iter().map(|comment| {
272272 DOC_COMMENT_DOCUMENT
273273 .to_doc(arena)
274274- .append(arena, EcoString::from(comment.content))
274274+ .append(arena, arena.zero_width_str(comment.content))
275275 }),
276276 LINE_DOCUMENT,
277277 );
···286286 let comments = self.module_comments.iter().map(|s| {
287287 MODULE_COMMENT_DOCUMENT
288288 .to_doc(arena)
289289- .append(arena, EcoString::from(s.content))
289289+ .append(arena, arena.zero_width_str(s.content))
290290 });
291291 arena
292292 .join(comments, LINE_DOCUMENT)
···845845 .join(
846846 comments.map(|comment| match comment {
847847 Some(comment) => {
848848- DOC_COMMENT_DOCUMENT.append(arena, EcoString::from(comment))
848848+ DOC_COMMENT_DOCUMENT.append(arena, arena.zero_width_str(comment))
849849 }
850850 None => unreachable!("empty lines dropped by pop_doc_comments"),
851851 }),
···36603660 }
36613661 (_, None) => continue,
36623662 };
36633663- doc.push(COMMENT_DOCUMENT.append(arena, EcoString::from(comment)));
36633663+ doc.push(
36643664+ COMMENT_DOCUMENT.append(arena, arena.zero_width_string(EcoString::from(comment))),
36653665+ );
36643666 match comments.peek() {
36653667 // Next line is a comment
36663668 Some((_, Some(_))) => doc.push(LINE_DOCUMENT),
···42464248 let _ = comments.peek()?;
4247424942484250 let mut doc = Vec::new();
42494249- while let Some(c) = comments.next() {
42504250- let c = match c {
42514251- Some(c) => c,
42524252- None => continue,
42534253- };
42544254- doc.push("//".to_doc(arena).append(arena, EcoString::from(c)));
42514251+ while let Some(comment) = comments.next() {
42524252+ let Some(comment) = comment else { continue };
42534253+42544254+ // The comment is turned into a zero width string rather than a regular
42554255+ // string document: comment lines are never touched by the formatter and
42564256+ // we don't need to know how long each one is.
42574257+ // So we can do this to avoid counting the graphemes of each one, which
42584258+ // is a lot of wasted work.
42594259+ let comment = arena.zero_width_str(comment);
42604260+42614261+ doc.push(COMMENT_DOCUMENT.append(arena, comment));
42554262 match comments.peek() {
42564263 // Next line is a comment
42574264 Some(Some(_)) => doc.push(LINE_DOCUMENT),
···10871087 )
10881088 }
1089108910901090+ /// Same as the `zero_width_string` but this works with string references.
10911091+ /// This is useful when you already have a string reference and you want to
10921092+ /// avoid allocating a further string.
10931093+ ///
10941094+ pub fn zero_width_str(&'doc self, string: &'string str) -> Document<'string, 'doc> {
10951095+ Document(
10961096+ self.documents
10971097+ .alloc(PrintableDocument::ZeroWidthStr { string }),
10981098+ )
10991099+ }
11001100+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.