Skip to content

Commit

Permalink
Avoid unnecessary allocations in float_lit and integer_lit.
Browse files Browse the repository at this point in the history
This commit avoids an allocation when parsing any float and integer
literals that don't involved underscores.

This reduces the number of allocations done for the `tuple-stress`
benchmark by 10%, reducing its instruction count by just under 1%.
  • Loading branch information
nnethercote committed Oct 26, 2018
1 parent f99911a commit eb637d2
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,17 @@ fn float_lit(s: &str, suffix: Option<Symbol>, diag: Option<(Span, &Handler)>)
-> Option<ast::LitKind> {
debug!("float_lit: {:?}, {:?}", s, suffix);
// FIXME #2252: bounds checking float literals is deferred until trans
let s = s.chars().filter(|&c| c != '_').collect::<String>();
filtered_float_lit(Symbol::intern(&s), suffix, diag)

// Strip underscores without allocating a new String unless necessary.
let s2;
let s = if s.chars().any(|c| c == '_') {
s2 = s.chars().filter(|&c| c != '_').collect::<String>();
&s2
} else {
s
};

filtered_float_lit(Symbol::intern(s), suffix, diag)
}

/// Parse a string representing a byte literal into its final form. Similar to `char_lit`
Expand Down Expand Up @@ -591,8 +600,14 @@ fn integer_lit(s: &str, suffix: Option<Symbol>, diag: Option<(Span, &Handler)>)
-> Option<ast::LitKind> {
// s can only be ascii, byte indexing is fine

let s2 = s.chars().filter(|&c| c != '_').collect::<String>();
let mut s = &s2[..];
// Strip underscores without allocating a new String unless necessary.
let s2;
let mut s = if s.chars().any(|c| c == '_') {
s2 = s.chars().filter(|&c| c != '_').collect::<String>();
&s2
} else {
s
};

debug!("integer_lit: {}, {:?}", s, suffix);

Expand Down

0 comments on commit eb637d2

Please sign in to comment.