Skip to content

Commit d5d389e

Browse files
committed
Use escape_default() for strings in LitKind::token().
This avoids converting every char to \u{...} form, which bloats the resulting strings unnecessarily. It also provides consistency with the existing escape_default() calls in LitKind::token() used for raw string literals, char literals, and raw byte char literals. There are two benefits from this change. - Compilation is faster. Most of the rustc-perf benchmarks see a non-trivial speedup, particularly for incremental rebuilds, with the best speedup over 13%, and multiple others over 10%. - Generated rlibs are smaller. An extreme example is libfutures.rlib, which shrinks from 2073306 bytes to 1765927 bytes, a 15% reduction.
1 parent f76f6fb commit d5d389e

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

src/libsyntax/attr.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1228,10 +1228,7 @@ impl LitKind {
12281228

12291229
match *self {
12301230
LitKind::Str(string, ast::StrStyle::Cooked) => {
1231-
let mut escaped = String::new();
1232-
for ch in string.as_str().chars() {
1233-
escaped.extend(ch.escape_unicode());
1234-
}
1231+
let escaped = string.as_str().escape_default();
12351232
Token::Literal(token::Lit::Str_(Symbol::intern(&escaped)), None)
12361233
}
12371234
LitKind::Str(string, ast::StrStyle::Raw(n)) => {

src/libsyntax/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#![feature(non_exhaustive)]
2626
#![feature(const_atomic_usize_new)]
2727
#![feature(rustc_attrs)]
28+
#![feature(str_escape)]
2829

2930
#![recursion_limit="256"]
3031

0 commit comments

Comments
 (0)