Skip to content

Commit 3de87f9

Browse files
committed
Implement some minor performance optimizations
1 parent b9c62ed commit 3de87f9

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/parser/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ unic-emoji-char = "0.9.0"
3232
unic-ucd-ident = "0.9.0"
3333
unicode_names2 = "0.5.0"
3434
thiserror = "1.0"
35+
fnv = "1.0.7"
3536

3637
[dev-dependencies]
3738
insta = "1.14.0"

compiler/parser/src/function.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::ast;
22
use crate::error::{LexicalError, LexicalErrorType};
3-
use ahash::RandomState;
4-
use std::collections::HashSet;
3+
use fnv::FnvHashSet;
54

65
pub struct ArgumentList {
76
pub args: Vec<ast::Expr>,
@@ -54,7 +53,8 @@ pub fn parse_args(func_args: Vec<FunctionArgument>) -> Result<ArgumentList, Lexi
5453
let mut args = vec![];
5554
let mut keywords = vec![];
5655

57-
let mut keyword_names = HashSet::with_capacity_and_hasher(func_args.len(), RandomState::new());
56+
let mut keyword_names =
57+
FnvHashSet::with_capacity_and_hasher(func_args.len(), Default::default());
5858
for (name, value) in func_args {
5959
match name {
6060
Some((start, end, name)) => {

compiler/parser/src/string.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ pub fn parse_strings(
1414
let initial_end = values[0].2;
1515
let initial_kind = (values[0].1 .1 == StringKind::U).then(|| "u".to_owned());
1616

17+
// Optimization: fast-track the common case of a single string.
18+
if values.len() == 1 && matches!(&values[0].1 .1, StringKind::Normal | StringKind::U) {
19+
let value = values.into_iter().last().unwrap().1 .0;
20+
return Ok(Expr::new(
21+
initial_start,
22+
initial_end,
23+
ExprKind::Constant {
24+
value: Constant::Str(value),
25+
kind: initial_kind,
26+
},
27+
));
28+
}
29+
1730
// Determine whether the list of values contains any f-strings. (If not, we can return a
1831
// single Constant at the end, rather than a JoinedStr.)
1932
let mut has_fstring = false;

0 commit comments

Comments
 (0)