Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid needless memcpy for codeblock #90332

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@ fn write_code(
decoration_info: Option<DecorationInfo>,
) {
// This replace allows to fix how the code source with DOS backline characters is displayed.
let src = src.replace("\r\n", "\n");
let replaced;
// We don't typically expect to find carriage returns in the src text here,
// and at minimum replace(...) needs to allocate a new String and copy over,
// which can add up. This does mean we traverse src twice looking for
// carriage returns, but that's generally pretty fast.
let src = if src.contains("\r") {
replaced = src.replace("\r\n", "\n");
replaced.as_str()
} else {
src
};
Classifier::new(
&src,
edition,
Expand Down