Skip to content

Commit 56c225c

Browse files
committed
Use ahash/phf in parser
1 parent 688880c commit 56c225c

File tree

6 files changed

+158
-67
lines changed

6 files changed

+158
-67
lines changed

Cargo.lock

Lines changed: 108 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ rustpython-ast = { path = "../ast" }
1515
num-complex = { version = "0.3", features = ["serde"] }
1616
num-traits = "0.2"
1717
log = "0.4"
18-
arrayvec = "0.5"
1918
ahash = "0.6"
2019

2120
[dev-dependencies]

parser/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ num-traits = "0.2"
2020
unic-emoji-char = "0.9"
2121
unic-ucd-ident = "0.9"
2222
unicode_names2 = "0.4"
23+
phf = { version = "0.8", features = ["macros"] }
24+
hashbrown = "0.9"
25+
ahash = "0.6"

parser/src/function.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use ahash::RandomState;
12
use std::collections::HashSet;
23

34
use crate::ast;
@@ -45,7 +46,7 @@ pub fn parse_args(func_args: Vec<FunctionArgument>) -> Result<ast::ArgumentList,
4546
let mut args = vec![];
4647
let mut keywords = vec![];
4748

48-
let mut keyword_names = HashSet::with_capacity(func_args.len());
49+
let mut keyword_names = HashSet::with_capacity_and_hasher(func_args.len(), RandomState::new());
4950
for (name, value) in func_args {
5051
match name {
5152
Some(n) => {

parser/src/lexer.rs

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use num_traits::identities::Zero;
1010
use num_traits::Num;
1111
use std::char;
1212
use std::cmp::Ordering;
13-
use std::collections::HashMap;
1413
use std::str::FromStr;
1514
use unic_emoji_char::is_emoji_presentation;
1615
use unic_ucd_ident::{is_xid_continue, is_xid_start};
@@ -66,52 +65,48 @@ pub struct Lexer<T: Iterator<Item = char>> {
6665
chr1: Option<char>,
6766
chr2: Option<char>,
6867
location: Location,
69-
keywords: HashMap<String, Tok>,
7068
}
7169

72-
pub fn get_keywords() -> HashMap<String, Tok> {
73-
let mut keywords: HashMap<String, Tok> = HashMap::new();
74-
70+
pub static KEYWORDS: phf::Map<&'static str, Tok> = phf::phf_map! {
7571
// Alphabetical keywords:
76-
keywords.insert(String::from("..."), Tok::Ellipsis);
77-
keywords.insert(String::from("False"), Tok::False);
78-
keywords.insert(String::from("None"), Tok::None);
79-
keywords.insert(String::from("True"), Tok::True);
80-
81-
keywords.insert(String::from("and"), Tok::And);
82-
keywords.insert(String::from("as"), Tok::As);
83-
keywords.insert(String::from("assert"), Tok::Assert);
84-
keywords.insert(String::from("async"), Tok::Async);
85-
keywords.insert(String::from("await"), Tok::Await);
86-
keywords.insert(String::from("break"), Tok::Break);
87-
keywords.insert(String::from("class"), Tok::Class);
88-
keywords.insert(String::from("continue"), Tok::Continue);
89-
keywords.insert(String::from("def"), Tok::Def);
90-
keywords.insert(String::from("del"), Tok::Del);
91-
keywords.insert(String::from("elif"), Tok::Elif);
92-
keywords.insert(String::from("else"), Tok::Else);
93-
keywords.insert(String::from("except"), Tok::Except);
94-
keywords.insert(String::from("finally"), Tok::Finally);
95-
keywords.insert(String::from("for"), Tok::For);
96-
keywords.insert(String::from("from"), Tok::From);
97-
keywords.insert(String::from("global"), Tok::Global);
98-
keywords.insert(String::from("if"), Tok::If);
99-
keywords.insert(String::from("import"), Tok::Import);
100-
keywords.insert(String::from("in"), Tok::In);
101-
keywords.insert(String::from("is"), Tok::Is);
102-
keywords.insert(String::from("lambda"), Tok::Lambda);
103-
keywords.insert(String::from("nonlocal"), Tok::Nonlocal);
104-
keywords.insert(String::from("not"), Tok::Not);
105-
keywords.insert(String::from("or"), Tok::Or);
106-
keywords.insert(String::from("pass"), Tok::Pass);
107-
keywords.insert(String::from("raise"), Tok::Raise);
108-
keywords.insert(String::from("return"), Tok::Return);
109-
keywords.insert(String::from("try"), Tok::Try);
110-
keywords.insert(String::from("while"), Tok::While);
111-
keywords.insert(String::from("with"), Tok::With);
112-
keywords.insert(String::from("yield"), Tok::Yield);
113-
keywords
114-
}
72+
"..." => Tok::Ellipsis,
73+
"False" => Tok::False,
74+
"None" => Tok::None,
75+
"True" => Tok::True,
76+
77+
"and" => Tok::And,
78+
"as" => Tok::As,
79+
"assert" => Tok::Assert,
80+
"async" => Tok::Async,
81+
"await" => Tok::Await,
82+
"break" => Tok::Break,
83+
"class" => Tok::Class,
84+
"continue" => Tok::Continue,
85+
"def" => Tok::Def,
86+
"del" => Tok::Del,
87+
"elif" => Tok::Elif,
88+
"else" => Tok::Else,
89+
"except" => Tok::Except,
90+
"finally" => Tok::Finally,
91+
"for" => Tok::For,
92+
"from" => Tok::From,
93+
"global" => Tok::Global,
94+
"if" => Tok::If,
95+
"import" => Tok::Import,
96+
"in" => Tok::In,
97+
"is" => Tok::Is,
98+
"lambda" => Tok::Lambda,
99+
"nonlocal" => Tok::Nonlocal,
100+
"not" => Tok::Not,
101+
"or" => Tok::Or,
102+
"pass" => Tok::Pass,
103+
"raise" => Tok::Raise,
104+
"return" => Tok::Return,
105+
"try" => Tok::Try,
106+
"while" => Tok::While,
107+
"with" => Tok::With,
108+
"yield" => Tok::Yield,
109+
};
115110

116111
pub type Spanned = (Location, Tok, Location);
117112
pub type LexResult = Result<Spanned, LexicalError>;
@@ -193,7 +188,6 @@ where
193188
location: Location::new(0, 0),
194189
chr1: None,
195190
chr2: None,
196-
keywords: get_keywords(),
197191
};
198192
lxr.next_char();
199193
lxr.next_char();
@@ -245,8 +239,8 @@ where
245239
}
246240
let end_pos = self.get_pos();
247241

