Skip to content

feat(minifier): remove unused import specifiers#16797

Merged
graphite-app[bot] merged 1 commit intomainfrom
c/12-13-feat_minifier_remove_unused_import_specifiers
Dec 15, 2025
Merged

feat(minifier): remove unused import specifiers#16797
graphite-app[bot] merged 1 commit intomainfrom
c/12-13-feat_minifier_remove_unused_import_specifiers

Conversation

@camc314
Copy link
Contributor

@camc314 camc314 commented Dec 13, 2025

Fixes #16742

🤖 generated with help from Claude Opus 4.5

@github-actions github-actions bot added A-minifier Area - Minifier C-enhancement Category - New feature or request labels Dec 13, 2025
Copy link
Contributor Author

camc314 commented Dec 13, 2025


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.

@camc314 camc314 force-pushed the c/12-13-feat_minifier_remove_unused_import_specifiers branch 2 times, most recently from 3b08078 to d174888 Compare December 13, 2025 14:17
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 13, 2025

CodSpeed Performance Report

Merging #16797 will not alter performance

Comparing c/12-13-feat_minifier_remove_unused_import_specifiers (74eae13) with main (763b25a)

Summary

✅ 38 untouched
⏩ 7 skipped1

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@camc314 camc314 force-pushed the c/12-13-feat_minifier_remove_unused_import_specifiers branch from d174888 to 4bceec6 Compare December 13, 2025 14:20
@camc314 camc314 marked this pull request as ready for review December 13, 2025 14:21
Copilot AI review requested due to automatic review settings December 13, 2025 14:21
Copy link

@charliecreates charliecreates bot left a comment

Choose a reason for hiding this comment

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

The new unused-import-specifier removal is directionally correct and well-tested, but there are a couple of semantics/compatibility risks around converting to side-effect-only imports in the presence of import attributes/assertions, especially for import {} from 'x' with { ... }. The implementation’s ctx.state.changed signaling is a bit brittle due to scattered mutation paths. Finally, the same transformation is wired into both peephole and DCE traversals, which increases the chance of order-dependent behavior and redundant work.

Summary of changes

What changed

  • Added unused-import-specifier removal pass to the minifier peephole pipeline:
    • PeepholeOptimizations now handles Statement::ImportDeclaration(_) via remove_unused_import_specifiers.
    • DeadCodeElimination also invokes the same logic for ImportDeclaration statements.
  • Implemented remove_unused_import_specifiers in crates/oxc_minifier/src/peephole/remove_unused_declaration.rs:
    • Skips when unused option is Keep, when parsing as script, or when root scope contains direct eval.
    • Removes specifiers whose local symbols are marked unused.
    • If all specifiers are removed, converts to a side-effect-only import (import 'x'), preserving potential module side effects.
  • Added extensive tests covering default/named/namespace/mixed imports, partial retention, dead-code scenario from #16742, import attributes, renames, and re-export usage.
  • Updated allocation snapshot (allocs_minifier.snap) reflecting changed allocation counts.

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 implements the removal of unused import specifiers as part of the minifier's dead code elimination. The feature converts imports with all unused specifiers into side-effect-only imports (e.g., import a from 'a'import 'a') to preserve potential side effects while removing unused bindings.

Key changes:

  • Adds remove_unused_import_specifiers() function that processes import declarations and removes unused specifiers
  • Integrates the new function into both PeepholeOptimizations and DeadCodeElimination traversal passes
  • Includes comprehensive test coverage for various import patterns (default, named, namespace, mixed, partial removal)

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
crates/oxc_minifier/src/peephole/remove_unused_declaration.rs Adds new remove_unused_import_specifiers() function with documentation and comprehensive test suite covering various import scenarios
crates/oxc_minifier/src/peephole/mod.rs Integrates the new function into both PeepholeOptimizations::exit_statement() and DeadCodeElimination::exit_statement() to process import declarations
tasks/track_memory_allocations/allocs_minifier.snap Updates memory allocation snapshot showing reduced allocations (84 vs 85 sys allocs) for RadixUIAdoptionSection.jsx due to removed import specifiers

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

@camc314 camc314 force-pushed the c/12-13-feat_minifier_remove_unused_import_specifiers branch 3 times, most recently from db72ac2 to 56940c0 Compare December 13, 2025 14:34
@camc314 camc314 requested a review from Copilot December 13, 2025 14:35
Copy link

@charliecreates charliecreates bot left a comment

