Skip to content

Commit c023ba6

Browse files
committed
fix(semantic): do not duplicate statements in temp Vec when binding TSModuleDeclarations (#15724)
I *think* this was a bug, but I don't really understand the logic, so not 100% sure. When binding a `TSModuleDeclaration`, references to all its statements are collected into a `Vec` called `module_declaration_stmts`: https://github.com/oxc-project/oxc/blob/4608549fc7715ee76c4a6d7a78069ff339f52267/crates/oxc_semantic/src/binder.rs#L434-L454 But `module_declaration_stmts.extend(block.body.iter())` is *inside* the loop, meaning that *all* the statements are inserted over and over until a statement is found which instantiates the namespace. e.g. this would push 25 `&Statement`s into the `Vec`: ```ts namespace X { type A = string; type B = number; type C = string; type D = number; interface E {} } ``` The number of entries pushed to the `Vec` rises exponentially with the number of statements in the namespace (100 statements -> 10,000 entries pushed). I assume this is unintentional. Move `module_declaration_stmts.extend(block.body.iter());` to before the loop. --- Do the `&Statement`s need to be pushed before the loop? Could it be delayed until after? That would be more performant. Though, as I said, I don't understand the logic, so I'm not sure if that'd break it.
1 parent bf20cf5 commit c023ba6

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

crates/oxc_semantic/src/binder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,10 @@ fn get_module_instance_state_impl<'a, 'b>(
433433
// A module is uninstantiated if it contains only specific declarations
434434
let state = match body {
435435
TSModuleDeclarationBody::TSModuleBlock(block) => {
436+
module_declaration_stmts.extend(block.body.iter());
437+
436438
let mut child_state = ModuleInstanceState::NonInstantiated;
437439
for stmt in &block.body {
438-
module_declaration_stmts.extend(block.body.iter());
439440
child_state = get_module_instance_state_for_statement(
440441
builder,
441442
stmt,

0 commit comments

Comments
 (0)