Skip to content

Commit

Permalink
perf: bump string_wizard for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Nov 16, 2023
1 parent 6601e88 commit 079c922
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ serde_json = "1.0.108"
insta = "1.34.0"
testing_macros = "0.2.11"
scoped-tls = "1.0.1"
string_wizard = { version = "0.0.14" }
string_wizard = { version = "0.0.17" }
async-trait = "0.1.74"
futures = "0.3.29"
thiserror = "1.0.50"
Expand Down
3 changes: 2 additions & 1 deletion crates/rolldown/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use sugar_path::SugarPathBuf;

#[tokio::main]
async fn main() -> Result<()> {
let _guard = rolldown_tracing::try_init_tracing_with_chrome_layer();
let root = PathBuf::from(&std::env::var("CARGO_MANIFEST_DIR").unwrap());
let cwd = root.join("./examples").into_normalize();
let mut bundler = Bundler::new(InputOptions {
input: vec![InputItem { name: Some("basic".to_string()), import: "./index.js".to_string() }],
input: vec![InputItem { name: Some("basic".to_string()), import: "react-dom".to_string() }],
cwd,
..Default::default()
});
Expand Down
9 changes: 8 additions & 1 deletion crates/rolldown/src/bundler/chunk/render_chunk_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ impl Chunk {
if matches!(linking_info.wrap_kind, WrapKind::Cjs) {
match output_options.format {
OutputFormat::Esm => {
let wrap_ref_name = &self.canonical_names[&linking_info.wrap_ref.unwrap()];
let wrap_ref_name =
&self.canonical_names.get(&linking_info.wrap_ref.unwrap()).unwrap_or_else(|| {
panic!(
"Cannot find canonical name for wrap ref {:?} of {:?}",
linking_info.wrap_ref.unwrap(),
graph.modules[entry].resource_id()
)
});
return Some(MagicString::new(format!("export default {wrap_ref_name}();\n")));
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl Module {
}
}

#[tracing::instrument(skip_all)]
pub fn render(&self, ctx: ModuleRenderContext) -> Option<MagicString<'_>> {
match self {
Self::Normal(m) => m.render(ctx),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl<'task, T: FileSystem + Default + 'static> NormalModuleTask<'task, T> {
}
}

#[tracing::instrument(skip_all)]
pub async fn run(mut self) -> BatchedResult<()> {
let mut builder = NormalModuleBuilder::default();
tracing::trace!("process {:?}", self.path);
Expand Down Expand Up @@ -204,6 +205,7 @@ impl<'task, T: FileSystem + Default + 'static> NormalModuleTask<'task, T> {
Ok(info)
}

#[tracing::instrument(skip_all)]
async fn resolve_dependencies(
&mut self,
dependencies: &IndexVec<ImportRecordId, RawImportRecord>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl RuntimeNormalModuleTask {
Self { module_id: id, tx, warnings: Vec::default() }
}

#[tracing::instrument(skip_all)]
pub fn run(self) {
let mut builder = NormalModuleBuilder::default();

Expand Down
8 changes: 8 additions & 0 deletions crates/rolldown/src/bundler/renderer/impl_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ use crate::bundler::{module::Module, renderer::RenderControl};
use super::AstRenderer;

impl<'ast, 'r> Visit<'ast> for AstRenderer<'r> {
#[tracing::instrument]
fn visit_program(&mut self, program: &ast::Program<'ast>) {
for directive in &program.directives {
self.visit_directive(directive);
}
self.visit_statements(&program.body);
}

fn visit_binding_identifier(&mut self, ident: &oxc::ast::ast::BindingIdentifier) {
self.render_binding_identifier(ident);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rolldown/src/bundler/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ impl<'r> AstRenderer<'r> {
self.wrapped_esm_ctx.hoisted_functions.iter().for_each(|f| {
self.ctx.source.relocate(f.start, f.end, 0);
self.ctx.source.append_left(f.end, "\n");
indent_excludes.push([f.start, f.end]);
indent_excludes.push((f.start, f.end));
});
self.ctx.source.indent2(&self.indentor, indent_excludes);
self.ctx.source.indent2(&self.indentor, &indent_excludes);
if !self.wrapped_esm_ctx.hoisted_vars.is_empty() {
self
.ctx
Expand All @@ -146,7 +146,7 @@ impl<'r> AstRenderer<'r> {
RenderKind::Cjs => {
let wrap_ref_name = self.ctx.wrap_ref_name.unwrap();
let prettify_id = &self.ctx.module.pretty_path;
self.ctx.source.indent2(&self.indentor, Vec::default());
self.ctx.source.indent2(&self.indentor, &[]);
let commonjs_ref_name = self.ctx.canonical_name_for_runtime("__commonJS");
self.ctx.source.prepend(format!(
"var {wrap_ref_name} = {commonjs_ref_name}({{\n{}'{prettify_id}'(exports, module) {{\n",
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/stages/scan_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl<Fs: FileSystem + Default + 'static> ScanStage<Fs> {
}

/// Resolve `InputOptions.input`
#[tracing::instrument(skip_all)]
fn resolve_user_defined_entries(
&self,
) -> BatchedResult<Vec<(Option<String>, ResolvedRequestInfo)>> {
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/visitors/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ impl<'a> Scanner<'a> {
}

impl<'ast> Visit<'ast> for Scanner<'ast> {
#[tracing::instrument(skip_all)]
fn visit_program(&mut self, program: &oxc::ast::ast::Program<'ast>) {
for (idx, stmt) in program.body.iter().enumerate() {
self.current_stmt_info.stmt_idx = Some(idx);
Expand Down
4 changes: 2 additions & 2 deletions crates/rolldown_utils/src/magic_string_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use string_wizard::{IndentOptions, MagicString, UpdateOptions};

pub trait MagicStringExt {
fn overwrite(&mut self, start: u32, end: u32, content: String) -> &mut Self;
fn indent2(&mut self, indentor: &str, exclude: Vec<[u32; 2]>) -> &mut Self;
fn indent2(&mut self, indentor: &str, exclude: &[(u32, u32)]) -> &mut Self;
}
impl<'text> MagicStringExt for MagicString<'text> {
fn overwrite(&mut self, start: u32, end: u32, content: String) -> &mut Self {
self.update_with(start, end, content, UpdateOptions { overwrite: true, ..Default::default() })
}

fn indent2(&mut self, indentor: &str, exclude: Vec<[u32; 2]>) -> &mut Self {
fn indent2(&mut self, indentor: &str, exclude: &[(u32, u32)]) -> &mut Self {
self.indent_with(IndentOptions { indentor: Some(&indentor.repeat(2)), exclude })
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"test:update": "yarn workspaces foreach run test:update",
"prettier": "prettier . '!**/*.{js||ts|json|md|yml|yaml}' -w",
"prettier:ci": "prettier . '!**/*.{js||ts|json|md|yml|yaml}' -c",
"bench": "yarn workspace bench run bench",
"TODO(hyf0): #need to investigate following commands": "_",
"build:ci:release": "run-s build:packages:ci build:ts build:binding:release",
"build:ts": "tsc -b tsconfig.project.json"
Expand Down
1 change: 1 addition & 0 deletions packages/bench/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "bench",
"private": true,
"type": "module",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2985,9 +2985,9 @@ __metadata:
languageName: node
linkType: hard

"bench-f64382@workspace:packages/bench":
"bench@workspace:packages/bench":
version: 0.0.0-use.local
resolution: "bench-f64382@workspace:packages/bench"
resolution: "bench@workspace:packages/bench"
dependencies:
"@rolldown/node": "workspace:*"
esbuild: ^0.19.5
Expand Down

0 comments on commit 079c922

Please sign in to comment.