-
-
Notifications
You must be signed in to change notification settings - Fork 476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(semantic): calculate number of nodes, scopes, symbols, references before visiting AST #4328
Changes from 12 commits
c4fbba1
59e4c74
f573c34
bbf8dc3
a698196
dc6b249
53bc5f5
e68d3fd
221674f
753c64d
ad6aa55
88b4bdf
b4a8d67
2992c54
9d58b6c
a615891
d7b8072
1cec151
062e3b3
8a2971b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -100,6 +100,58 @@ pub struct SemanticBuilderReturn<'a> { | |||||||||||||||||||||
pub errors: Vec<OxcDiagnostic>, | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
#[derive(Default, Debug)] | ||||||||||||||||||||||
pub struct Collector { | ||||||||||||||||||||||
node: usize, | ||||||||||||||||||||||
scope: usize, | ||||||||||||||||||||||
symbol: usize, | ||||||||||||||||||||||
reference: usize, | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
impl<'a> Visit<'a> for Collector { | ||||||||||||||||||||||
#[inline] | ||||||||||||||||||||||
fn enter_node(&mut self, _: AstKind<'a>) { | ||||||||||||||||||||||
self.node += 1; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
#[inline] | ||||||||||||||||||||||
fn enter_scope(&mut self, _: ScopeFlags, _: &Cell<Option<ScopeId>>) { | ||||||||||||||||||||||
self.scope += 1; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
#[inline] | ||||||||||||||||||||||
fn leave_node(&mut self, _: AstKind<'a>) {} | ||||||||||||||||||||||
Dunqing marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
#[inline] | ||||||||||||||||||||||
fn leave_scope(&mut self) {} | ||||||||||||||||||||||
|
||||||||||||||||||||||
fn visit_binding_identifier(&mut self, _: &BindingIdentifier<'a>) { | ||||||||||||||||||||||
self.symbol += 1; | ||||||||||||||||||||||
self.node += 1; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
fn visit_identifier_reference(&mut self, _: &IdentifierReference<'a>) { | ||||||||||||||||||||||
self.reference += 1; | ||||||||||||||||||||||
self.node += 1; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
fn visit_jsx_identifier(&mut self, _: &JSXIdentifier<'a>) { | ||||||||||||||||||||||
self.reference += 1; | ||||||||||||||||||||||
self.node += 1; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
fn visit_jsx_member_expression_object(&mut self, it: &JSXMemberExpressionObject<'a>) { | ||||||||||||||||||||||
if let JSXMemberExpressionObject::MemberExpression(expr) = &it { | ||||||||||||||||||||||
self.visit_jsx_member_expression(expr); | ||||||||||||||||||||||
} else { | ||||||||||||||||||||||
self.node += 1; | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you're counting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oxc/crates/oxc_semantic/src/builder.rs Lines 1966 to 1975 in b4a8d67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things: In that case shouldn't it call I don't think we necessarily need to make the counts 100% accurate. Over-estimating is fine. It may be that the cost of complicating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If I call the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless I'm reading That would be correct behavior - in But this isn't easy to reason about. #3528 would make it much simpler. |
||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
// fn visit_jsx_element_name(&mut self, it: &JSXElementName<'a>) { | ||||||||||||||||||||||
// if let JSXElementName::Identifier(ident) = it { | ||||||||||||||||||||||
// if !ident.name.chars().next().is_some_and(char::is_uppercase) { | ||||||||||||||||||||||
// return; | ||||||||||||||||||||||
// } | ||||||||||||||||||||||
// } | ||||||||||||||||||||||
|
||||||||||||||||||||||
// self.visit_jsx_element_name(it); | ||||||||||||||||||||||
// } | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
impl<'a> SemanticBuilder<'a> { | ||||||||||||||||||||||
pub fn new(source_text: &'a str, source_type: SourceType) -> Self { | ||||||||||||||||||||||
let scope = ScopeTree::default(); | ||||||||||||||||||||||
|
@@ -192,8 +244,13 @@ impl<'a> SemanticBuilder<'a> { | |||||||||||||||||||||
let scope_id = self.scope.add_scope(None, AstNodeId::DUMMY, ScopeFlags::Top); | ||||||||||||||||||||||
program.scope_id.set(Some(scope_id)); | ||||||||||||||||||||||
} else { | ||||||||||||||||||||||
self.visit_program(program); | ||||||||||||||||||||||
let mut collector = Collector::default(); | ||||||||||||||||||||||
collector.visit_program(program); | ||||||||||||||||||||||
self.nodes.reserve(collector.node); | ||||||||||||||||||||||
self.scope.reserve(collector.scope); | ||||||||||||||||||||||
self.symbols.reserve(collector.symbol, collector.reference); | ||||||||||||||||||||||
|
||||||||||||||||||||||
self.visit_program(program); | ||||||||||||||||||||||
// Checking syntax error on module record requires scope information from the previous AST pass | ||||||||||||||||||||||
if self.check_syntax_error { | ||||||||||||||||||||||
checker::check_module_record(&self); | ||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should move this to another file and have some doc comments explaning the why before merge.