Skip to content

Commit 910617d

Browse files
committed
Auto merge of #147443 - krtab:dosreplaceonlywhenneeded, r=GuillaumeGomez
[rustdoc] a small performance improvement: only allocate new string if there are DOS backlines in highlight.rs r? `@GuillaumeGomez`
2 parents 82224f6 + e98856b commit 910617d

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,13 @@ pub(super) fn write_code(
408408
line_info: Option<LineInfo>,
409409
) {
410410
// This replace allows to fix how the code source with DOS backline characters is displayed.
411-
let src = src.replace("\r\n", "\n");
411+
let src =
412+
// The first "\r\n" should be fairly close to the beginning of the string relatively
413+
// to its overall length, and most strings handled by rustdoc likely don't have
414+
// DOS backlines anyway.
415+
// Checking for the single ASCII character '\r' is much more efficient than checking for
416+
// the whole string "\r\n".
417+
if src.contains('\r') { src.replace("\r\n", "\n").into() } else { Cow::Borrowed(src) };
412418
let mut token_handler = TokenHandler {
413419
out,
414420
closing_tags: Vec::new(),

0 commit comments

Comments
 (0)