From da03c9df33177d77029c52f8a68a5d214a6e83c7 Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Fri, 1 May 2015 17:08:39 +0200 Subject: [PATCH] syntax: Avoid reallocating or copying in CodeMap::new_filemap Avoid creating a new String when there is no BOM to strip, and otherwises use .drain(..3) to strip the BOM using the same allocation. --- src/libsyntax/codemap.rs | 12 ++++-------- src/libsyntax/lib.rs | 1 + 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 5e0cb647c8b41..decc6f01eefff 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -543,7 +543,7 @@ impl CodeMap { } } - pub fn new_filemap(&self, filename: FileName, src: String) -> Rc { + pub fn new_filemap(&self, filename: FileName, mut src: String) -> Rc { let mut files = self.files.borrow_mut(); let start_pos = match files.last() { None => 0, @@ -551,13 +551,9 @@ impl CodeMap { }; // Remove utf-8 BOM if any. - // FIXME #12884: no efficient/safe way to remove from the start of a string - // and reuse the allocation. - let mut src = if src.starts_with("\u{feff}") { - String::from(&src[3..]) - } else { - String::from(&src[..]) - }; + if src.starts_with("\u{feff}") { + src.drain(..3); + } // Append '\n' in case it's not already there. // This is a workaround to prevent CodeMap.lookup_filemap_idx from diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 275400009f5aa..330fe86deeb37 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -27,6 +27,7 @@ #![feature(associated_consts)] #![feature(collections)] +#![feature(collections_drain)] #![feature(core)] #![feature(libc)] #![feature(rustc_private)]