Choose a reason for hiding this comment

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

Main concerns are maintainability and determinism: the same import-specifier removal runs in both peephole and DCE, which can lead to redundant work and order-dependent behavior. Additionally, ctx.state.changed signaling in remove_unused_import_specifiers is still somewhat coupled to vector-length checks rather than explicit before/after structural state, making it fragile to future edits. The semantic/compatibility risk around import {} ... with {} appears covered by the added test, but the implementation would benefit from more robust change tracking.

Additional notes (1)
  • Maintainability | crates/oxc_minifier/src/peephole/mod.rs:169-169
    remove_unused_import_specifiers is invoked from both PeepholeOptimizations and DeadCodeElimination. That creates a risk of order-dependent behavior (one pass mutates the import, the other sees a different shape) and redundant work. It also makes reasoning about ctx.state.changed and later optimizations harder because the same transformation can happen in multiple phases.

If the intent is “always do this”, consider making it a single, well-defined stage (either peephole or DCE), or ensure one of the callers is gated (e.g., only run in DCE where symbol-use info is guaranteed complete).

Summary of changes

Summary of changes

  • Minifier now removes unused import specifiers by introducing PeepholeOptimizations::remove_unused_import_specifiers.
  • Wired the new optimization into two traversals:
    • PeepholeOptimizations::exit_statement() handles Statement::ImportDeclaration(_).
    • DeadCodeElimination::exit_statement() also invokes the same import-specifier removal.
  • Implementation details (crates/oxc_minifier/src/peephole/remove_unused_declaration.rs):
    • Skips when root scope contains direct eval.
    • For ImportDeclaration with specifiers: Some(...), retains only specifiers whose local symbols are not marked unused.
    • If all specifiers are removed, converts to a side-effect-only import by setting import_decl.specifiers = None.
  • Added comprehensive tests for default/named/namespace/mixed imports, partial removals, import {} handling, with { type: 'json' } attributes, renames, re-exports, and direct-eval safety.
  • Updated allocation snapshot showing fewer allocations for RadixUIAdoptionSection.jsx.

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


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

@camc314 camc314 marked this pull request as draft December 13, 2025 14:40
@camc314
Copy link
Contributor Author

camc314 commented Dec 13, 2025

The new unused-import-specifier removal is directionally correct and well-tested, but there are a couple of semantics/compatibility risks around converting to side-effect-only imports in the presence of import attributes/assertions, especially for import {} from 'x' with { ... }.

Import Source and import Defer are valid points, i've disabled this optimization when these are supplied

The implementation’s ctx.state.changed signaling is a bit brittle due to scattered mutation paths.

I've refactored this to be slightly cleaner. We now only change ctx.state.changed at function exit

Finally, the same transformation is wired into both peephole and DCE traversals, which increases the chance of order-dependent behavior and redundant work.

I think this change is OK, since we follow the same pattern as the other code removal paths, e.g.remove_unused_class_declaration

@CharlieHelps re-review this PR please

@camc314 camc314 force-pushed the c/12-13-feat_minifier_remove_unused_import_specifiers branch from 56940c0 to 5ba4d67 Compare December 13, 2025 14:47
@camc314 camc314 requested a review from Copilot December 13, 2025 14:47
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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.


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

@camc314 camc314 marked this pull request as ready for review December 13, 2025 14:57
@camc314 camc314 requested review from CharlieHelps and removed request for CharlieHelps December 13, 2025 15:06
Copy link

@charliecreates charliecreates bot left a comment

Choose a reason for hiding this comment

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

The main issues are determinism/maintainability: remove_unused_import_specifiers is executed in both peephole and DCE, increasing redundant work and the risk of order-dependent behavior. Relying on debug_assert! for module-only invariants makes behavior differ between debug and release builds; use an explicit source_type().is_script() guard if the optimization is module-only. The phase.is_some() early return is overly broad and should be narrowed or explicitly justified. Finally, ctx.state.changed should be driven by a single before/after decision to reduce future fragility.

Additional notes (1)
  • Maintainability | crates/oxc_minifier/src/peephole/mod.rs:169-169
    Running remove_unused_import_specifiers in both PeepholeOptimizations and DeadCodeElimination increases the chance of order-dependent behavior and redundant work. After peephole runs, DCE will observe a mutated AST shape (e.g., specifiers=None), which can subtly affect other downstream heuristics and ctx.state.changed semantics. If symbol-usage information is the prerequisite, this pass belongs in the stage where that information is authoritative, not duplicated across stages.
