Repository navigation
Bun.Transpiler: make treeShaking remove unused declarations and their imports #38352
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
Open
robobun
wants to merge
7
commits into
main
Choose a base branch
from
farm/5241778b/transpiler-tree-shaking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
173be93
Bun.Transpiler: make treeShaking remove unused declarations and their…
robobun 56b34ff
Keep every declaration of a live symbol; flag import records of remov…
robobun de08f61
Shorten comments
robobun 997039b
Single-line comments
robobun 1e8e05f
Assert the flag is off on cached parses; document the treeShaking def…
robobun a073794
ci: retrigger
robobun 726a69c
Shake before the post-visit prologue; let exports opt TypeScript impo…
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub mod scan_imports; | ||
| pub mod scan_side_effects; | ||
| pub(crate) mod scan_symbols; | ||
| pub(crate) mod scan_unused_parts; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| use crate::RuntimeImports; | ||
| use crate::p::P; | ||
| use bun_alloc::ArenaVec; | ||
| use bun_ast::{self as js_ast, DeclaredSymbol, ImportRecordFlags, Ref, flags}; | ||
| use bun_collections::HashMap; | ||
| use bun_crash_handler::handle_oom::handle_oom; | ||
| use smallvec::SmallVec; | ||
|
|
||
| /// The parts declaring each top-level symbol, keyed by the end of the symbol's `link` chain. | ||
| type DeclaringParts = HashMap<Ref, SmallVec<[u32; 1]>>; | ||
|
|
||
| impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_ONLY> { | ||
| /// Single-file tree shaking of the hoisted (`before`) and remaining top-level parts. | ||
| pub(crate) fn remove_unused_parts( | ||
| &mut self, | ||
| before: &mut ArenaVec<'a, js_ast::Part>, | ||
| parts: &mut ArenaVec<'a, js_ast::Part>, | ||
| ) { | ||
| // The bundler tree shakes in the linker, where cross-file uses are known. | ||
| debug_assert!(!self.options.bundle); | ||
|
|
||
| // Code inside a direct eval() can name any top-level declaration. | ||
| if self.module_scope().contains_direct_eval { | ||
| return; | ||
| } | ||
|
|
||
| let arena = self.arena; | ||
| let hoisted = before.len(); | ||
| let mut all = core::mem::replace(before, ArenaVec::new_in(arena)); | ||
| all.append(parts); | ||
|
|
||
| let mut live = bun_alloc::vec_from_iter_in(core::iter::repeat_n(false, all.len()), arena); | ||
| let mut worklist = ArenaVec::<u32>::new_in(arena); | ||
| let mut declaring_parts = DeclaringParts::default(); | ||
|
|
||
| for (i, part) in all.iter().enumerate() { | ||
| if !self.part_only_declares_removable_symbols(part) { | ||
| live[i] = true; | ||
| worklist.push(i as u32); | ||
| } | ||
| DeclaredSymbol::for_each_top_level_symbol( | ||
| &part.declared_symbols, | ||
| &mut declaring_parts, | ||
| |declaring_parts, declared| { | ||
| handle_oom(declaring_parts.get_or_put(self.follow_symbol_links(declared))) | ||
| .value_ptr | ||
| .push(i as u32); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| while let Some(i) = worklist.pop() { | ||
| let part = &all[i as usize]; | ||
| for &used in part.symbol_uses.keys() { | ||
| self.mark_declaring_parts_live(used, &declaring_parts, &mut live, &mut worklist); | ||
| } | ||
| // Every declaration of a live symbol stays: `export var x = 1; var x = 2;` | ||
| DeclaredSymbol::for_each_top_level_symbol( | ||
| &part.declared_symbols, | ||
| &mut (&mut live, &mut worklist), | ||
| |(live, worklist), declared| { | ||
| self.mark_declaring_parts_live(declared, &declaring_parts, live, worklist); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| for (i, (part, is_live)) in all.into_iter().zip(live.iter()).enumerate() { | ||
| if *is_live { | ||
| let kept = if i < hoisted { | ||
| &mut *before | ||
| } else { | ||
| &mut *parts | ||
| }; | ||
| kept.push(part); | ||
| continue; | ||
| } | ||
| // `scan()` and the linker skip unused records. | ||
| for &record_index in part.import_record_indices.iter() { | ||
| self.import_records.items_mut()[record_index as usize] | ||
| .flags | ||
| .insert(ImportRecordFlags::IS_UNUSED); | ||
| } | ||
| self.clear_symbol_usages_from_dead_part(&part); | ||
| } | ||
|
|
||
| self.forget_unused_runtime_helpers(); | ||
| } | ||
|
|
||
| fn mark_declaring_parts_live( | ||
| &self, | ||
| symbol: Ref, | ||
| declaring_parts: &DeclaringParts, | ||
| live: &mut [bool], | ||
| worklist: &mut ArenaVec<'a, u32>, | ||
| ) { | ||
| let Some(declaring) = declaring_parts.get(&self.follow_symbol_links(symbol)) else { | ||
| return; | ||
| }; | ||
| for &i in declaring { | ||
| if !live[i as usize] { | ||
| live[i as usize] = true; | ||
| worklist.push(i); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Imports stay here and get trimmed by use count in the import scanner. | ||
| fn part_only_declares_removable_symbols(&self, part: &js_ast::Part) -> bool { | ||
| part.can_be_removed_if_unused | ||
| && part.stmts.iter().all(|stmt| match &stmt.data { | ||
| js_ast::StmtData::SLocal(local) => !local.is_export, | ||
| js_ast::StmtData::SFunction(func) => { | ||
| !func.func.flags.contains(flags::Function::IsExport) | ||
| } | ||
| js_ast::StmtData::SClass(class) => !class.is_export, | ||
| // Generated next to a declaration, e.g. a TypeScript enum's closure. | ||
| js_ast::StmtData::SExpr(expr) => expr.does_not_affect_tree_shaking, | ||
| js_ast::StmtData::SEmpty(_) => true, | ||
| _ => false, | ||
| }) | ||
| } | ||
|
|
||
| /// Redeclaring a `var` or function links the earlier symbol to the new one. | ||
| fn follow_symbol_links(&self, mut ref_: Ref) -> Ref { | ||
| loop { | ||
| let symbol = &self.symbols[ref_.inner_index() as usize]; | ||
| if !symbol.has_link() { | ||
| return ref_; | ||
| } | ||
| ref_ = symbol.link.get(); | ||
| } | ||
| } | ||
|
|
||
| /// A `bun:wrap` helper whose callers were all swept would otherwise still be imported. | ||
| fn forget_unused_runtime_helpers(&mut self) { | ||
| let mut unused: SmallVec<[&'static [u8]; 4]> = SmallVec::new(); | ||
| let mut helpers = self.runtime_imports.iter(); | ||
| while let Some(helper) = helpers.next() { | ||
| if self.symbols[helper.value.inner_index() as usize].use_count_estimate == 0 { | ||
| unused.push(RuntimeImports::ALL[helper.key as usize]); | ||
| } | ||
| } | ||
| for name in unused { | ||
| self.runtime_imports.put(name, Ref::NONE); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.