Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

make the formatter faster by not counting chars of comments

author
Giacomo Cavalieri
committer
Louis Pilfold
date (Jul 15, 2026, 5:34 PM +0100) commit e8eb002a parent b435df86 change-id qnuxytxo
+28 -10
+17 -10
format/src/lib.rs
··· 271 271 self.doc_comments.iter().map(|comment| { 272 272 DOC_COMMENT_DOCUMENT 273 273 .to_doc(arena) 274 - .append(arena, EcoString::from(comment.content)) 274 + .append(arena, arena.zero_width_str(comment.content)) 275 275 }), 276 276 LINE_DOCUMENT, 277 277 ); ··· 286 286 let comments = self.module_comments.iter().map(|s| { 287 287 MODULE_COMMENT_DOCUMENT 288 288 .to_doc(arena) 289 - .append(arena, EcoString::from(s.content)) 289 + .append(arena, arena.zero_width_str(s.content)) 290 290 }); 291 291 arena 292 292 .join(comments, LINE_DOCUMENT) ··· 845 845 .join( 846 846 comments.map(|comment| match comment { 847 847 Some(comment) => { 848 - DOC_COMMENT_DOCUMENT.append(arena, EcoString::from(comment)) 848 + DOC_COMMENT_DOCUMENT.append(arena, arena.zero_width_str(comment)) 849 849 } 850 850 None => unreachable!("empty lines dropped by pop_doc_comments"), 851 851 }), ··· 3660 3660 } 3661 3661 (_, None) => continue, 3662 3662 }; 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 + ); 3664 3666 match comments.peek() { 3665 3667 // Next line is a comment 3666 3668 Some((_, Some(_))) => doc.push(LINE_DOCUMENT), ··· 4246 4248 let _ = comments.peek()?; 4247 4249 4248 4250 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)); 4255 4262 match comments.peek() { 4256 4263 // Next line is a comment 4257 4264 Some(Some(_)) => doc.push(LINE_DOCUMENT),
+11
pretty-arena/src/lib.rs
··· 1087 1087 ) 1088 1088 } 1089 1089 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 + 1090 1101 /// Joins together an iterator of documents into a single document. 1091 1102 /// All the documents are gonna be rendered next to each other with no 1092 1103 /// spaces in between.