From 3de87f9828beb6e0b7bf2c4e6a9f025ab33825b7 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 19 Nov 2022 09:39:22 -0500 Subject: [PATCH 1/3] Implement some minor performance optimizations --- Cargo.lock | 1 + compiler/parser/Cargo.toml | 1 + compiler/parser/src/function.rs | 6 +++--- compiler/parser/src/string.rs | 13 +++++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 683e9e0049..43be517153 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1876,6 +1876,7 @@ version = "0.1.2" dependencies = [ "ahash", "anyhow", + "fnv", "insta", "itertools", "lalrpop", diff --git a/compiler/parser/Cargo.toml b/compiler/parser/Cargo.toml index b27bb500e3..26241c40d0 100644 --- a/compiler/parser/Cargo.toml +++ b/compiler/parser/Cargo.toml @@ -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" diff --git a/compiler/parser/src/function.rs b/compiler/parser/src/function.rs index 28cb44fe24..b2b24a88a6 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 fnv::FnvHashSet; 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..e76821d5a6 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 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; From ebeb0904d48c5f1471e63e9f3240098f0c1bd66d Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 20 Nov 2022 15:30:19 -0500 Subject: [PATCH 2/3] Use rustc-hash --- Cargo.lock | 8 +++++++- compiler/parser/Cargo.toml | 4 ++-- compiler/parser/src/function.rs | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43be517153..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" @@ -1876,7 +1882,6 @@ version = "0.1.2" dependencies = [ "ahash", "anyhow", - "fnv", "insta", "itertools", "lalrpop", @@ -1886,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 26241c40d0..e3ce5a39b4 100644 --- a/compiler/parser/Cargo.toml +++ b/compiler/parser/Cargo.toml @@ -28,11 +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" -fnv = "1.0.7" [dev-dependencies] insta = "1.14.0" diff --git a/compiler/parser/src/function.rs b/compiler/parser/src/function.rs index b2b24a88a6..bc4d5142ea 100644 --- a/compiler/parser/src/function.rs +++ b/compiler/parser/src/function.rs @@ -1,6 +1,6 @@ use crate::ast; use crate::error::{LexicalError, LexicalErrorType}; -use fnv::FnvHashSet; +use rustc_hash::FxHashSet; pub struct ArgumentList { pub args: Vec, @@ -54,7 +54,7 @@ pub fn parse_args(func_args: Vec) -> Result { From 3a6729eedda387d9e8f1f3100594e005339b135d Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 20 Nov 2022 23:41:52 -0500 Subject: [PATCH 3/3] Use match --- compiler/parser/src/string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/parser/src/string.rs b/compiler/parser/src/string.rs index e76821d5a6..838da94c7e 100644 --- a/compiler/parser/src/string.rs +++ b/compiler/parser/src/string.rs @@ -15,7 +15,7 @@ pub fn parse_strings( 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) { + if matches!(&*values, [(_, (_, StringKind::Normal | StringKind::U), _)]) { let value = values.into_iter().last().unwrap().1 .0; return Ok(Expr::new( initial_start,