Skip to content

Commit 826f673

Browse files
committed
Lazily construct panic messages in char_lit().
This reduces the time taken to run `rustc -Zparse-only rustc-benchmarks/issue-32278-big-array-of-strings` from 0.18s to 0.15s on my machine, and reduces the number of instructions (as measured by Cachegrind) from 1.34B to 1.01B. With the change applied, the time to fully compile that benchmark is 1.96s, so this is a 1.5% improvement.
1 parent 4c274b6 commit 826f673

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/libsyntax/parse/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,6 @@ pub fn char_lit(lit: &str) -> (char, isize) {
302302
_ => panic!("lexer accepted invalid char escape `{}`", lit)
303303
};
304304

305-
let msg = format!("lexer should have rejected a bad character escape {}", lit);
306-
let msg2 = &msg[..];
307-
308305
fn esc(len: usize, lit: &str) -> Option<(char, isize)> {
309306
u32::from_str_radix(&lit[2..len], 16).ok()
310307
.and_then(char::from_u32)
@@ -313,7 +310,10 @@ pub fn char_lit(lit: &str) -> (char, isize) {
313310

314311
let unicode_escape = || -> Option<(char, isize)> {
315312
if lit.as_bytes()[2] == b'{' {
316-
let idx = lit.find('}').expect(msg2);
313+
let idx = lit.find('}').unwrap_or_else(|| {
314+
panic!("lexer should have rejected a bad character escape {}", lit)
315+
});
316+
317317
let subslice = &lit[3..idx];
318318
u32::from_str_radix(subslice, 16).ok()
319319
.and_then(char::from_u32)
@@ -329,7 +329,9 @@ pub fn char_lit(lit: &str) -> (char, isize) {
329329
'u' => unicode_escape(),
330330
'U' => esc(10, lit),
331331
_ => None,
332-
}.expect(msg2);
332+
}.unwrap_or_else(|| {
333+
panic!("lexer should have rejected a bad character escape {}", lit)
334+
})
333335
}
334336

335337
/// Parse a string representing a string literal into its final form. Does

0 commit comments

Comments
 (0)