Summary of changes

Summary of changes

  • Added a new minifier optimization remove_unused_import_specifiers to remove unused import bindings and, when all bindings are removed, convert the statement to a side-effect-only import (import 'x').
  • Wired the optimization into two traversal passes:
    • PeepholeOptimizations::exit_statement now handles Statement::ImportDeclaration(_).
    • DeadCodeElimination::exit_statement now also handles Statement::ImportDeclaration(_).
  • Implemented guards in remove_unused_import_specifiers:
    • Skip when options.unused == Keep.
    • Skip when the root scope contains direct eval.
    • Skip when import_decl.phase.is_some().
    • Added a debug_assert!(!ctx.source_type().is_script()).
  • Added extensive unit tests covering default/named/namespace/mixed imports, partial removals, import {} handling, with { type: 'json' } attributes, renames, re-export usage, and direct-eval safety.
  • Updated memory allocation snapshot (allocs_minifier.snap) reflecting fewer allocations for RadixUIAdoptionSection.jsx.

@charliecreates charliecreates bot removed the request for review from CharlieHelps December 13, 2025 15:08
@graphite-app
Copy link
Contributor

graphite-app bot commented Dec 15, 2025

Merge activity

Fixes #16742

🤖 generated with help from Claude Opus 4.5
@graphite-app graphite-app bot force-pushed the c/12-13-feat_minifier_remove_unused_import_specifiers branch from 5ba4d67 to 74eae13 Compare December 15, 2025 11:29
@graphite-app graphite-app bot merged commit 74eae13 into main Dec 15, 2025
27 checks passed
@graphite-app graphite-app bot deleted the c/12-13-feat_minifier_remove_unused_import_specifiers branch December 15, 2025 11:35
overlookmotel added a commit that referenced this pull request Dec 19, 2025
### 🚀 Features

- d209c21 allocator: Add cap to FixedSizeAllocatorPool and block when
exhausted (#17023) (Cameron)
- fb2af91 allocator: Add bitset utils (#17042) (zhaoting zhou)
- c16082c tasks/compat_data: Integrate `node-compat-table` (#16831)
(Boshen)
- 5586823 span: Extract TS declaration file check to its own function
(#17037) (camchenry)
- 3d2b492 minifier: Fold iife arrow functions in call expressions
(#16477) (Armano)
- 67e9f9e codegen: Keep comments on the export specifiers (#16943)
(夕舞八弦)
- cb515fa parser: Improve error message for `yield` as identifier usage
(#16950) (sapphi-red)
- dcc856b parser: Add help for `new_dynamic_import` error (#16949)
(sapphi-red)
- c3c79f8 parser: Improve import attribute value error message (#16948)
(sapphi-red)
- 291b57b ast_tools: Generate TS declaration files for deserializer and
walk files (#16912) (camc314)
- 74eae13 minifier: Remove unused import specifiers (#16797) (camc314)

### 🐛 Bug Fixes

- fb9e193 linter: OOM problems with custom plugins (#17082)
(overlookmotel)
- e59132b parser/napi: Fix lazy deser (#17069) (overlookmotel)
- a92faf0 ast_tools: Support `u128` in `assert_layouts` generator
(#17050) (overlookmotel)
- 47b4c2f minifier/docs: Correct hyperlink path in OPTIMIZATIONS.md
(#16986) (GRK)
- 3002649 transformer/typescript: Remove unused import equals
declaration (#16776) (Dunqing)
- 5a2af88 regular_expression: Correct named capture group reference
error (#16952) (sapphi-red)

### ⚡ Performance

- b657bb6 allocator: Reduce time `Mutex` lock is held in
`FixedSizeAllocatorPool::get` (#17079) (overlookmotel)
- 1f3b19b ast: `#[ast]` macro use `#[repr(transparent)]` for
single-field structs (#17052) (overlookmotel)
- 225f229 parser: Use SmallVec for duplicate default export detection
(#16801) (camc314)

### 📚 Documentation

- a9c419f traverse: Update safety comments (#16944) (overlookmotel)

Co-authored-by: overlookmotel <[email protected]>
graphite-app bot pushed a commit that referenced this pull request Dec 23, 2025
Added a new option (`invalid_import_side_effects`) to disable #16797 as it requires a new assumptions.

refs rolldown/rolldown#7512 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-minifier Area - Minifier C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

minifier: remove unused specifiers from import statements

2 participants

Comments