Skip to content

Commit

Permalink
feat: add Renamer (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 authored Oct 30, 2023
1 parent 6524413 commit 7709cd4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 27 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"Finalizer",
"hasher",
"importee",
"Renamer",
"rolldown",
"rollup",
"rustc"
Expand Down
37 changes: 10 additions & 27 deletions crates/rolldown/src/bundler/chunk/de_conflict.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::borrow::Cow;

use oxc::span::Atom;
use rolldown_common::SymbolRef;
use rustc_hash::FxHashMap;

use super::chunk::Chunk;
use crate::bundler::{graph::graph::Graph, module::Module};
use crate::bundler::{graph::graph::Graph, module::Module, utils::renamer::Renamer};

impl Chunk {
pub fn de_conflict(&mut self, graph: &Graph) {
let mut name_to_count = self
let mut renamer = Renamer::new(&graph.symbols);

self
.modules
.iter()
.copied()
Expand All @@ -19,10 +17,10 @@ impl Chunk {
Module::External(_) => None,
})
.flatten()
.map(|name| (name, 1u32))
.collect::<FxHashMap<_, _>>();

let mut canonical_names: FxHashMap<SymbolRef, Atom> = FxHashMap::default();
.for_each(|name| {
// global names should be reserved
renamer.inc(name);
});

self
.modules
Expand All @@ -44,27 +42,12 @@ impl Chunk {
.flat_map(|part| part.declared_symbols.iter().copied()),
)
.for_each(|symbol_ref| {
let canonical_ref = graph.symbols.par_get_canonical_ref(symbol_ref);

let original_name = graph.symbols.get_original_name(canonical_ref);

match canonical_names.entry(canonical_ref) {
std::collections::hash_map::Entry::Occupied(_) => {}
std::collections::hash_map::Entry::Vacant(vacant) => {
let count = name_to_count.entry(Cow::Borrowed(original_name)).or_default();
if *count == 0 {
vacant.insert(original_name.clone());
} else {
vacant.insert(format!("{}${}", original_name, *count).into());
}
*count += 1;
}
}
renamer.add_top_level_symbol(symbol_ref);
});
}
Module::External(_) => {}
});

self.canonical_names = canonical_names;
self.canonical_names = renamer.into_canonical_names();
}
}
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod graph;
mod module;
pub mod options;
mod runtime;
pub mod utils;
mod visitors;

#[allow(clippy::module_inception)]
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod renamer;
46 changes: 46 additions & 0 deletions crates/rolldown/src/bundler/utils/renamer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::borrow::Cow;

use oxc::span::Atom;
use rolldown_common::SymbolRef;
use rustc_hash::FxHashMap;

use crate::bundler::graph::symbols::Symbols;

#[derive(Debug)]
pub struct Renamer<'name> {
name_to_count: FxHashMap<Cow<'name, Atom>, u32>,
canonical_names: FxHashMap<SymbolRef, Atom>,
symbols: &'name Symbols,
}

impl<'name> Renamer<'name> {
pub fn new(symbols: &'name Symbols) -> Self {
Self { name_to_count: FxHashMap::default(), canonical_names: FxHashMap::default(), symbols }
}

pub fn inc(&mut self, name: Cow<'name, Atom>) {
*self.name_to_count.entry(name).or_default() += 1;
}

pub fn add_top_level_symbol(&mut self, symbol_ref: SymbolRef) {
let canonical_ref = self.symbols.par_get_canonical_ref(symbol_ref);
let original_name = self.symbols.get_original_name(canonical_ref);

match self.canonical_names.entry(canonical_ref) {
std::collections::hash_map::Entry::Occupied(_) => {}
std::collections::hash_map::Entry::Vacant(vacant) => {
let count = self.name_to_count.entry(Cow::Borrowed(original_name)).or_default();
if *count == 0 {
vacant.insert(original_name.clone());
} else {
vacant.insert(format!("{}${}", original_name, *count).into());
}
*count += 1;
}
}
}

pub fn into_canonical_names(self) -> FxHashMap<SymbolRef, Atom> {
self.canonical_names
}
}

0 comments on commit 7709cd4

Please sign in to comment.