mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-12 07:35:16 +00:00
Implement some minor performance optimizations
This commit is contained in:
parent
4e5626dfd5
commit
28a8c3a062
3 changed files with 17 additions and 3 deletions
|
@ -32,6 +32,7 @@ unic-emoji-char = "0.9.0"
|
|||
unic-ucd-ident = "0.9.0"
|
||||
unicode_names2 = "0.5.0"
|
||||
thiserror = "1.0"
|
||||
fnv = "1.0.7"
|
||||
|
||||
[dev-dependencies]
|
||||
insta = "1.14.0"
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use crate::ast;
|
||||
use crate::error::{LexicalError, LexicalErrorType};
|
||||
use ahash::RandomState;
|
||||
use std::collections::HashSet;
|
||||
use fnv::FnvHashSet;
|
||||
|
||||
pub struct ArgumentList {
|
||||
pub args: Vec<ast::Expr>,
|
||||
|
@ -54,7 +53,8 @@ pub fn parse_args(func_args: Vec<FunctionArgument>) -> Result<ArgumentList, Lexi
|
|||
let mut args = vec![];
|
||||
let mut keywords = vec![];
|
||||
|
||||
let mut keyword_names = HashSet::with_capacity_and_hasher(func_args.len(), RandomState::new());
|
||||
let mut keyword_names =
|
||||
FnvHashSet::with_capacity_and_hasher(func_args.len(), Default::default());
|
||||
for (name, value) in func_args {
|
||||
match name {
|
||||
Some((start, end, name)) => {
|
||||
|
|
|
@ -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 values.len() == 1 && matches!(&values[0].1 .1, 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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue