Skip to content

Commit 3d52b74

Browse files
committed
GC
1 parent d5ae7dc commit 3d52b74

File tree

7 files changed

+70
-7
lines changed

7 files changed

+70
-7
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/napi/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ static ALLOC: dhat::Alloc = dhat::Alloc;
7070
#[cfg(not(target_arch = "wasm32"))]
7171
#[napi::module_init]
7272
fn init() {
73-
use std::panic::{set_hook, take_hook};
73+
use std::{
74+
cell::RefCell,
75+
panic::{set_hook, take_hook},
76+
time::{Duration, Instant},
77+
};
78+
79+
thread_local! {
80+
static LAST_SWC_ATOM_GC_TIME: RefCell<Option<Instant>> = const { RefCell::new(None) };
81+
}
7482

7583
use tokio::runtime::Builder;
7684
use turbo_tasks::panic_hooks::handle_panic;
@@ -87,6 +95,14 @@ fn init() {
8795
.on_thread_stop(|| {
8896
TurboMalloc::thread_stop();
8997
})
98+
.on_thread_park(|| {
99+
LAST_SWC_ATOM_GC_TIME.with_borrow_mut(|cell| {
100+
if cell.is_none_or(|t| t.elapsed() > Duration::from_secs(2)) {
101+
swc_core::ecma::atoms::hstr::global_atom_store_gc();
102+
*cell = Some(Instant::now());
103+
}
104+
});
105+
})
90106
.disable_lifo_slot()
91107
.build()
92108
.unwrap();

crates/next-api/benches/hmr.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
cell::RefCell,
23
fs::{create_dir_all, write},
34
mem::forget,
45
path::{Path, PathBuf},
@@ -154,11 +155,23 @@ fn load_next_config() -> RcStr {
154155
}
155156

156157
fn runtime() -> Runtime {
158+
thread_local! {
159+
static LAST_SWC_ATOM_GC_TIME: RefCell<Option<Instant>> = const { RefCell::new(None) };
160+
}
161+
157162
tokio::runtime::Builder::new_multi_thread()
158163
.enable_all()
159164
.on_thread_stop(|| {
160165
turbo_tasks_malloc::TurboMalloc::thread_stop();
161166
})
167+
.on_thread_park(|| {
168+
LAST_SWC_ATOM_GC_TIME.with_borrow_mut(|cell| {
169+
if cell.is_none_or(|t| t.elapsed() > Duration::from_secs(2)) {
170+
swc_core::ecma::atoms::hstr::global_atom_store_gc();
171+
*cell = Some(Instant::now());
172+
}
173+
});
174+
})
162175
.build()
163176
.context("Failed to build tokio runtime")
164177
.unwrap()

crates/next-build-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ next-api = { workspace = true }
1616
next-core = { workspace = true }
1717
num_cpus = "1.16.0"
1818
rand = { workspace = true, features = ["small_rng"] }
19+
swc_core = { workspace = true }
1920
serde_json = { workspace = true }
2021
tokio = { workspace = true, features = ["full"] }
2122
tokio-stream = "0.1.15"

crates/next-build-test/src/main.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use std::{convert::Infallible, str::FromStr, time::Instant};
1+
use std::{
2+
cell::RefCell,
3+
convert::Infallible,
4+
str::FromStr,
5+
time::{Duration, Instant},
6+
};
27

38
use next_api::project::{DefineEnv, ProjectOptions};
49
use next_build_test::{Strategy, main_inner};
@@ -70,12 +75,23 @@ fn main() {
7075
factor = 1;
7176
}
7277

78+
thread_local! {
79+
static LAST_SWC_ATOM_GC_TIME: RefCell<Option<Instant>> = const { RefCell::new(None) };
80+
}
7381
tokio::runtime::Builder::new_multi_thread()
7482
.enable_all()
7583
.on_thread_stop(|| {
7684
TurboMalloc::thread_stop();
7785
tracing::debug!("threads stopped");
7886
})
87+
.on_thread_park(|| {
88+
LAST_SWC_ATOM_GC_TIME.with_borrow_mut(|cell| {
89+
if cell.is_none_or(|t| t.elapsed() > Duration::from_secs(2)) {
90+
swc_core::ecma::atoms::hstr::global_atom_store_gc();
91+
*cell = Some(Instant::now());
92+
}
93+
});
94+
})
7995
.build()
8096
.unwrap()
8197
.block_on(async {

turbopack/crates/turbopack-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ futures = { workspace = true }
4545
owo-colors = { workspace = true }
4646
rustc-hash = { workspace = true }
4747
serde = { workspace = true }
48+
swc_core = { workspace = true }
4849
tokio = { workspace = true, features = ["full"] }
4950
tracing = { workspace = true }
5051
tracing-subscriber = { workspace = true, features = ["json"] }

turbopack/crates/turbopack-cli/src/main.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(future_join)]
22
#![feature(min_specialization)]
33

4-
use std::path::Path;
4+
use std::{cell::RefCell, path::Path, time::Instant};
55

66
use anyhow::{Context, Result};
77
use clap::Parser;
@@ -22,16 +22,30 @@ use turbopack_trace_utils::{
2222
static ALLOC: TurboMalloc = TurboMalloc;
2323

2424
fn main() {
25-
let args = Arguments::parse();
25+
thread_local! {
26+
static LAST_SWC_ATOM_GC_TIME: RefCell<Option<Instant>> = const { RefCell::new(None) };
27+
}
2628

2729
let mut rt = tokio::runtime::Builder::new_multi_thread();
28-
rt.enable_all().on_thread_stop(|| {
29-
TurboMalloc::thread_stop();
30-
});
30+
rt.enable_all()
31+
.on_thread_stop(|| {
32+
TurboMalloc::thread_stop();
33+
})
34+
.on_thread_park(|| {
35+
LAST_SWC_ATOM_GC_TIME.with_borrow_mut(|cell| {
36+
use std::time::Duration;
37+
38+
if cell.is_none_or(|t| t.elapsed() > Duration::from_secs(2)) {
39+
swc_core::ecma::atoms::hstr::global_atom_store_gc();
40+
*cell = Some(Instant::now());
41+
}
42+
});
43+
});
3144

3245
#[cfg(not(codspeed))]
3346
rt.disable_lifo_slot();
3447

48+
let args = Arguments::parse();
3549
rt.build().unwrap().block_on(main_inner(args)).unwrap();
3650
}
3751

0 commit comments

Comments
 (0)