Skip to content

Commit 745ab1a

Browse files
Avoid needless memcpy for codeblock
1 parent c7a30c8 commit 745ab1a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,17 @@ fn write_code(
9898
decoration_info: Option<DecorationInfo>,
9999
) {
100100
// This replace allows to fix how the code source with DOS backline characters is displayed.
101-
let src = src.replace("\r\n", "\n");
101+
let replaced;
102+
// We don't typically expect to find carriage returns in the src text here,
103+
// and at minimum replace(...) needs to allocate a new String and copy over,
104+
// which can add up. This does mean we traverse src twice looking for
105+
// carriage returns, but that's generally pretty fast.
106+
let src = if src.contains("\r") {
107+
replaced = src.replace("\r\n", "\n");
108+
replaced.as_str()
109+
} else {
110+
src
111+
};
102112
Classifier::new(
103113
&src,
104114
edition,

0 commit comments

Comments
 (0)