From 745ab1a7e24634095af45415cff5058c4365f5cb Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 26 Oct 2021 21:49:27 -0400 Subject: [PATCH] Avoid needless memcpy for codeblock --- src/librustdoc/html/highlight.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 66059ef65de6d..2e3b2c83ca6a3 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -98,7 +98,17 @@ fn write_code( decoration_info: Option, ) { // 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,