Skip to content

Commit 42b304a

Browse files
authored
Merge pull request RustPython#4283 from charliermarsh/charlie/perf
Implement some minor performance optimizations
2 parents b9c62ed + 3a6729e commit 42b304a

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

Cargo.lock

Lines changed: 7 additions & 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ log = "0.4.16"
2828
num-bigint = "0.4.3"
2929
num-traits = "0.2.14"
3030
phf = "0.10.1"
31+
rustc-hash = "1.1.0"
32+
thiserror = "1.0"
3133
unic-emoji-char = "0.9.0"
3234
unic-ucd-ident = "0.9.0"
3335
unicode_names2 = "0.5.0"
34-
thiserror = "1.0"
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 rustc_hash::FxHashSet;
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+
FxHashSet::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 matches!(&*values, [(_, (_, 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)