Skip to content

Commit

Permalink
perf(es): Do not create tokio runtime if not required (#8711)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8708.
  • Loading branch information
kdy1 authored Mar 8, 2024
1 parent 1f65271 commit 9a1f04f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 99 deletions.
5 changes: 3 additions & 2 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 bindings/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 bindings/binding_core_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,3 @@ swc_core = { version = "0.90.17", features = [
"base_concurrent",
] }
swc_malloc = "0.5.10"
tokio = { version = "1.36.0", features = ["rt", "rt-multi-thread"] }
165 changes: 76 additions & 89 deletions bindings/binding_core_node/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,45 +50,40 @@ impl Task for TransformTask {

let error_format = options.experimental.error_format.unwrap_or_default();

tokio::runtime::Runtime::new()
.unwrap()
.block_on(async move {
try_with(
self.c.cm.clone(),
!options.config.error.filename.into_bool(),
error_format,
|handler| {
self.c.run(|| match &self.input {
Input::Program(ref s) => {
let program: Program =
deserialize_json(s).expect("failed to deserialize Program");
// TODO: Source map
self.c.process_js(handler, program, &options)
}

Input::File(ref path) => {
let fm =
self.c.cm.load_file(path).context("failed to load file")?;
self.c.process_js_file(fm, handler, &options)
}

Input::Source { src } => {
let fm = self.c.cm.new_source_file(
if options.filename.is_empty() {
FileName::Anon
} else {
FileName::Real(options.filename.clone().into())
},
src.to_string(),
);

self.c.process_js_file(fm, handler, &options)
}
})
},
)
})
.convert_err()
try_with(
self.c.cm.clone(),
!options.config.error.filename.into_bool(),
error_format,
|handler| {
self.c.run(|| match &self.input {
Input::Program(ref s) => {
let program: Program =
deserialize_json(s).expect("failed to deserialize Program");
// TODO: Source map
self.c.process_js(handler, program, &options)
}

Input::File(ref path) => {
let fm = self.c.cm.load_file(path).context("failed to load file")?;
self.c.process_js_file(fm, handler, &options)
}

Input::Source { src } => {
let fm = self.c.cm.new_source_file(
if options.filename.is_empty() {
FileName::Anon
} else {
FileName::Real(options.filename.clone().into())
},
src.to_string(),
);

self.c.process_js_file(fm, handler, &options)
}
})
},
)
.convert_err()
}

fn resolve(&mut self, _env: Env, result: Self::Output) -> napi::Result<Self::JsValue> {
Expand Down Expand Up @@ -142,35 +137,31 @@ pub fn transform_sync(s: String, is_module: bool, opts: Buffer) -> napi::Result<

let error_format = options.experimental.error_format.unwrap_or_default();

tokio::runtime::Runtime::new()
.unwrap()
.block_on(async move {
try_with(
c.cm.clone(),
!options.config.error.filename.into_bool(),
error_format,
|handler| {
c.run(|| {
if is_module {
let program: Program = deserialize_json(s.as_str())
.context("failed to deserialize Program")?;
c.process_js(handler, program, &options)
try_with(
c.cm.clone(),
!options.config.error.filename.into_bool(),
error_format,
|handler| {
c.run(|| {
if is_module {
let program: Program =
deserialize_json(s.as_str()).context("failed to deserialize Program")?;
c.process_js(handler, program, &options)
} else {
let fm = c.cm.new_source_file(
if options.filename.is_empty() {
FileName::Anon
} else {
let fm = c.cm.new_source_file(
if options.filename.is_empty() {
FileName::Anon
} else {
FileName::Real(options.filename.clone().into())
},
s,
);
c.process_js_file(fm, handler, &options)
}
})
},
)
})
.convert_err()
FileName::Real(options.filename.clone().into())
},
s,
);
c.process_js_file(fm, handler, &options)
}
})
},
)
.convert_err()
}

#[napi]
Expand Down Expand Up @@ -212,26 +203,22 @@ pub fn transform_file_sync(

let error_format = options.experimental.error_format.unwrap_or_default();

tokio::runtime::Runtime::new()
.unwrap()
.block_on(async move {
try_with(
c.cm.clone(),
!options.config.error.filename.into_bool(),
error_format,
|handler| {
c.run(|| {
if is_module {
let program: Program = deserialize_json(s.as_str())
.context("failed to deserialize Program")?;
c.process_js(handler, program, &options)
} else {
let fm = c.cm.load_file(Path::new(&s)).expect("failed to load file");
c.process_js_file(fm, handler, &options)
}
})
},
)
})
.convert_err()
try_with(
c.cm.clone(),
!options.config.error.filename.into_bool(),
error_format,
|handler| {
c.run(|| {
if is_module {
let program: Program =
deserialize_json(s.as_str()).context("failed to deserialize Program")?;
c.process_js(handler, program, &options)
} else {
let fm = c.cm.load_file(Path::new(&s)).expect("failed to load file");
c.process_js_file(fm, handler, &options)
}
})
},
)
.convert_err()
}
5 changes: 5 additions & 0 deletions crates/swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ plugin = [
"swc_plugin_runner/ecma",
"swc_plugin_runner/rkyv-impl",
"swc_plugin_proxy/plugin-rt",
"tokio",
]
plugin_transform_schema_v1 = [
"swc_common/plugin_transform_schema_v1",
Expand Down Expand Up @@ -106,6 +107,10 @@ swc_plugin_proxy = { version = "0.41.5", path = "../swc_plugin_proxy", optional
swc_plugin_runner = { version = "0.106.10", path = "../swc_plugin_runner", optional = true, default-features = false }
swc_timer = { version = "0.21.20", path = "../swc_timer" }
swc_visit = { version = "0.5.10", path = "../swc_visit" }
tokio = { version = "1.36.0", optional = true, features = [
"rt",
"rt-multi-thread",
] }


[dependencies.napi-derive]
Expand Down
16 changes: 10 additions & 6 deletions crates/swc/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ impl RustPlugins {
return Ok(n);
}

self.apply_inner(n).with_context(|| {
format!(
"failed to invoke plugin on '{:?}'",
self.metadata_context.filename
)
})
tokio::runtime::Runtime::new()
.unwrap()
.block_on(async move {
self.apply_inner(n).with_context(|| {
format!(
"failed to invoke plugin on '{:?}'",
self.metadata_context.filename
)
})
})
}

#[tracing::instrument(level = "info", skip_all, name = "apply_plugins")]
Expand Down

0 comments on commit 9a1f04f

Please sign in to comment.