Skip to content
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): remove a branch from add_scope #4384

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl<'a> SemanticBuilder<'a> {
/// # Panics
pub fn build(mut self, program: &Program<'a>) -> SemanticBuilderReturn<'a> {
if self.source_type.is_typescript_definition() {
let scope_id = self.scope.add_scope(None, AstNodeId::DUMMY, ScopeFlags::Top);
let scope_id = self.scope.add_root_scope(AstNodeId::DUMMY, ScopeFlags::Top);
program.scope_id.set(Some(scope_id));
} else {
self.visit_program(program);
Expand Down Expand Up @@ -471,8 +471,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
};
flags = self.scope.get_new_scope_flags(flags, parent_scope_id);

self.current_scope_id =
self.scope.add_scope(Some(parent_scope_id), self.current_node_id, flags);
self.current_scope_id = self.scope.add_scope(parent_scope_id, self.current_node_id, flags);
scope_id.set(Some(self.current_scope_id));

if self.scope.get_flags(parent_scope_id).is_catch_clause() {
Expand Down Expand Up @@ -553,7 +552,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
if program.is_strict() {
flags |= ScopeFlags::StrictMode;
}
self.current_scope_id = self.scope.add_scope(None, self.current_node_id, flags);
self.current_scope_id = self.scope.add_root_scope(self.current_node_id, flags);
program.scope_id.set(Some(self.current_scope_id));

if let Some(hashbang) = &program.hashbang {
Expand Down
30 changes: 24 additions & 6 deletions crates/oxc_semantic/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,31 @@ impl ScopeTree {
&mut self.bindings[scope_id]
}

/// Create scope.
/// For root (`Program`) scope, use `add_root_scope`.
pub fn add_scope(
&mut self,
parent_id: ScopeId,
node_id: AstNodeId,
flags: ScopeFlags,
) -> ScopeId {
let scope_id = self.add_scope_impl(Some(parent_id), node_id, flags);

// Set this scope as child of parent scope
self.child_ids[parent_id].push(scope_id);

scope_id
}

/// Create root (`Program`) scope.
pub fn add_root_scope(&mut self, node_id: AstNodeId, flags: ScopeFlags) -> ScopeId {
self.add_scope_impl(None, node_id, flags)
}

// `#[inline]` because almost always called from `add_scope` and want to avoid
// overhead of a function call there.
#[inline]
fn add_scope_impl(
&mut self,
parent_id: Option<ScopeId>,
node_id: AstNodeId,
Expand All @@ -202,12 +226,6 @@ impl ScopeTree {
self.flags.push(flags);
self.bindings.push(Bindings::default());
self.node_ids.push(node_id);

// Set this scope as child of parent scope.
if let Some(parent_id) = parent_id {
self.child_ids[parent_id].push(scope_id);
}

scope_id
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl TraverseScoping {
/// `flags` provided are amended to inherit from parent scope's flags.
pub fn create_scope_child_of_current(&mut self, flags: ScopeFlags) -> ScopeId {
let flags = self.scopes.get_new_scope_flags(flags, self.current_scope_id);
self.scopes.add_scope(Some(self.current_scope_id), AstNodeId::DUMMY, flags)
self.scopes.add_scope(self.current_scope_id, AstNodeId::DUMMY, flags)
}

/// Insert a scope into scope tree below a statement.
Expand Down
Loading