@@ -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 ) ,
0 commit comments