diff --git a/Cargo.lock b/Cargo.lock index 683e9e0049..715154d091 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1720,6 +1720,12 @@ dependencies = [ "syn-ext", ] +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "rustc_version" version = "0.4.0" @@ -1885,6 +1891,7 @@ dependencies = [ "num-traits", "phf", "phf_codegen", + "rustc-hash", "rustpython-ast", "rustpython-compiler-core", "thiserror", diff --git a/compiler/parser/Cargo.toml b/compiler/parser/Cargo.toml index b27bb500e3..e3ce5a39b4 100644 --- a/compiler/parser/Cargo.toml +++ b/compiler/parser/Cargo.toml @@ -28,10 +28,11 @@ log = "0.4.16" num-bigint = "0.4.3" num-traits = "0.2.14" phf = "0.10.1" +rustc-hash = "1.1.0" +thiserror = "1.0" unic-emoji-char = "0.9.0" unic-ucd-ident = "0.9.0" unicode_names2 = "0.5.0" -thiserror = "1.0" [dev-dependencies] insta = "1.14.0" diff --git a/compiler/parser/src/function.rs b/compiler/parser/src/function.rs index 28cb44fe24..bc4d5142ea 100644 --- a/compiler/parser/src/function.rs +++ b/compiler/parser/src/function.rs @@ -1,7 +1,6 @@ use crate::ast; use crate::error::{LexicalError, LexicalErrorType}; -use ahash::RandomState; -use std::collections::HashSet; +use rustc_hash::FxHashSet; pub struct ArgumentList { pub args: Vec, @@ -54,7 +53,8 @@ pub fn parse_args(func_args: Vec) -> Result { diff --git a/compiler/parser/src/string.rs b/compiler/parser/src/string.rs index 078ac1e27e..838da94c7e 100644 --- a/compiler/parser/src/string.rs +++ b/compiler/parser/src/string.rs @@ -14,6 +14,19 @@ pub fn parse_strings( let initial_end = values[0].2; let initial_kind = (values[0].1 .1 == StringKind::U).then(|| "u".to_owned()); + // Optimization: fast-track the common case of a single string. + if matches!(&*values, [(_, (_, StringKind::Normal | StringKind::U), _)]) { + let value = values.into_iter().last().unwrap().1 .0; + return Ok(Expr::new( + initial_start, + initial_end, + ExprKind::Constant { + value: Constant::Str(value), + kind: initial_kind, + }, + )); + } + // Determine whether the list of values contains any f-strings. (If not, we can return a // single Constant at the end, rather than a JoinedStr.) let mut has_fstring = false;