Skip to content

Commit 4ce2514

Browse files
authoredJul 15, 2024··
perf(allocator): Drop scoped_tls (#9240)
**Description:** The performance is much better without it.
1 parent e5f925d commit 4ce2514

File tree

5 files changed

+13
-11
lines changed

5 files changed

+13
-11
lines changed
 

‎Cargo.lock

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

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Also, SWC tries to ensure that
4646
4747
for rust users.
4848

49-
MSRV of crates is currently `1.71`.
49+
MSRV of crates is currently `1.73`.
5050

5151
To update all SWC crates you use, you can run `curl https://raw.githubusercontent.com/swc-project/swc/main/scripts/update-all-swc-crates.sh | bash -s`. This script will update all dependencies to the latest version and run `cargo build` to ensure that everything works.
5252
Note that you need

‎clippy.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ ignore-interior-mutability = [
2222
"swc_atoms::JsWord",
2323
"swc_ecma_ast::Id",
2424
]
25-
msrv = "1.71"
25+
msrv = "1.73"
2626
type-complexity-threshold = 25000

‎crates/swc_allocator/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ bumpalo = { workspace = true, features = [
2121
] }
2222
ptr_meta = { workspace = true }
2323
rkyv = { workspace = true, optional = true }
24-
scoped-tls = { workspace = true }
2524
serde = { workspace = true, optional = true }
2625
serde_derive = { workspace = true, optional = true }
2726
triomphe = "0.1.13"

‎crates/swc_allocator/src/alloc.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use std::{alloc::Layout, mem::transmute, ptr::NonNull};
1+
use std::{alloc::Layout, cell::Cell, mem::transmute, ptr::NonNull};
22

33
use allocator_api2::alloc::Global;
4-
use scoped_tls::scoped_thread_local;
54

65
use crate::{FastAlloc, MemorySpace};
76

8-
scoped_thread_local!(pub(crate) static ALLOC: &'static SwcAllocator);
7+
thread_local! {
8+
static ALLOC: Cell<Option<&'static SwcAllocator>> = const { Cell::new(None) };
9+
}
910

1011
#[derive(Default)]
1112
pub struct SwcAllocator(MemorySpace);
@@ -22,15 +23,18 @@ impl SwcAllocator {
2223
transmute::<&'a SwcAllocator, &'static SwcAllocator>(self)
2324
};
2425

25-
ALLOC.set(&s, f)
26+
ALLOC.set(Some(s));
27+
let ret = f();
28+
ALLOC.set(None);
29+
ret
2630
}
2731
}
2832

2933
impl Default for FastAlloc {
3034
fn default() -> Self {
3135
Self {
32-
alloc: if ALLOC.is_set() {
33-
Some(ALLOC.with(|v| *v))
36+
alloc: if let Some(v) = ALLOC.get() {
37+
Some(v)
3438
} else {
3539
None
3640
},
@@ -87,7 +91,7 @@ unsafe impl allocator_api2::alloc::Allocator for FastAlloc {
8791
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
8892
if self.alloc.is_some() {
8993
debug_assert!(
90-
ALLOC.is_set(),
94+
ALLOC.get().is_some(),
9195
"Deallocating a pointer allocated with arena mode with a non-arena mode allocator"
9296
);
9397

0 commit comments

Comments
 (0)
Please sign in to comment.