Skip to content

Commit

Permalink
feat: code spltting for dynamic import (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Oct 8, 2023
1 parent 79de1ad commit 6cd5c8e
Show file tree
Hide file tree
Showing 18 changed files with 218 additions and 64 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ serde = { version = "1.0.147", features = ["derive"] }
serde_json = "1.0.87"
insta = "1.21.0"
testing_macros = "0.2.7"
beef = "0.5.2"
scoped-tls = "1.0"
string_wizard = { version = "0.0.7" }
59 changes: 39 additions & 20 deletions crates/rolldown/src/bundler/bundle/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::bundler::{
bitset::BitSet,
chunk::{
chunk::{Chunk, CrossChunksMeta},
ChunksVec,
ChunkId, ChunksVec,
},
graph::graph::Graph,
module::module::{Module, ModuleRenderContext},
Expand All @@ -14,7 +14,7 @@ use crate::bundler::{
};
use anyhow::Ok;
use index_vec::IndexVec;
use rolldown_common::ModuleId;
use rolldown_common::{ImportKind, ModuleId};
use rustc_hash::FxHashMap;

pub struct Bundle<'a> {
Expand All @@ -41,13 +41,16 @@ impl<'a> Bundle<'a> {
}
modules_entry_bit[module_id].set_bit(index as u32);
if let Module::Normal(m) = &self.graph.modules[module_id] {
m.import_records
.iter()
.for_each(|i| self.mark_modules_entry_bit(i.resolved_module, index, modules_entry_bit));
m.import_records.iter().for_each(|i| {
// because dynamic import is already as entry, so here ignore it
if i.kind != ImportKind::DynamicImport {
self.mark_modules_entry_bit(i.resolved_module, index, modules_entry_bit)
}
});
}
}

pub fn generate_chunks(&self) -> ChunksVec {
pub fn generate_chunks(&self) -> (ChunksVec, IndexVec<ModuleId, Option<ChunkId>>) {
let mut module_to_bits = index_vec::index_vec![
BitSet::new(self.graph.entries.len().try_into().unwrap());
self.graph.modules.len()
Expand All @@ -56,11 +59,11 @@ impl<'a> Bundle<'a> {
let mut chunks = FxHashMap::default();
chunks.shrink_to(self.graph.entries.len());

for (i, (name, _)) in self.graph.entries.iter().enumerate() {
for (i, (name, module_id)) in self.graph.entries.iter().enumerate() {
let count: u32 = i as u32;
let mut entry_bits = BitSet::new(self.graph.entries.len() as u32);
entry_bits.set_bit(count);
let c = Chunk::new(name.clone(), true, entry_bits.clone(), vec![]);
let c = Chunk::new(name.clone(), Some(*module_id), entry_bits.clone(), vec![]);
chunks.insert(entry_bits, c);
}

Expand All @@ -82,25 +85,34 @@ impl<'a> Bundle<'a> {
let len = chunks.len();
chunks.insert(
bits.clone(),
Chunk::new(
Some(len.to_string()),
false,
bits.clone(),
vec![module.id()],
),
Chunk::new(Some(len.to_string()), None, bits.clone(), vec![module.id()]),
);
}
});

chunks
let chunks = chunks
.into_values()
.map(|mut chunk| {
chunk
.modules
.sort_by_key(|id| self.graph.modules[*id].exec_order());
chunk
})
.collect::<ChunksVec>()
.collect::<ChunksVec>();

let mut module_to_chunk: IndexVec<ModuleId, Option<ChunkId>> = index_vec::index_vec![
None;
self.graph.modules.len()
];

// perf: this process could be done with computing chunks together
for (i, chunk) in chunks.iter_enumerated() {
for module_id in &chunk.modules {
module_to_chunk[*module_id] = Some(i);
}
}

(chunks, module_to_chunk)
}

pub fn generate_cross_chunks_meta(&mut self, _chunks: &ChunksVec) -> CrossChunksMeta {
Expand All @@ -110,11 +122,12 @@ impl<'a> Bundle<'a> {

pub fn generate(
&mut self,
input_options: &'a NormalizedInputOptions,
_input_options: &'a NormalizedInputOptions,
) -> anyhow::Result<Vec<Asset>> {
use rayon::prelude::*;
let mut chunks = self.generate_chunks();
let (mut chunks, module_to_chunk) = self.generate_chunks();
let _generate_cross_chunks_meta = self.generate_cross_chunks_meta(&chunks);

chunks
.iter_mut()
.par_bridge()
Expand All @@ -124,8 +137,12 @@ impl<'a> Bundle<'a> {
chunk.de_conflict(self.graph);
});

let mut entries_chunk_final_names = FxHashMap::default();
entries_chunk_final_names.shrink_to(self.graph.entries.len());

chunks.iter_mut().for_each(|chunk| {
if chunk.is_entry {
if let Some(module_id) = chunk.entry_module {
entries_chunk_final_names.insert(module_id, chunk.file_name.clone().unwrap());
chunk.initialize_exports(&mut self.graph.modules, &self.graph.symbols);
}
});
Expand All @@ -139,14 +156,16 @@ impl<'a> Bundle<'a> {
module.render(ModuleRenderContext {
canonical_names: &chunks[0].canonical_names,
symbols: &self.graph.symbols,
module_to_chunk: &module_to_chunk,
chunks: &chunks,
});
});

let assets = chunks
.iter()
.enumerate()
.map(|(_chunk_id, c)| {
let content = c.render(self.graph, input_options).unwrap();
let content = c.render(self.graph, &module_to_chunk, &chunks).unwrap();

Asset {
file_name: c.file_name.clone().unwrap(),
Expand Down
21 changes: 14 additions & 7 deletions crates/rolldown/src/bundler/chunk/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ use crate::bundler::{
graph::{graph::Graph, symbols::Symbols},
module::{module::ModuleRenderContext, module_id::ModuleVec},
options::{
file_name_template::FileNameRenderOptions, normalized_input_options::NormalizedInputOptions,
normalized_output_options::NormalizedOutputOptions,
file_name_template::FileNameRenderOptions, normalized_output_options::NormalizedOutputOptions,
},
};

use super::ChunkId;
use super::{ChunkId, ChunksVec};

#[derive(Debug, Default)]
pub struct Chunk {
pub is_entry: bool,
pub entry_module: Option<ModuleId>,
pub modules: Vec<ModuleId>,
pub name: Option<String>,
pub file_name: Option<String>,
Expand All @@ -28,10 +27,15 @@ pub struct Chunk {
}

impl Chunk {
pub fn new(name: Option<String>, is_entry: bool, bits: BitSet, modules: Vec<ModuleId>) -> Self {
pub fn new(
name: Option<String>,
entry_module: Option<ModuleId>,
bits: BitSet,
modules: Vec<ModuleId>,
) -> Self {
Self {
name,
is_entry,
entry_module,
bits,
modules,
..Default::default()
Expand Down Expand Up @@ -86,7 +90,8 @@ impl Chunk {
pub fn render(
&self,
graph: &Graph,
_input_options: &NormalizedInputOptions,
module_to_chunk: &IndexVec<ModuleId, Option<ChunkId>>,
chunks: &ChunksVec,
) -> anyhow::Result<String> {
use rayon::prelude::*;
let mut joiner = Joiner::with_options(JoinerOptions {
Expand All @@ -101,6 +106,8 @@ impl Chunk {
m.render(ModuleRenderContext {
canonical_names: &self.canonical_names,
symbols: &graph.symbols,
module_to_chunk,
chunks,
})
})
.collect::<Vec<_>>()
Expand Down
6 changes: 3 additions & 3 deletions crates/rolldown/src/bundler/graph/graph.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use rolldown_common::ModuleId;
use rustc_hash::FxHashSet;

use super::{linker::Linker, symbols::Symbols};
use crate::bundler::{
module::module_id::ModuleVec, module_loader::ModuleLoader,
options::normalized_input_options::NormalizedInputOptions,
};
use rolldown_common::ModuleId;
use rustc_hash::FxHashSet;

#[derive(Default, Debug)]
pub struct Graph {
Expand Down Expand Up @@ -56,6 +55,7 @@ impl Graph {
module
.import_records()
.iter()
.filter(|rec| rec.kind.is_static())
.filter_map(|rec| {
rec
.resolved_module
Expand Down
7 changes: 6 additions & 1 deletion crates/rolldown/src/bundler/module/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use rolldown_common::{ImportRecord, ImportRecordId, ModuleId, SymbolRef};
use rustc_hash::FxHashMap;
use string_wizard::MagicString;

use crate::bundler::graph::symbols::Symbols;
use crate::bundler::{
chunk::{ChunkId, ChunksVec},
graph::symbols::Symbols,
};

use super::{external_module::ExternalModule, NormalModule};

Expand Down Expand Up @@ -80,4 +83,6 @@ impl Module {
pub struct ModuleRenderContext<'a> {
pub canonical_names: &'a FxHashMap<SymbolRef, Atom>,
pub symbols: &'a Symbols,
pub module_to_chunk: &'a IndexVec<ModuleId, Option<ChunkId>>,
pub chunks: &'a ChunksVec,
}
4 changes: 3 additions & 1 deletion crates/rolldown/src/bundler/module/module_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use index_vec::IndexVec;
use oxc::{
semantic::{ReferenceId, ScopeTree, SymbolId},
span::Atom,
span::{Atom, Span},
};
use rolldown_common::{
ImportRecord, ImportRecordId, LocalOrReExport, ModuleId, NamedImport, ResourceId, StmtInfo,
Expand All @@ -23,6 +23,7 @@ pub struct ModuleBuilder {
pub named_exports: Option<FxHashMap<Atom, LocalOrReExport>>,
pub stmt_infos: Option<IndexVec<StmtInfoId, StmtInfo>>,
pub import_records: Option<IndexVec<ImportRecordId, ImportRecord>>,
pub dynamic_imports: Option<FxHashMap<Span, ImportRecordId>>,
pub star_exports: Option<Vec<ImportRecordId>>,
pub scope: Option<ScopeTree>,
pub default_export_symbol: Option<SymbolId>,
Expand All @@ -47,6 +48,7 @@ impl ModuleBuilder {
named_exports: self.named_exports.unwrap(),
stmt_infos: self.stmt_infos.unwrap(),
import_records: self.import_records.unwrap(),
dynamic_imports: self.dynamic_imports.unwrap(),
star_exports: self.star_exports.unwrap(),
resolved_exports: Default::default(),
resolved_star_exports: Default::default(),
Expand Down
7 changes: 6 additions & 1 deletion crates/rolldown/src/bundler/module/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use index_vec::IndexVec;
use oxc::{
ast::Visit,
semantic::{ReferenceId, ScopeTree, SymbolId},
span::Atom,
span::{Atom, Span},
};
use rolldown_common::{
ImportRecord, ImportRecordId, LocalOrReExport, ModuleId, NamedImport, ResolvedExport, ResourceId,
Expand Down Expand Up @@ -33,6 +33,7 @@ pub struct NormalModule {
pub named_exports: FxHashMap<Atom, LocalOrReExport>,
pub stmt_infos: IndexVec<StmtInfoId, StmtInfo>,
pub import_records: IndexVec<ImportRecordId, ImportRecord>,
pub dynamic_imports: FxHashMap<Span, ImportRecordId>,
// [[StarExportEntries]] in https://tc39.es/ecma262/#sec-source-text-module-records
pub star_exports: Vec<ImportRecordId>,
// resolved
Expand Down Expand Up @@ -79,6 +80,10 @@ impl NormalModule {
final_names: ctx.canonical_names,
default_export_symbol: self.default_export_symbol.map(|id| (self.id, id).into()),
source: &mut source,
dynamic_imports: &self.dynamic_imports,
import_records: &self.import_records,
chunks: ctx.chunks,
module_to_chunk: ctx.module_to_chunk,
});
renderer.visit_program(program);

Expand Down

0 comments on commit 6cd5c8e

Please sign in to comment.