Skip to content

Commit e60dc50

Browse files
Brooooooklynhyf0
authored andcommitted
chore(rolldown): use bounded channel to avoid dynamic memory allocation
1 parent 5106eda commit e60dc50

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

crates/rolldown/src/module_loader/module_loader.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl IntermediateNormalModules {
4444
pub struct ModuleLoader {
4545
input_options: SharedOptions,
4646
shared_context: Arc<TaskContext>,
47-
rx: tokio::sync::mpsc::UnboundedReceiver<Msg>,
47+
rx: tokio::sync::mpsc::Receiver<Msg>,
4848
visited: FxHashMap<Arc<str>, ModuleId>,
4949
runtime_id: NormalModuleId,
5050
remaining: u32,
@@ -72,7 +72,9 @@ impl ModuleLoader {
7272
fs: OsFileSystem,
7373
resolver: SharedResolver,
7474
) -> Self {
75-
let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<Msg>();
75+
// 1024 should be enough for most cases
76+
// over 1024 pending tasks are insane
77+
let (tx, rx) = tokio::sync::mpsc::channel::<Msg>(1024);
7678

7779
let tx_to_runtime_module = tx.clone();
7880

crates/rolldown/src/module_loader/normal_module_task.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ impl NormalModuleTask {
4646
match self.run_inner().await {
4747
Ok(()) => {
4848
if !self.errors.is_empty() {
49-
self.ctx.tx.send(Msg::BuildErrors(self.errors)).expect("Send should not fail");
49+
self.ctx.tx.send(Msg::BuildErrors(self.errors)).await.expect("Send should not fail");
5050
}
5151
}
5252
Err(err) => {
53-
self.ctx.tx.send(Msg::Panics(err)).expect("Send should not fail");
53+
self.ctx.tx.send(Msg::Panics(err)).await.expect("Send should not fail");
5454
}
5555
}
5656
}
@@ -124,6 +124,7 @@ impl NormalModuleTask {
124124
raw_import_records: import_records,
125125
ast,
126126
}))
127+
.await
127128
.expect("Send should not fail");
128129
tracing::trace!("end process {:?}", self.resolved_path);
129130
Ok(())

crates/rolldown/src/module_loader/runtime_normal_module_task.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
types::{ast_symbols::AstSymbols, normal_module_builder::NormalModuleBuilder},
1616
};
1717
pub struct RuntimeNormalModuleTask {
18-
tx: tokio::sync::mpsc::UnboundedSender<Msg>,
18+
tx: tokio::sync::mpsc::Sender<Msg>,
1919
module_id: NormalModuleId,
2020
warnings: Vec<BuildError>,
2121
}
@@ -29,7 +29,7 @@ pub struct RuntimeNormalModuleTaskResult {
2929
}
3030

3131
impl RuntimeNormalModuleTask {
32-
pub fn new(id: NormalModuleId, tx: tokio::sync::mpsc::UnboundedSender<Msg>) -> Self {
32+
pub fn new(id: NormalModuleId, tx: tokio::sync::mpsc::Sender<Msg>) -> Self {
3333
Self { module_id: id, tx, warnings: Vec::default() }
3434
}
3535

@@ -77,13 +77,15 @@ impl RuntimeNormalModuleTask {
7777
builder.is_user_defined_entry = Some(false);
7878
builder.is_virtual = true;
7979

80-
if let Err(_err) = self.tx.send(Msg::RuntimeNormalModuleDone(RuntimeNormalModuleTaskResult {
81-
warnings: self.warnings,
82-
ast_symbol: symbol,
83-
builder,
84-
runtime,
85-
ast,
86-
})) {
80+
if let Err(_err) =
81+
self.tx.try_send(Msg::RuntimeNormalModuleDone(RuntimeNormalModuleTaskResult {
82+
warnings: self.warnings,
83+
ast_symbol: symbol,
84+
builder,
85+
runtime,
86+
ast,
87+
}))
88+
{
8789
// hyf0: If main thread is dead, we should handle errors of main thread. So we just ignore the error here.
8890
};
8991
}

crates/rolldown/src/module_loader/task_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::Msg;
88
/// Used to store common data shared between all tasks.
99
pub struct TaskContext {
1010
pub input_options: SharedOptions,
11-
pub tx: tokio::sync::mpsc::UnboundedSender<Msg>,
11+
pub tx: tokio::sync::mpsc::Sender<Msg>,
1212
pub resolver: SharedResolver,
1313
pub fs: OsFileSystem,
1414
pub plugin_driver: SharedPluginDriver,

0 commit comments

Comments
 (0)