248-
if self.keywords.contains_key(&name) {
249-
Ok((start_pos, self.keywords[&name].clone(), end_pos))
242+
if let Some(tok) = KEYWORDS.get(name.as_str()) {
243+
Ok((start_pos, tok.clone(), end_pos))
250244
} else {
251245
Ok((start_pos, Tok::Name { name }, end_pos))
252246
}

vm/src/stdlib/keyword.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@ mod decl {
66
use rustpython_parser::lexer;
77

88
use crate::builtins::pystr::PyStrRef;
9-
use crate::pyobject::{BorrowValue, PyObjectRef, PyResult};
9+
use crate::pyobject::{BorrowValue, PyObjectRef};
1010
use crate::vm::VirtualMachine;
1111

1212
#[pyfunction]
13-
fn iskeyword(s: PyStrRef, vm: &VirtualMachine) -> PyResult {
14-
let keywords = lexer::get_keywords();
15-
let value = keywords.contains_key(s.borrow_value());
16-
let value = vm.ctx.new_bool(value);
17-
Ok(value)
13+
fn iskeyword(s: PyStrRef) -> bool {
14+
lexer::KEYWORDS.contains_key(s.borrow_value())
1815
}
1916

2017
#[pyattr]
2118
fn kwlist(vm: &VirtualMachine) -> PyObjectRef {
2219
vm.ctx.new_list(
23-
lexer::get_keywords()
20+
lexer::KEYWORDS
2421
.keys()
2522
.map(|k| vm.ctx.new_str(k.to_owned()))
2623
.collect(),

0 commit comments

Comments
 (0)