Skip to content

Commit

Permalink
Use interning for function names
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Oct 15, 2024
1 parent 445a2c5 commit c338fbf
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 120 deletions.
56 changes: 27 additions & 29 deletions parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ pub struct ExceptHandler {
#[derive(Debug, Clone)]
pub struct FunctionDef {
pub node: Node,
pub name: String,
pub name: StrId,
pub args: Arguments,
pub body: Vec<Statement>,
pub decorator_list: Vec<Expression>,
Expand All @@ -936,7 +936,7 @@ pub struct FunctionDef {
#[derive(Debug, Clone)]
pub struct AsyncFunctionDef {
pub node: Node,
pub name: String,
pub name: StrId,
pub args: Arguments,
pub body: Vec<Statement>,
pub decorator_list: Vec<Expression>,
Expand All @@ -946,10 +946,32 @@ pub struct AsyncFunctionDef {
}

impl AsyncFunctionDef {
#[allow(clippy::too_many_arguments)]
pub fn new(
node: Node,
name: StrId,
args: Arguments,
body: Vec<Statement>,
decorator_list: Vec<Expression>,
returns: Option<Expression>,
type_comment: Option<&str>,
type_params: Vec<TypeParam>,
) -> Self {
Self {
node,
name,
args,
body,
decorator_list,
returns,
type_comment: type_comment.map(|s| s.to_owned()),
type_params,
}
}
pub fn to_function_def(&self) -> FunctionDef {
FunctionDef {
node: self.node,
name: self.name.clone(),
name: self.name,
args: self.args.clone(),
body: self.body.clone(),
decorator_list: self.decorator_list.clone(),
Expand Down Expand Up @@ -1608,31 +1630,7 @@ impl FunctionDef {
#[allow(clippy::too_many_arguments)]
pub fn new(
node: Node,
name: &str,
args: Arguments,
body: Vec<Statement>,
decorator_list: Vec<Expression>,
returns: Option<Expression>,
type_comment: Option<&str>,
type_params: Vec<TypeParam>,
) -> Self {
Self {
node,
name: name.to_owned(),
args,
body,
decorator_list,
returns,
type_comment: type_comment.map(|s| s.to_owned()),
type_params,
}
}
}
impl AsyncFunctionDef {
#[allow(clippy::too_many_arguments)]
pub fn new(
node: Node,
name: &str,
name: StrId,
args: Arguments,
body: Vec<Statement>,
decorator_list: Vec<Expression>,
Expand All @@ -1642,7 +1640,7 @@ impl AsyncFunctionDef {
) -> Self {
Self {
node,
name: name.to_owned(),
name,
args,
body,
decorator_list,
Expand Down
3 changes: 2 additions & 1 deletion parser/src/intern.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use fxhash::FxHashMap;
use serde::Serialize;

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize)]
pub struct StrId(u32);

#[derive(Default, Debug, Clone)]
Expand Down
40 changes: 20 additions & 20 deletions parser/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ impl<'a> Parser<'a> {
decorators: Vec<Expression>,
is_async: bool,
) -> Result<Statement, ParsingError> {
let name = self.cur_token().to_string(self.source);
let name = self.interner.intern(self.cur_token().as_str(self.source));
self.expect(Kind::Identifier)?;
let type_params = if self.at(Kind::LeftBrace) {
self.parse_type_parameters()?
Expand All @@ -601,28 +601,30 @@ impl<'a> Parser<'a> {
self.expect(Kind::Colon)?;
let body = self.parse_suite()?;
if is_async {
Ok(Statement::AsyncFunctionDef(Arc::new(AsyncFunctionDef {
node: self.finish_node_chomped(node),
name,
args,
body,
decorator_list: decorators,
returns: return_type,
type_comment: None,
type_params,
})))
Ok(Statement::AsyncFunctionDef(Arc::new(
AsyncFunctionDef::new(
self.finish_node_chomped(node),
name,
args,
body,
decorators,
return_type,
None,
type_params,
),
)))
} else {
Ok(Statement::FunctionDef(Arc::new(FunctionDef {
node: self.finish_node_chomped(node),
Ok(Statement::FunctionDef(Arc::new(FunctionDef::new(
self.finish_node_chomped(node),
name,
args,
body,
decorator_list: decorators,
returns: return_type,
decorators,
return_type,
// TODO: type comment
type_comment: None,
None,
type_params,
})))
))))
}
}

Expand Down Expand Up @@ -659,9 +661,7 @@ impl<'a> Parser<'a> {
self.start_node()
};
self.expect(Kind::Class)?;
let name = self
.interner
.intern(&self.source[self.cur_token().start as usize..self.cur_token().end as usize]);
let name = self.interner.intern(self.cur_token().as_str(self.source));
self.expect(Kind::Identifier)?;
let type_params = if self.at(Kind::LeftBrace) {
self.parse_type_parameters()?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Module {
start: 17,
end: 29,
},
name: "a",
name: StrId(
0,
),
args: Arguments {
node: Node {
start: 23,
Expand Down Expand Up @@ -53,7 +55,9 @@ Module {
start: 35,
end: 64,
},
name: "b",
name: StrId(
1,
),
args: Arguments {
node: Node {
start: 41,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Module {
start: 0,
end: 16,
},
name: "a",
name: StrId(
0,
),
args: Arguments {
node: Node {
start: 6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Module {
start: 0,
end: 12,
},
name: "a",
name: StrId(
0,
),
args: Arguments {
node: Node {
start: 6,
Expand Down
Loading

0 comments on commit c338fbf

Please sign in to comment.