Skip to content

Commit 9a1f04f

Browse files
authoredMar 8, 2024··
perf(es): Do not create tokio runtime if not required (#8711)
**Related issue:** - Closes #8708.
1 parent 1f65271 commit 9a1f04f

File tree

6 files changed

+94
-99
lines changed

6 files changed

+94
-99
lines changed
 

‎Cargo.lock

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bindings/Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bindings/binding_core_node/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,3 @@ swc_core = { version = "0.90.17", features = [
6565
"base_concurrent",
6666
] }
6767
swc_malloc = "0.5.10"
68-
tokio = { version = "1.36.0", features = ["rt", "rt-multi-thread"] }

‎bindings/binding_core_node/src/transform.rs

+76-89
Original file line numberDiff line numberDiff line change
@@ -50,45 +50,40 @@ impl Task for TransformTask {
5050

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

53-
tokio::runtime::Runtime::new()
54-
.unwrap()
55-
.block_on(async move {
56-
try_with(
57-
self.c.cm.clone(),
58-
!options.config.error.filename.into_bool(),
59-
error_format,
60-
|handler| {
61-
self.c.run(|| match &self.input {
62-
Input::Program(ref s) => {
63-
let program: Program =
64-
deserialize_json(s).expect("failed to deserialize Program");
65-
// TODO: Source map
66-
self.c.process_js(handler, program, &options)
67-
}
68-
69-
Input::File(ref path) => {
70-
let fm =
71-
self.c.cm.load_file(path).context("failed to load file")?;
72-
self.c.process_js_file(fm, handler, &options)
73-
}
74-
75-
Input::Source { src } => {
76-
let fm = self.c.cm.new_source_file(
77-
if options.filename.is_empty() {
78-
FileName::Anon
79-
} else {
80-
FileName::Real(options.filename.clone().into())
81-
},
82-
src.to_string(),
83-
);
84-
85-
self.c.process_js_file(fm, handler, &options)
86-
}
87-
})
88-
},
89-
)
90-
})
91-
.convert_err()
53+
try_with(
54+
self.c.cm.clone(),
55+
!options.config.error.filename.into_bool(),
56+
error_format,
57+
|handler| {
58+
self.c.run(|| match &self.input {
59+
Input::Program(ref s) => {
60+
let program: Program =
61+
deserialize_json(s).expect("failed to deserialize Program");
62+
// TODO: Source map
63+
self.c.process_js(handler, program, &options)
64+
}
65+
66+
Input::File(ref path) => {
67+
let fm = self.c.cm.load_file(path).context("failed to load file")?;
68+
self.c.process_js_file(fm, handler, &options)
69+
}
70+
71+
Input::Source { src } => {
72+
let fm = self.c.cm.new_source_file(
73+
if options.filename.is_empty() {
74+
FileName::Anon
75+
} else {
76+
FileName::Real(options.filename.clone().into())
77+
},
78+
src.to_string(),
79+
);
80+
81+
self.c.process_js_file(fm, handler, &options)
82+
}
83+
})
84+
},
85+
)
86+
.convert_err()
9287
}
9388

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

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

145-
tokio::runtime::Runtime::new()
146-
.unwrap()
147-
.block_on(async move {
148-
try_with(
149-
c.cm.clone(),
150-
!options.config.error.filename.into_bool(),
151-
error_format,
152-
|handler| {
153-
c.run(|| {
154-
if is_module {
155-
let program: Program = deserialize_json(s.as_str())
156-
.context("failed to deserialize Program")?;
157-
c.process_js(handler, program, &options)
140+
try_with(
141+
c.cm.clone(),
142+
!options.config.error.filename.into_bool(),
143+
error_format,
144+
|handler| {
145+
c.run(|| {
146+
if is_module {
147+
let program: Program =
148+
deserialize_json(s.as_str()).context("failed to deserialize Program")?;
149+
c.process_js(handler, program, &options)
150+
} else {
151+
let fm = c.cm.new_source_file(
152+
if options.filename.is_empty() {
153+
FileName::Anon
158154
} else {
159-
let fm = c.cm.new_source_file(
160-
if options.filename.is_empty() {
161-
FileName::Anon
162-
} else {
163-
FileName::Real(options.filename.clone().into())
164-
},
165-
s,
166-
);
167-
c.process_js_file(fm, handler, &options)
168-
}
169-
})
170-
},
171-
)
172-
})
173-
.convert_err()
155+
FileName::Real(options.filename.clone().into())
156+
},
157+
s,
158+
);
159+
c.process_js_file(fm, handler, &options)
160+
}
161+
})
162+
},
163+
)
164+
.convert_err()
174165
}
175166

176167
#[napi]
@@ -212,26 +203,22 @@ pub fn transform_file_sync(
212203

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

215-
tokio::runtime::Runtime::new()
216-
.unwrap()
217-
.block_on(async move {
218-
try_with(
219-
c.cm.clone(),
220-
!options.config.error.filename.into_bool(),
221-
error_format,
222-
|handler| {
223-
c.run(|| {
224-
if is_module {
225-
let program: Program = deserialize_json(s.as_str())
226-
.context("failed to deserialize Program")?;
227-
c.process_js(handler, program, &options)
228-
} else {
229-
let fm = c.cm.load_file(Path::new(&s)).expect("failed to load file");
230-
c.process_js_file(fm, handler, &options)
231-
}
232-
})
233-
},
234-
)
235-
})
236-
.convert_err()
206+
try_with(
207+
c.cm.clone(),
208+
!options.config.error.filename.into_bool(),
209+
error_format,
210+
|handler| {
211+
c.run(|| {
212+
if is_module {
213+
let program: Program =
214+
deserialize_json(s.as_str()).context("failed to deserialize Program")?;
215+
c.process_js(handler, program, &options)
216+
} else {
217+
let fm = c.cm.load_file(Path::new(&s)).expect("failed to load file");
218+
c.process_js_file(fm, handler, &options)
219+
}
220+
})
221+
},
222+
)
223+
.convert_err()
237224
}

‎crates/swc/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ plugin = [
3232
"swc_plugin_runner/ecma",
3333
"swc_plugin_runner/rkyv-impl",
3434
"swc_plugin_proxy/plugin-rt",
35+
"tokio",
3536
]
3637
plugin_transform_schema_v1 = [
3738
"swc_common/plugin_transform_schema_v1",
@@ -106,6 +107,10 @@ swc_plugin_proxy = { version = "0.41.5", path = "../swc_plugin_proxy", optional
106107
swc_plugin_runner = { version = "0.106.10", path = "../swc_plugin_runner", optional = true, default-features = false }
107108
swc_timer = { version = "0.21.20", path = "../swc_timer" }
108109
swc_visit = { version = "0.5.10", path = "../swc_visit" }
110+
tokio = { version = "1.36.0", optional = true, features = [
111+
"rt",
112+
"rt-multi-thread",
113+
] }
109114

110115

111116
[dependencies.napi-derive]

‎crates/swc/src/plugin.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,16 @@ impl RustPlugins {
5555
return Ok(n);
5656
}
5757

58-
self.apply_inner(n).with_context(|| {
59-
format!(
60-
"failed to invoke plugin on '{:?}'",
61-
self.metadata_context.filename
62-
)
63-
})
58+
tokio::runtime::Runtime::new()
59+
.unwrap()
60+
.block_on(async move {
61+
self.apply_inner(n).with_context(|| {
62+
format!(
63+
"failed to invoke plugin on '{:?}'",
64+
self.metadata_context.filename
65+
)
66+
})
67+
})
6468
}
6569

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

0 commit comments

Comments
 (0)
Please sign in to comment.