Skip to content

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Nov 15, 2025

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:

let state = match body {
TSModuleDeclarationBody::TSModuleBlock(block) => {
let mut child_state = ModuleInstanceState::NonInstantiated;
for stmt in &block.body {
module_declaration_stmts.extend(block.body.iter());
child_state = get_module_instance_state_for_statement(
builder,
stmt,
current_node_id,
module_declaration_stmts,
);
if child_state.is_instantiated() {
break;
}
}
child_state
}
TSModuleDeclarationBody::TSModuleDeclaration(module) => {
get_module_instance_state(builder, module, current_node_id)
}
};

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 &Statements into the Vec:

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 &Statements 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.

@github-actions github-actions bot added A-semantic Area - Semantic C-bug Category - Bug labels Nov 15, 2025
Copy link
Member Author


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 15, 2025

CodSpeed Performance Report

Merging #15724 will not alter performance

Comparing 11-15-fix_semantic_do_not_duplicate_statements_in_temp_vec_when_binding_tsmoduledeclaration_s (99e3d41) with main (d8d4e31)1

Summary

✅ 37 untouched

Footnotes

  1. No successful run was found on main (4608549) during the generation of this report, so d8d4e31 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@overlookmotel overlookmotel marked this pull request as ready for review November 15, 2025 16:17
Copilot AI review requested due to automatic review settings November 15, 2025 16:17
Copilot finished reviewing on behalf of overlookmotel November 15, 2025 16:18
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a performance bug where statements in TypeScript module declarations were being duplicated exponentially in a temporary Vec during the binding process.

Key Changes:

  • Moved the module_declaration_stmts.extend(block.body.iter()) call outside the loop in get_module_instance_state_impl
  • This prevents statements from being added repeatedly on each iteration, reducing complexity from O(n²) to O(n)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Member

@Dunqing Dunqing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, good catch, it is a bug indeed!

Do the &Statements 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.

I do not remember the logic very well. I just roughly looked through the implementation again, and don't see the blocker for this

@Dunqing Dunqing added the 0-merge Merge with Graphite Merge Queue label Nov 17, 2025
Copy link
Member

Dunqing commented Nov 17, 2025

Merge activity

… `TSModuleDeclaration`s (#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.
@graphite-app graphite-app bot force-pushed the 11-15-fix_semantic_do_not_duplicate_statements_in_temp_vec_when_binding_tsmoduledeclaration_s branch from 99e3d41 to c023ba6 Compare November 17, 2025 08:57
@graphite-app graphite-app bot merged commit c023ba6 into main Nov 17, 2025
20 checks passed
@graphite-app graphite-app bot deleted the 11-15-fix_semantic_do_not_duplicate_statements_in_temp_vec_when_binding_tsmoduledeclaration_s branch November 17, 2025 09:03
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Nov 17, 2025
graphite-app bot pushed a commit that referenced this pull request Nov 17, 2025
### 💥 BREAKING CHANGES

- ea51b0b napi: [**BREAKING**] Standardize function naming with sync suffixes (#15661) (Boshen)

### 🚀 Features

- 77efb76 parser: Improve error message for invalid switch clauses (#15728) (sapphi-red)
- 5691727 parser: Improve `import source` `from` error message (#15727) (sapphi-red)
- b7404bc parser: Improve error message for missing function body (#15726) (sapphi-red)
- 71c2fb0 parser: Improve error message when JSX is found while not enabled (#15725) (sapphi-red)
- 56e7e44 minifier: Disable removal of unnecessary `use strict` directives for DCE (#15691) (sapphi-red)
- 8a61cfd allocator, ast: Introduce `UnstableAddress` trait (#15700) (overlookmotel)
- f5ce55a napi: Export all options using wildcard exports (Boshen)
- 68703b9 minifier: Rotate binary expressions to remove parentheses (#15473) (sapphi-red)

### 🐛 Bug Fixes

- c023ba6 semantic: Do not duplicate statements in temp `Vec` when binding `TSModuleDeclaration`s (#15724) (overlookmotel)
- d60ca81 parser: Reject `import something 'source'` (#15746) (sapphi-red)
- e0728fa ast: Exclude comment end position from `is_inside_comment` check (#15753) (camc314)
- 9f54a36 semantic: Error on `\00` in strict mode (#15743) (sapphi-red)
- 440a977 ast: Include rest properties when using `get_binding_identifiers` (#15710) (camc314)

### ⚡ Performance

- 1f09d3c parser: Faster checking for invalid modifiers (#15717) (overlookmotel)
- d8d4e31 ast: Use loop instead of recursion in `TSModuleDeclarationBody::as_module_block_mut` (#15713) (overlookmotel)

### 📚 Documentation

- e033d50 ast: Clarify behavior of `TSModuleDeclaration::has_use_strict_directive` (#15730) (overlookmotel)
- 9eda70f allocator: Improve docs for `Address` methods (#15697) (overlookmotel)
overlookmotel pushed a commit that referenced this pull request Nov 17, 2025
### 💥 BREAKING CHANGES

- ea51b0b napi: [**BREAKING**] Standardize function naming with sync
suffixes (#15661) (Boshen)

### 🚀 Features

- 77efb76 parser: Improve error message for invalid switch clauses
(#15728) (sapphi-red)
- 5691727 parser: Improve `import source` `from` error message (#15727)
(sapphi-red)
- b7404bc parser: Improve error message for missing function body
(#15726) (sapphi-red)
- 71c2fb0 parser: Improve error message when JSX is found while not
enabled (#15725) (sapphi-red)
- 56e7e44 minifier: Disable removal of unnecessary `use strict`
directives for DCE (#15691) (sapphi-red)
- 8a61cfd allocator, ast: Introduce `UnstableAddress` trait (#15700)
(overlookmotel)
- f5ce55a napi: Export all options using wildcard exports (Boshen)
- 68703b9 minifier: Rotate binary expressions to remove parentheses
(#15473) (sapphi-red)

### 🐛 Bug Fixes

- c023ba6 semantic: Do not duplicate statements in temp `Vec` when
binding `TSModuleDeclaration`s (#15724) (overlookmotel)
- d60ca81 parser: Reject `import something 'source'` (#15746)
(sapphi-red)
- e0728fa ast: Exclude comment end position from `is_inside_comment`
check (#15753) (camc314)
- 9f54a36 semantic: Error on `\00` in strict mode (#15743) (sapphi-red)
- 440a977 ast: Include rest properties when using
`get_binding_identifiers` (#15710) (camc314)

### ⚡ Performance

- 1f09d3c parser: Faster checking for invalid modifiers (#15717)
(overlookmotel)
- d8d4e31 ast: Use loop instead of recursion in
`TSModuleDeclarationBody::as_module_block_mut` (#15713) (overlookmotel)

### 📚 Documentation

- e033d50 ast: Clarify behavior of
`TSModuleDeclaration::has_use_strict_directive` (#15730) (overlookmotel)
- 9eda70f allocator: Improve docs for `Address` methods (#15697)
(overlookmotel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-semantic Area - Semantic C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants