From c4c497e47d4dd022292d8aec3bdd2fa6e2a43beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4/Donny?= Date: Fri, 12 Jul 2024 21:12:55 +0900 Subject: [PATCH 01/65] boxed --- crates/swc_allocator/src/boxed.rs | 6 ++++++ crates/swc_allocator/src/lib.rs | 2 ++ 2 files changed, 8 insertions(+) create mode 100644 crates/swc_allocator/src/boxed.rs diff --git a/crates/swc_allocator/src/boxed.rs b/crates/swc_allocator/src/boxed.rs new file mode 100644 index 000000000000..eabc2fddab4d --- /dev/null +++ b/crates/swc_allocator/src/boxed.rs @@ -0,0 +1,6 @@ +use std::{marker::PhantomData, ptr::NonNull}; + +pub struct Box { + data: NonNull, + marker: PhantomData<&mut T>, +} diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 5c6a3917885a..77da9017bedc 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -6,6 +6,8 @@ use std::ops::{Deref, DerefMut}; use bumpalo::Bump; +pub mod boxed; + #[derive(Default)] pub struct Allocator { alloc: Bump, From e88decf107b5efd3edb7c979f087a4ec8c7c30ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4/Donny?= Date: Fri, 12 Jul 2024 21:14:52 +0900 Subject: [PATCH 02/65] doc --- crates/swc_allocator/src/boxed.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/swc_allocator/src/boxed.rs b/crates/swc_allocator/src/boxed.rs index eabc2fddab4d..5ecad23c16c5 100644 --- a/crates/swc_allocator/src/boxed.rs +++ b/crates/swc_allocator/src/boxed.rs @@ -1,5 +1,7 @@ use std::{marker::PhantomData, ptr::NonNull}; +/// A special `Box` which has size of [`std::boxed::Box`] but **may** be +/// allocated with a custom allocator. pub struct Box { data: NonNull, marker: PhantomData<&mut T>, From 3a0d765975e3c12931236e801b1eec25a7818e1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4/Donny?= Date: Fri, 12 Jul 2024 21:16:28 +0900 Subject: [PATCH 03/65] Dep --- crates/swc_allocator/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/swc_allocator/Cargo.toml b/crates/swc_allocator/Cargo.toml index ca32f62fd4c1..81d3ffebd2e9 100644 --- a/crates/swc_allocator/Cargo.toml +++ b/crates/swc_allocator/Cargo.toml @@ -9,4 +9,5 @@ repository = { workspace = true } version = "0.1.1" [dependencies] -bumpalo = { workspace = true, features = ["boxed", "collections"] } +bumpalo = { workspace = true, features = ["boxed", "collections"] } +scoped-tls = { workspace = true } From f25366120c5a6f3f2a10cc08c90be7d44efd9cca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4/Donny?= Date: Fri, 12 Jul 2024 21:16:34 +0900 Subject: [PATCH 04/65] cargo lockfiel --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index c85cc9408857..5a7c43daf4c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3689,6 +3689,7 @@ name = "swc_allocator" version = "0.1.1" dependencies = [ "bumpalo", + "scoped-tls", ] [[package]] From 0ee78935d183da57d7cd4bf1974b0ed34582a819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4/Donny?= Date: Fri, 12 Jul 2024 21:18:12 +0900 Subject: [PATCH 05/65] alloc::ALLOC --- crates/swc_allocator/src/alloc.rs | 5 +++++ crates/swc_allocator/src/lib.rs | 1 + 2 files changed, 6 insertions(+) create mode 100644 crates/swc_allocator/src/alloc.rs diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs new file mode 100644 index 000000000000..1278934964a9 --- /dev/null +++ b/crates/swc_allocator/src/alloc.rs @@ -0,0 +1,5 @@ +use scoped_tls::scoped_thread_local; + +use crate::Allocator; + +scoped_thread_local!(pub(crate) static ALLOC: Allocator); diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 77da9017bedc..7379260835b3 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -6,6 +6,7 @@ use std::ops::{Deref, DerefMut}; use bumpalo::Bump; +mod alloc; pub mod boxed; #[derive(Default)] From 580d0b15b5f5ace81438052018ed484397ee45d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4/Donny?= Date: Fri, 12 Jul 2024 21:20:08 +0900 Subject: [PATCH 06/65] doc --- crates/swc_allocator/src/boxed.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/swc_allocator/src/boxed.rs b/crates/swc_allocator/src/boxed.rs index 5ecad23c16c5..4a13c5673e5f 100644 --- a/crates/swc_allocator/src/boxed.rs +++ b/crates/swc_allocator/src/boxed.rs @@ -1,8 +1,12 @@ -use std::{marker::PhantomData, ptr::NonNull}; +use std::ptr::NonNull; /// A special `Box` which has size of [`std::boxed::Box`] but **may** be /// allocated with a custom allocator. +/// +/// +/// # Representation +/// +/// The last bit is 1 if the box is allocated with a custom allocator. pub struct Box { data: NonNull, - marker: PhantomData<&mut T>, } From 663726df65563194425d42ed663d3eddcc3b02ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 04:49:08 +0900 Subject: [PATCH 07/65] Dep --- Cargo.toml | 1 + crates/swc_allocator/Cargo.toml | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 66a445736df2..1804e63f95b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -137,6 +137,7 @@ resolver = "2" wasm-bindgen-futures = "0.4.41" wasmer = { version = "4.2.5", default-features = false } wasmer-wasix = { version = "0.18.0", default-features = false } +allocator-api2 = "0.2.18" [profile.release] # lto = true diff --git a/crates/swc_allocator/Cargo.toml b/crates/swc_allocator/Cargo.toml index 81d3ffebd2e9..ebf970e16aeb 100644 --- a/crates/swc_allocator/Cargo.toml +++ b/crates/swc_allocator/Cargo.toml @@ -9,5 +9,10 @@ repository = { workspace = true } version = "0.1.1" [dependencies] -bumpalo = { workspace = true, features = ["boxed", "collections"] } +allocator-api2 = { workspace = true } +bumpalo = { workspace = true, features = [ + "allocator-api2", + "boxed", + "collections", +] } scoped-tls = { workspace = true } From 4252d5eabfea35fd30dd74415c5adee230c1f953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 04:49:14 +0900 Subject: [PATCH 08/65] cargo lockfile --- Cargo.lock | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 5a7c43daf4c5..08bf78ff4dd2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,6 +62,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" + [[package]] name = "android-tzdata" version = "0.1.1" @@ -406,6 +412,9 @@ name = "bumpalo" version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +dependencies = [ + "allocator-api2", +] [[package]] name = "bytecheck" @@ -3688,6 +3697,7 @@ dependencies = [ name = "swc_allocator" version = "0.1.1" dependencies = [ + "allocator-api2", "bumpalo", "scoped-tls", ] From 6567db1f24342ada0b60da42d99701eccc64b366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 04:49:57 +0900 Subject: [PATCH 09/65] SwcAlloc --- crates/swc_allocator/src/alloc.rs | 6 +----- crates/swc_allocator/src/boxed.rs | 6 ++---- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 1278934964a9..9bfbd93f279f 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -1,5 +1 @@ -use scoped_tls::scoped_thread_local; - -use crate::Allocator; - -scoped_thread_local!(pub(crate) static ALLOC: Allocator); +pub(crate) struct SwcAlloc; diff --git a/crates/swc_allocator/src/boxed.rs b/crates/swc_allocator/src/boxed.rs index 4a13c5673e5f..914fc7e5ffe2 100644 --- a/crates/swc_allocator/src/boxed.rs +++ b/crates/swc_allocator/src/boxed.rs @@ -1,4 +1,4 @@ -use std::ptr::NonNull; +use crate::alloc::SwcAlloc; /// A special `Box` which has size of [`std::boxed::Box`] but **may** be /// allocated with a custom allocator. @@ -7,6 +7,4 @@ use std::ptr::NonNull; /// # Representation /// /// The last bit is 1 if the box is allocated with a custom allocator. -pub struct Box { - data: NonNull, -} +pub struct Box(allocator_api2::boxed::Box); From e4f31409f730bd13c6384d2d0a0a04254d0f9ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 04:50:57 +0900 Subject: [PATCH 10/65] traits --- crates/swc_allocator/src/alloc.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 9bfbd93f279f..6229498da987 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -1 +1,9 @@ +use scoped_tls::scoped_thread_local; + +use crate::Allocator; + +scoped_thread_local!(pub(crate) static ALLOC: Allocator); + pub(crate) struct SwcAlloc; + +impl allocator_api2::alloc::Allocator for SwcAlloc {} From 565012e1aa44e765d9124dc58d3fa9fde668f2fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 04:54:17 +0900 Subject: [PATCH 11/65] `with_allocator` --- crates/swc_allocator/src/alloc.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 6229498da987..c40a0f23c81d 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -4,6 +4,17 @@ use crate::Allocator; scoped_thread_local!(pub(crate) static ALLOC: Allocator); +fn with_allocator(f: impl FnOnce(&dyn allocator_api2::alloc::Allocator)) { + if ALLOC.is_set() { + ALLOC.with(|a| { + // + f(&&**a as &dyn allocator_api2::alloc::Allocator) + }); + } else { + f(&allocator_api2::alloc::Global); + } +} + pub(crate) struct SwcAlloc; impl allocator_api2::alloc::Allocator for SwcAlloc {} From 8de808865980046676c4074d3963bdcccc59d2d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 05:00:23 +0900 Subject: [PATCH 12/65] SwcAlloc --- crates/swc_allocator/src/alloc.rs | 62 ++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index c40a0f23c81d..ea9eed5e7c5f 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -1,20 +1,72 @@ +use std::{alloc::Layout, ptr::NonNull}; + use scoped_tls::scoped_thread_local; use crate::Allocator; scoped_thread_local!(pub(crate) static ALLOC: Allocator); -fn with_allocator(f: impl FnOnce(&dyn allocator_api2::alloc::Allocator)) { +/// `true` is passed to `f` if the box is allocated with a custom allocator. +fn with_allocator(f: impl FnOnce(&dyn allocator_api2::alloc::Allocator, bool) -> T) -> T { if ALLOC.is_set() { ALLOC.with(|a| { // - f(&&**a as &dyn allocator_api2::alloc::Allocator) - }); + f(&&**a as &dyn allocator_api2::alloc::Allocator, true) + }) } else { - f(&allocator_api2::alloc::Global); + f(&allocator_api2::alloc::Global, false) } } pub(crate) struct SwcAlloc; -impl allocator_api2::alloc::Allocator for SwcAlloc {} +unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { + fn allocate(&self, layout: Layout) -> Result, allocator_api2::alloc::AllocError> { + with_allocator(|a, _custom| a.allocate(layout)) + } + + fn allocate_zeroed( + &self, + layout: Layout, + ) -> Result, allocator_api2::alloc::AllocError> { + with_allocator(|a, _custom| a.allocate_zeroed(layout)) + } + + unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { + with_allocator(|a, _custom| a.deallocate(ptr, layout)) + } + + unsafe fn grow( + &self, + ptr: NonNull, + old_layout: Layout, + new_layout: Layout, + ) -> Result, allocator_api2::alloc::AllocError> { + with_allocator(|a, _custom| a.grow(ptr, old_layout, new_layout)) + } + + unsafe fn grow_zeroed( + &self, + ptr: NonNull, + old_layout: Layout, + new_layout: Layout, + ) -> Result, allocator_api2::alloc::AllocError> { + with_allocator(|a, _custom| a.grow_zeroed(ptr, old_layout, new_layout)) + } + + unsafe fn shrink( + &self, + ptr: NonNull, + old_layout: Layout, + new_layout: Layout, + ) -> Result, allocator_api2::alloc::AllocError> { + with_allocator(|a, _custom| a.shrink(ptr, old_layout, new_layout)) + } + + fn by_ref(&self) -> &Self + where + Self: Sized, + { + self + } +} From 074b4708a156483421cb5e1966db178af8296905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 05:05:35 +0900 Subject: [PATCH 13/65] `SwcAlloc` --- crates/swc_allocator/src/alloc.rs | 38 +++++++++++++++++-------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index ea9eed5e7c5f..5054f9e2b9f2 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -6,34 +6,38 @@ use crate::Allocator; scoped_thread_local!(pub(crate) static ALLOC: Allocator); -/// `true` is passed to `f` if the box is allocated with a custom allocator. -fn with_allocator(f: impl FnOnce(&dyn allocator_api2::alloc::Allocator, bool) -> T) -> T { - if ALLOC.is_set() { - ALLOC.with(|a| { - // - f(&&**a as &dyn allocator_api2::alloc::Allocator, true) - }) - } else { - f(&allocator_api2::alloc::Global, false) - } +pub(crate) struct SwcAlloc { + pub is_custom: bool, } -pub(crate) struct SwcAlloc; +impl SwcAlloc { + /// `true` is passed to `f` if the box is allocated with a custom allocator. + fn with_allocator(&self, f: impl FnOnce(&dyn allocator_api2::alloc::Allocator) -> T) -> T { + if self.is_custom { + ALLOC.with(|a| { + // + f(&&**a as &dyn allocator_api2::alloc::Allocator) + }) + } else { + f(&allocator_api2::alloc::Global) + } + } +} unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { fn allocate(&self, layout: Layout) -> Result, allocator_api2::alloc::AllocError> { - with_allocator(|a, _custom| a.allocate(layout)) + self.with_allocator(|a| a.allocate(layout)) } fn allocate_zeroed( &self, layout: Layout, ) -> Result, allocator_api2::alloc::AllocError> { - with_allocator(|a, _custom| a.allocate_zeroed(layout)) + self.with_allocator(|a| a.allocate_zeroed(layout)) } unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { - with_allocator(|a, _custom| a.deallocate(ptr, layout)) + self.with_allocator(|a| a.deallocate(ptr, layout)) } unsafe fn grow( @@ -42,7 +46,7 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { old_layout: Layout, new_layout: Layout, ) -> Result, allocator_api2::alloc::AllocError> { - with_allocator(|a, _custom| a.grow(ptr, old_layout, new_layout)) + self.with_allocator(|a| a.grow(ptr, old_layout, new_layout)) } unsafe fn grow_zeroed( @@ -51,7 +55,7 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { old_layout: Layout, new_layout: Layout, ) -> Result, allocator_api2::alloc::AllocError> { - with_allocator(|a, _custom| a.grow_zeroed(ptr, old_layout, new_layout)) + self.with_allocator(|a| a.grow_zeroed(ptr, old_layout, new_layout)) } unsafe fn shrink( @@ -60,7 +64,7 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { old_layout: Layout, new_layout: Layout, ) -> Result, allocator_api2::alloc::AllocError> { - with_allocator(|a, _custom| a.shrink(ptr, old_layout, new_layout)) + self.with_allocator(|a| a.shrink(ptr, old_layout, new_layout)) } fn by_ref(&self) -> &Self From b22e6b5a3d61bc87c5dd768d3b18c7365ec9fef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 05:06:54 +0900 Subject: [PATCH 14/65] WIP --- crates/swc_allocator/src/boxed.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/swc_allocator/src/boxed.rs b/crates/swc_allocator/src/boxed.rs index 914fc7e5ffe2..abb40cf8d406 100644 --- a/crates/swc_allocator/src/boxed.rs +++ b/crates/swc_allocator/src/boxed.rs @@ -7,4 +7,5 @@ use crate::alloc::SwcAlloc; /// # Representation /// /// The last bit is 1 if the box is allocated with a custom allocator. +#[repr(transparent)] pub struct Box(allocator_api2::boxed::Box); From 2574bec414d432d3ff331675dcdfee50bfcd0208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 05:09:03 +0900 Subject: [PATCH 15/65] Box --- crates/swc_allocator/src/boxed.rs | 31 ++++++++++++++++++++++++++++++- crates/swc_allocator/src/lib.rs | 25 ------------------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/crates/swc_allocator/src/boxed.rs b/crates/swc_allocator/src/boxed.rs index abb40cf8d406..0b6fb8407808 100644 --- a/crates/swc_allocator/src/boxed.rs +++ b/crates/swc_allocator/src/boxed.rs @@ -1,4 +1,6 @@ -use crate::alloc::SwcAlloc; +use std::ops::{Deref, DerefMut}; + +use crate::alloc::{SwcAlloc, ALLOC}; /// A special `Box` which has size of [`std::boxed::Box`] but **may** be /// allocated with a custom allocator. @@ -8,4 +10,31 @@ use crate::alloc::SwcAlloc; /// /// The last bit is 1 if the box is allocated with a custom allocator. #[repr(transparent)] +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Box(allocator_api2::boxed::Box); + +impl Box { + #[inline(always)] + pub fn new(value: T) -> Self { + let is_custom = ALLOC.is_set(); + + Self(allocator_api2::boxed::Box::new_in( + value, + SwcAlloc { is_custom }, + )) + } +} + +impl Deref for Box { + type Target = T; + + fn deref(&self) -> &T { + &self.0 + } +} + +impl DerefMut for Box { + fn deref_mut(&mut self) -> &mut T { + &mut self.0 + } +} diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 7379260835b3..dadd1ba90168 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -34,31 +34,6 @@ impl DerefMut for Allocator { } } -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[repr(transparent)] -pub struct Box<'alloc, T>(bumpalo::boxed::Box<'alloc, T>); - -impl<'alloc, T> Box<'alloc, T> { - #[inline(always)] - pub fn new(alloc: &'alloc Allocator, value: T) -> Self { - Self(bumpalo::boxed::Box::new_in(value, alloc)) - } -} - -impl<'alloc, T> Deref for Box<'alloc, T> { - type Target = T; - - fn deref(&self) -> &T { - &self.0 - } -} - -impl<'alloc, T> DerefMut for Box<'alloc, T> { - fn deref_mut(&mut self) -> &mut T { - &mut self.0 - } -} - #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] pub struct Vec<'alloc, T>(bumpalo::collections::Vec<'alloc, T>); From c4e08c1f1e1ba7099d65375c3ab8c5e7c7087352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:05:26 +0900 Subject: [PATCH 16/65] more traits --- crates/swc_allocator/src/alloc.rs | 1 + crates/swc_allocator/src/boxed.rs | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 5054f9e2b9f2..4cf8f569a025 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -6,6 +6,7 @@ use crate::Allocator; scoped_thread_local!(pub(crate) static ALLOC: Allocator); +#[derive(Clone, Copy)] pub(crate) struct SwcAlloc { pub is_custom: bool, } diff --git a/crates/swc_allocator/src/boxed.rs b/crates/swc_allocator/src/boxed.rs index 0b6fb8407808..9502c6c4b38e 100644 --- a/crates/swc_allocator/src/boxed.rs +++ b/crates/swc_allocator/src/boxed.rs @@ -10,7 +10,7 @@ use crate::alloc::{SwcAlloc, ALLOC}; /// /// The last bit is 1 if the box is allocated with a custom allocator. #[repr(transparent)] -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Box(allocator_api2::boxed::Box); impl Box { @@ -23,6 +23,10 @@ impl Box { SwcAlloc { is_custom }, )) } + + pub fn unbox(self) -> T { + allocator_api2::boxed::Box::into_inner(self.0) + } } impl Deref for Box { From 6ac00236b7011c6c8a941700eb86ff890544efd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:07:39 +0900 Subject: [PATCH 17/65] scope --- crates/swc_allocator/src/lib.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index dadd1ba90168..d805ae601e04 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -2,6 +2,7 @@ //! //! API designed after [`oxc_allocator`](https://github.com/oxc-project/oxc/tree/725571aad193ec6ba779c820baeb4a7774533ed7/crates/oxc_allocator/src). +use alloc::ALLOC; use std::ops::{Deref, DerefMut}; use bumpalo::Bump; @@ -14,6 +15,17 @@ pub struct Allocator { alloc: Bump, } +impl Allocator { + /// Invokes `f` in a scope where the allocations are done in this allocator. + #[inline(always)] + pub fn scope(&self, f: F) -> R + where + F: FnOnce() -> R, + { + ALLOC.set(self, f) + } +} + impl From for Allocator { fn from(alloc: Bump) -> Self { Self { alloc } From b53d2d6a391e2df6a7a4acbfbea5957a91260578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:09:21 +0900 Subject: [PATCH 18/65] Dep --- crates/swc_ecma_ast/Cargo.toml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/swc_ecma_ast/Cargo.toml b/crates/swc_ecma_ast/Cargo.toml index 2a871818c148..31a9f37bad52 100644 --- a/crates/swc_ecma_ast/Cargo.toml +++ b/crates/swc_ecma_ast/Cargo.toml @@ -42,10 +42,12 @@ rkyv = { workspace = true, features = [ ], optional = true } scoped-tls = { workspace = true } serde = { workspace = true, features = ["derive"], optional = true } -string_enum = { version = "0.4.4", path = "../string_enum" } -swc_atoms = { version = "0.6.5", path = "../swc_atoms" } -swc_common = { version = "0.35.0", path = "../swc_common" } unicode-id-start = { workspace = true } +string_enum = { version = "0.4.4", path = "../string_enum" } +swc_allocator = { version = "0.1.1", path = "../swc_allocator" } +swc_atoms = { version = "0.6.5", path = "../swc_atoms" } +swc_common = { version = "0.35.0", path = "../swc_common" } + [dev-dependencies] serde_json = { workspace = true } From 5b0c3ea46066962dab0685e30f14a2e46fa78a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:09:25 +0900 Subject: [PATCH 19/65] cargo lockfile --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index 08bf78ff4dd2..fecc38b913c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4134,6 +4134,7 @@ dependencies = [ "serde", "serde_json", "string_enum", + "swc_allocator", "swc_atoms", "swc_common", "unicode-id-start", From 86af131cb859942e45d91fa1391e7f95e212730d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:10:35 +0900 Subject: [PATCH 20/65] AST --- crates/swc_ecma_ast/src/class.rs | 1 + crates/swc_ecma_ast/src/decl.rs | 1 + crates/swc_ecma_ast/src/expr.rs | 1 + crates/swc_ecma_ast/src/function.rs | 1 + crates/swc_ecma_ast/src/ident.rs | 1 + crates/swc_ecma_ast/src/jsx.rs | 1 + crates/swc_ecma_ast/src/lit.rs | 1 + crates/swc_ecma_ast/src/module.rs | 1 + crates/swc_ecma_ast/src/module_decl.rs | 1 + crates/swc_ecma_ast/src/operators.rs | 1 + crates/swc_ecma_ast/src/prop.rs | 1 + crates/swc_ecma_ast/src/source_map.rs | 1 + crates/swc_ecma_ast/src/stmt.rs | 1 + 13 files changed, 13 insertions(+) diff --git a/crates/swc_ecma_ast/src/class.rs b/crates/swc_ecma_ast/src/class.rs index 4574c0edb5bb..86f013fdc13e 100644 --- a/crates/swc_ecma_ast/src/class.rs +++ b/crates/swc_ecma_ast/src/class.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP}; use crate::{ diff --git a/crates/swc_ecma_ast/src/decl.rs b/crates/swc_ecma_ast/src/decl.rs index af6d89e7ab84..639fbc41035b 100644 --- a/crates/swc_ecma_ast/src/decl.rs +++ b/crates/swc_ecma_ast/src/decl.rs @@ -1,5 +1,6 @@ use is_macro::Is; use string_enum::StringEnum; +use swc_allocator::{boxed::Box, Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP}; use crate::{ diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index 489daa80759a..7a6a6dcfc980 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -3,6 +3,7 @@ use std::{borrow::Cow, mem::transmute}; use is_macro::Is; use string_enum::StringEnum; +use swc_allocator::{boxed::Box, Vec}; use swc_atoms::Atom; use swc_common::{ ast_node, util::take::Take, BytePos, EqIgnoreSpan, Span, Spanned, SyntaxContext, DUMMY_SP, diff --git a/crates/swc_ecma_ast/src/function.rs b/crates/swc_ecma_ast/src/function.rs index 1d34eeca9772..b6dedc6505f1 100644 --- a/crates/swc_ecma_ast/src/function.rs +++ b/crates/swc_ecma_ast/src/function.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP}; use crate::{ diff --git a/crates/swc_ecma_ast/src/ident.rs b/crates/swc_ecma_ast/src/ident.rs index fa67207b645a..54b4e1ca29a6 100644 --- a/crates/swc_ecma_ast/src/ident.rs +++ b/crates/swc_ecma_ast/src/ident.rs @@ -5,6 +5,7 @@ use std::{ }; use phf::phf_set; +use swc_allocator::{boxed::Box, Vec}; use swc_atoms::{js_word, Atom}; use swc_common::{ ast_node, util::take::Take, BytePos, EqIgnoreSpan, Mark, Span, Spanned, SyntaxContext, DUMMY_SP, diff --git a/crates/swc_ecma_ast/src/jsx.rs b/crates/swc_ecma_ast/src/jsx.rs index 41055f03b864..9a59c9457b4a 100644 --- a/crates/swc_ecma_ast/src/jsx.rs +++ b/crates/swc_ecma_ast/src/jsx.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, Vec}; use swc_atoms::Atom; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/lit.rs b/crates/swc_ecma_ast/src/lit.rs index bb7555c59a4b..1ff1edb12e7c 100644 --- a/crates/swc_ecma_ast/src/lit.rs +++ b/crates/swc_ecma_ast/src/lit.rs @@ -5,6 +5,7 @@ use std::{ }; use num_bigint::BigInt as BigIntValue; +use swc_allocator::{boxed::Box, Vec}; use swc_atoms::{js_word, Atom}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/module.rs b/crates/swc_ecma_ast/src/module.rs index fe9456d323e1..54aa541c2f58 100644 --- a/crates/swc_ecma_ast/src/module.rs +++ b/crates/swc_ecma_ast/src/module.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, Vec}; use swc_atoms::Atom; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/module_decl.rs b/crates/swc_ecma_ast/src/module_decl.rs index df23cd837afd..be5b9b561e40 100644 --- a/crates/swc_ecma_ast/src/module_decl.rs +++ b/crates/swc_ecma_ast/src/module_decl.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, Vec}; use swc_atoms::Atom; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/operators.rs b/crates/swc_ecma_ast/src/operators.rs index f64efc54a3af..d2718a6e9d4d 100644 --- a/crates/swc_ecma_ast/src/operators.rs +++ b/crates/swc_ecma_ast/src/operators.rs @@ -1,4 +1,5 @@ use string_enum::StringEnum; +use swc_allocator::{boxed::Box, Vec}; use swc_common::EqIgnoreSpan; #[derive(StringEnum, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, EqIgnoreSpan, Default)] diff --git a/crates/swc_ecma_ast/src/prop.rs b/crates/swc_ecma_ast/src/prop.rs index 40318dcffd0a..69610ba0e23a 100644 --- a/crates/swc_ecma_ast/src/prop.rs +++ b/crates/swc_ecma_ast/src/prop.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; use crate::{ diff --git a/crates/swc_ecma_ast/src/source_map.rs b/crates/swc_ecma_ast/src/source_map.rs index 27440fea7491..604abe7b5969 100644 --- a/crates/swc_ecma_ast/src/source_map.rs +++ b/crates/swc_ecma_ast/src/source_map.rs @@ -1,5 +1,6 @@ use std::{rc::Rc, sync::Arc}; +use swc_allocator::{boxed::Box, Vec}; use swc_common::{BytePos, SourceMap, SourceMapper, SourceMapperDyn, Span, Spanned}; use crate::list::ListFormat; diff --git a/crates/swc_ecma_ast/src/stmt.rs b/crates/swc_ecma_ast/src/stmt.rs index 32665b359a73..f2d12075115c 100644 --- a/crates/swc_ecma_ast/src/stmt.rs +++ b/crates/swc_ecma_ast/src/stmt.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP}; use crate::{ From 73b25826a452cd851cdd575a671972d75e4e3ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:12:04 +0900 Subject: [PATCH 21/65] `Vec` --- crates/swc_allocator/src/alloc.rs | 4 ++-- crates/swc_allocator/src/lib.rs | 26 +++++++------------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 4cf8f569a025..63beabe86d82 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -7,8 +7,8 @@ use crate::Allocator; scoped_thread_local!(pub(crate) static ALLOC: Allocator); #[derive(Clone, Copy)] -pub(crate) struct SwcAlloc { - pub is_custom: bool, +pub struct SwcAlloc { + pub(crate) is_custom: bool, } impl SwcAlloc { diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index d805ae601e04..69e361af0ef4 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -2,7 +2,7 @@ //! //! API designed after [`oxc_allocator`](https://github.com/oxc-project/oxc/tree/725571aad193ec6ba779c820baeb4a7774533ed7/crates/oxc_allocator/src). -use alloc::ALLOC; +use alloc::{SwcAlloc, ALLOC}; use std::ops::{Deref, DerefMut}; use bumpalo::Bump; @@ -46,23 +46,11 @@ impl DerefMut for Allocator { } } -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] -pub struct Vec<'alloc, T>(bumpalo::collections::Vec<'alloc, T>); - -impl<'alloc, T> Vec<'alloc, T> { - #[inline(always)] - pub fn new(alloc: &'alloc Allocator) -> Self { - Self(bumpalo::collections::Vec::new_in(alloc)) - } - - #[inline(always)] - pub fn with_capacity(alloc: &'alloc Allocator, capacity: usize) -> Self { - Self(bumpalo::collections::Vec::with_capacity_in(capacity, alloc)) - } -} +pub struct Vec(allocator_api2::vec::Vec); -impl<'alloc, T> Deref for Vec<'alloc, T> { +impl Deref for Vec { type Target = [T]; fn deref(&self) -> &[T] { @@ -70,14 +58,14 @@ impl<'alloc, T> Deref for Vec<'alloc, T> { } } -impl<'alloc, T> DerefMut for Vec<'alloc, T> { +impl DerefMut for Vec { fn deref_mut(&mut self) -> &mut [T] { &mut self.0 } } -impl<'alloc, T> IntoIterator for Vec<'alloc, T> { - type IntoIter = bumpalo::collections::vec::IntoIter<'alloc, T>; +impl IntoIterator for Vec { + type IntoIter = allocator_api2::vec::IntoIter; type Item = T; fn into_iter(self) -> Self::IntoIter { From d31747c7dfcef3818341e18274e75796e299d0b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:12:53 +0900 Subject: [PATCH 22/65] Dep serde --- crates/swc_allocator/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_allocator/Cargo.toml b/crates/swc_allocator/Cargo.toml index ebf970e16aeb..7c2b6b6f367e 100644 --- a/crates/swc_allocator/Cargo.toml +++ b/crates/swc_allocator/Cargo.toml @@ -9,7 +9,7 @@ repository = { workspace = true } version = "0.1.1" [dependencies] -allocator-api2 = { workspace = true } +allocator-api2 = { workspace = true, features = ["serde"] } bumpalo = { workspace = true, features = [ "allocator-api2", "boxed", From 09e4a42fcfe5e765ffefb5f7219e470f415648d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:13:24 +0900 Subject: [PATCH 23/65] dep --- crates/swc_allocator/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/swc_allocator/Cargo.toml b/crates/swc_allocator/Cargo.toml index 7c2b6b6f367e..78b5fd504a43 100644 --- a/crates/swc_allocator/Cargo.toml +++ b/crates/swc_allocator/Cargo.toml @@ -16,3 +16,5 @@ bumpalo = { workspace = true, features = [ "collections", ] } scoped-tls = { workspace = true } +serde = { workspace = true } +serde_derive = { workspace = true } From e2de2f7a733b9dccaed525e72096428d0f6d0897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:13:33 +0900 Subject: [PATCH 24/65] cargo lockfile --- Cargo.lock | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fecc38b913c7..5f9bd1d4c3b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,6 +67,9 @@ name = "allocator-api2" version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +dependencies = [ + "serde", +] [[package]] name = "android-tzdata" @@ -3319,9 +3322,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.197" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] @@ -3349,9 +3352,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.197" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", @@ -3700,6 +3703,8 @@ dependencies = [ "allocator-api2", "bumpalo", "scoped-tls", + "serde", + "serde_derive", ] [[package]] From 0d27464d60b7f1ec70ba8ee1dcc5f93c53a947b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:15:48 +0900 Subject: [PATCH 25/65] More work for allocator --- crates/swc_allocator/src/alloc.rs | 2 +- crates/swc_allocator/src/boxed.rs | 24 ++++++++++++++++++++++++ crates/swc_allocator/src/lib.rs | 3 ++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 63beabe86d82..712358650878 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -6,7 +6,7 @@ use crate::Allocator; scoped_thread_local!(pub(crate) static ALLOC: Allocator); -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Default)] pub struct SwcAlloc { pub(crate) is_custom: bool, } diff --git a/crates/swc_allocator/src/boxed.rs b/crates/swc_allocator/src/boxed.rs index 9502c6c4b38e..89e9974d7405 100644 --- a/crates/swc_allocator/src/boxed.rs +++ b/crates/swc_allocator/src/boxed.rs @@ -42,3 +42,27 @@ impl DerefMut for Box { &mut self.0 } } + +impl serde::Serialize for Box +where + T: serde::Serialize, +{ + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + self.0.serialize(serializer) + } +} + +impl<'de, T> serde::Deserialize<'de> for Box +where + T: serde::Deserialize<'de>, +{ + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: serde::Deserializer<'de>, + { + Ok(Box(allocator_api2::boxed::Box::deserialize(deserializer)?)) + } +} diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 69e361af0ef4..5e6407f1632d 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -6,6 +6,7 @@ use alloc::{SwcAlloc, ALLOC}; use std::ops::{Deref, DerefMut}; use bumpalo::Bump; +use serde_derive::{Deserialize, Serialize}; mod alloc; pub mod boxed; @@ -46,7 +47,7 @@ impl DerefMut for Allocator { } } -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[repr(transparent)] pub struct Vec(allocator_api2::vec::Vec); From fff6b2b13ac2f7e839d2184a6a2772272bbaec06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:18:24 +0900 Subject: [PATCH 26/65] more work --- crates/swc_allocator/src/alloc.rs | 10 +++++++++- crates/swc_allocator/src/lib.rs | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 712358650878..42198d8e01b3 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -6,11 +6,19 @@ use crate::Allocator; scoped_thread_local!(pub(crate) static ALLOC: Allocator); -#[derive(Clone, Copy, Default)] +#[derive(Clone, Copy)] pub struct SwcAlloc { pub(crate) is_custom: bool, } +impl Default for SwcAlloc { + fn default() -> Self { + Self { + is_custom: ALLOC.is_set(), + } + } +} + impl SwcAlloc { /// `true` is passed to `f` if the box is allocated with a custom allocator. fn with_allocator(&self, f: impl FnOnce(&dyn allocator_api2::alloc::Allocator) -> T) -> T { diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 5e6407f1632d..b1fbf420c1d8 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -65,6 +65,12 @@ impl DerefMut for Vec { } } +impl Default for Vec { + fn default() -> Self { + Self(allocator_api2::vec::Vec::new_in(SwcAlloc::default())) + } +} + impl IntoIterator for Vec { type IntoIter = allocator_api2::vec::IntoIter; type Item = T; From 2e010b6844335d05578e4606fca0746258e0c7d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:18:56 +0900 Subject: [PATCH 27/65] Dep --- crates/swc_allocator/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/swc_allocator/Cargo.toml b/crates/swc_allocator/Cargo.toml index 78b5fd504a43..9214ed30a3ff 100644 --- a/crates/swc_allocator/Cargo.toml +++ b/crates/swc_allocator/Cargo.toml @@ -15,6 +15,7 @@ bumpalo = { workspace = true, features = [ "boxed", "collections", ] } +rkyv = { workspace = true } scoped-tls = { workspace = true } serde = { workspace = true } serde_derive = { workspace = true } From 856cff9753d1b3d318c6c51a4305269e727344c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:19:02 +0900 Subject: [PATCH 28/65] cargo lockfile --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index 5f9bd1d4c3b4..37609943372c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3702,6 +3702,7 @@ version = "0.1.1" dependencies = [ "allocator-api2", "bumpalo", + "rkyv", "scoped-tls", "serde", "serde_derive", From 970bb225eca2f0a116f7db3815ee34d5774ee77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:33:34 +0900 Subject: [PATCH 29/65] rkyv --- crates/swc_allocator/src/boxed.rs | 14 ++++++++ crates/swc_allocator/src/lib.rs | 53 ++++++------------------------- 2 files changed, 23 insertions(+), 44 deletions(-) diff --git a/crates/swc_allocator/src/boxed.rs b/crates/swc_allocator/src/boxed.rs index 89e9974d7405..aff268ccc1d8 100644 --- a/crates/swc_allocator/src/boxed.rs +++ b/crates/swc_allocator/src/boxed.rs @@ -1,5 +1,7 @@ use std::ops::{Deref, DerefMut}; +use rkyv::Archived; + use crate::alloc::{SwcAlloc, ALLOC}; /// A special `Box` which has size of [`std::boxed::Box`] but **may** be @@ -66,3 +68,15 @@ where Ok(Box(allocator_api2::boxed::Box::deserialize(deserializer)?)) } } + +impl rkyv::Archive for Box +where + T: rkyv::Archive, +{ + type Archived = Archived; + type Resolver = ::Resolver; + + unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { + self.0.resolve(pos, resolver, out as *mut Archived); + } +} diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index b1fbf420c1d8..88c970dbb8cf 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -6,6 +6,7 @@ use alloc::{SwcAlloc, ALLOC}; use std::ops::{Deref, DerefMut}; use bumpalo::Bump; +use rkyv::vec::{ArchivedVec, VecResolver}; use serde_derive::{Deserialize, Serialize}; mod alloc; @@ -80,50 +81,14 @@ impl IntoIterator for Vec { } } -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct String<'alloc>(bumpalo::collections::String<'alloc>); +impl rkyv::Archive for Vec +where + T: rkyv::Archive, +{ + type Archived = rkyv::vec::ArchivedVec; + type Resolver = rkyv::vec::VecResolver; -impl<'alloc> String<'alloc> { - #[inline(always)] - pub fn new(alloc: &'alloc Allocator) -> Self { - Self(bumpalo::collections::String::new_in(alloc)) - } - - #[inline(always)] - pub fn with_capacity(alloc: &'alloc Allocator, capacity: usize) -> Self { - Self(bumpalo::collections::String::with_capacity_in( - capacity, alloc, - )) - } -} - -impl Deref for String<'_> { - type Target = str; - - fn deref(&self) -> &str { - &self.0 - } -} - -impl DerefMut for String<'_> { - fn deref_mut(&mut self) -> &mut str { - &mut self.0 - } -} - -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum CowStr<'alloc> { - Borrowed(&'alloc str), - Owned(String<'alloc>), -} - -impl Deref for CowStr<'_> { - type Target = str; - - fn deref(&self) -> &str { - match self { - CowStr::Borrowed(s) => s, - CowStr::Owned(s) => s, - } + unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { + rkyv::vec::ArchivedVec::resolve_from_slice(self, pos, resolver, out); } } From f61905d0b48ef50d2b4db67645a2cbe8ea3f31d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:38:06 +0900 Subject: [PATCH 30/65] dep --- Cargo.toml | 1 + crates/swc_allocator/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 1804e63f95b2..0bbf01d1e3e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -138,6 +138,7 @@ resolver = "2" wasmer = { version = "4.2.5", default-features = false } wasmer-wasix = { version = "0.18.0", default-features = false } allocator-api2 = "0.2.18" +ptr_meta = "0.2.0" [profile.release] # lto = true diff --git a/crates/swc_allocator/Cargo.toml b/crates/swc_allocator/Cargo.toml index 9214ed30a3ff..da8812f0a248 100644 --- a/crates/swc_allocator/Cargo.toml +++ b/crates/swc_allocator/Cargo.toml @@ -15,6 +15,7 @@ bumpalo = { workspace = true, features = [ "boxed", "collections", ] } +ptr_meta = { workspace = true } rkyv = { workspace = true } scoped-tls = { workspace = true } serde = { workspace = true } From 7b4dcdb11b45ad057c3bda309cbcb870c866f475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:38:18 +0900 Subject: [PATCH 31/65] cargo lockfile --- Cargo.lock | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37609943372c..f540109ca578 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -426,7 +426,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b6372023ac861f6e6dc89c8344a8f398fb42aaba2b5dbc649ca0c0e9dbcb627" dependencies = [ "bytecheck_derive", - "ptr_meta", + "ptr_meta 0.1.4", "simdutf8", ] @@ -2870,7 +2870,16 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" dependencies = [ - "ptr_meta_derive", + "ptr_meta_derive 0.1.4", +] + +[[package]] +name = "ptr_meta" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcada80daa06c42ed5f48c9a043865edea5dc44cbf9ac009fda3b89526e28607" +dependencies = [ + "ptr_meta_derive 0.2.0", ] [[package]] @@ -2884,6 +2893,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "ptr_meta_derive" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bca9224df2e20e7c5548aeb5f110a0f3b77ef05f8585139b7148b59056168ed2" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "pulldown-cmark" version = "0.8.0" @@ -3115,7 +3135,7 @@ dependencies = [ "bytes", "hashbrown 0.12.3", "indexmap 1.9.3", - "ptr_meta", + "ptr_meta 0.1.4", "rend", "rkyv_derive", "seahash", @@ -3702,6 +3722,7 @@ version = "0.1.1" dependencies = [ "allocator-api2", "bumpalo", + "ptr_meta 0.2.0", "rkyv", "scoped-tls", "serde", From 71b2af954e8c4783ba4538e3e316d87fefcf6338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:39:50 +0900 Subject: [PATCH 32/65] More work --- crates/swc_allocator/src/boxed.rs | 15 +++++++++---- crates/swc_allocator/src/lib.rs | 37 +++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/crates/swc_allocator/src/boxed.rs b/crates/swc_allocator/src/boxed.rs index aff268ccc1d8..bdb375976313 100644 --- a/crates/swc_allocator/src/boxed.rs +++ b/crates/swc_allocator/src/boxed.rs @@ -2,7 +2,7 @@ use std::ops::{Deref, DerefMut}; use rkyv::Archived; -use crate::alloc::{SwcAlloc, ALLOC}; +use crate::alloc::SwcAlloc; /// A special `Box` which has size of [`std::boxed::Box`] but **may** be /// allocated with a custom allocator. @@ -18,11 +18,9 @@ pub struct Box(allocator_api2::boxed::Box); impl Box { #[inline(always)] pub fn new(value: T) -> Self { - let is_custom = ALLOC.is_set(); - Self(allocator_api2::boxed::Box::new_in( value, - SwcAlloc { is_custom }, + SwcAlloc::default(), )) } @@ -31,6 +29,15 @@ impl Box { } } +impl Box { + pub unsafe fn from_raw(raw: *mut T) -> Self { + Self(allocator_api2::boxed::Box::from_raw_in( + raw, + SwcAlloc::default(), + )) + } +} + impl Deref for Box { type Target = T; diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 88c970dbb8cf..e32f6c39eb99 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -2,13 +2,20 @@ //! //! API designed after [`oxc_allocator`](https://github.com/oxc-project/oxc/tree/725571aad193ec6ba779c820baeb4a7774533ed7/crates/oxc_allocator/src). -use alloc::{SwcAlloc, ALLOC}; use std::ops::{Deref, DerefMut}; use bumpalo::Bump; -use rkyv::vec::{ArchivedVec, VecResolver}; +use rkyv::{ + vec::{ArchivedVec, VecResolver}, + DeserializeUnsized, +}; use serde_derive::{Deserialize, Serialize}; +use crate::{ + alloc::{SwcAlloc, ALLOC}, + boxed::Box, +}; + mod alloc; pub mod boxed; @@ -92,3 +99,29 @@ where rkyv::vec::ArchivedVec::resolve_from_slice(self, pos, resolver, out); } } + +impl, S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer + ?Sized> + rkyv::Serialize for Vec +{ + #[inline] + fn serialize(&self, serializer: &mut S) -> Result { + ArchivedVec::::serialize_from_slice(self, serializer) + } +} + +impl rkyv::Deserialize, D> + for ArchivedVec +where + [T::Archived]: rkyv::DeserializeUnsized<[T], D>, +{ + #[inline] + fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + unsafe { + let data_address = + (&**self).deserialize_unsized(deserializer, |layout| std::alloc::alloc(layout))?; + let metadata = self.as_slice().deserialize_metadata(deserializer)?; + let ptr = ptr_meta::from_raw_parts_mut(data_address, metadata); + Ok(Box::<[T]>::from_raw(ptr).into()) + } + } +} From c8b779980304c6eda338dd7637e85e4bdc6c573e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:41:37 +0900 Subject: [PATCH 33/65] from impl --- crates/swc_allocator/src/boxed.rs | 2 +- crates/swc_allocator/src/lib.rs | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/swc_allocator/src/boxed.rs b/crates/swc_allocator/src/boxed.rs index bdb375976313..97f9abfe80ba 100644 --- a/crates/swc_allocator/src/boxed.rs +++ b/crates/swc_allocator/src/boxed.rs @@ -13,7 +13,7 @@ use crate::alloc::SwcAlloc; /// The last bit is 1 if the box is allocated with a custom allocator. #[repr(transparent)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Box(allocator_api2::boxed::Box); +pub struct Box(pub(crate) allocator_api2::boxed::Box); impl Box { #[inline(always)] diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index e32f6c39eb99..a2c0c9ac0997 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -5,10 +5,7 @@ use std::ops::{Deref, DerefMut}; use bumpalo::Bump; -use rkyv::{ - vec::{ArchivedVec, VecResolver}, - DeserializeUnsized, -}; +use rkyv::{vec::ArchivedVec, DeserializeUnsized}; use serde_derive::{Deserialize, Serialize}; use crate::{ @@ -88,6 +85,12 @@ impl IntoIterator for Vec { } } +impl From> for Vec { + fn from(v: Box<[T]>) -> Self { + Self(allocator_api2::vec::Vec::from(v.0)) + } +} + impl rkyv::Archive for Vec where T: rkyv::Archive, From 42626cfbf8d082ee5ac0902c62b5714ea7368f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:59:09 +0900 Subject: [PATCH 34/65] dep --- crates/swc_common/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/swc_common/Cargo.toml b/crates/swc_common/Cargo.toml index 15480f7386de..7e842f79c9c5 100644 --- a/crates/swc_common/Cargo.toml +++ b/crates/swc_common/Cargo.toml @@ -65,6 +65,7 @@ url = { workspace = true } ast_node = { version = "0.9.8", path = "../ast_node" } better_scoped_tls = { version = "0.1.1", path = "../better_scoped_tls" } from_variant = { version = "0.1.8", path = "../from_variant" } +swc_allocator = { version = "0.1.1", path = "../swc_allocator" } swc_atoms = { version = "0.6.5", path = "../swc_atoms" } swc_eq_ignore_macros = { version = "0.1.3", path = "../swc_eq_ignore_macros" } swc_visit = { version = "0.5.14", path = "../swc_visit" } From 301b060c4e257264a575b059f7c73263331c3dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 06:59:13 +0900 Subject: [PATCH 35/65] cargo lockfile --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index f540109ca578..002435a92df9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3848,6 +3848,7 @@ dependencies = [ "serde_json", "siphasher", "sourcemap", + "swc_allocator", "swc_atoms", "swc_eq_ignore_macros", "swc_visit", From b6204bb525ea84d8954a3fa5459faeb1b790faec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 14:36:39 +0900 Subject: [PATCH 36/65] rkyv & serde --- .../src/{boxed.rs => boxed/mod.rs} | 43 +++---------------- crates/swc_allocator/src/boxed/rkyv.rs | 24 +++++++++++ crates/swc_allocator/src/boxed/serde.rs | 27 ++++++++++++ 3 files changed, 57 insertions(+), 37 deletions(-) rename crates/swc_allocator/src/{boxed.rs => boxed/mod.rs} (57%) create mode 100644 crates/swc_allocator/src/boxed/rkyv.rs create mode 100644 crates/swc_allocator/src/boxed/serde.rs diff --git a/crates/swc_allocator/src/boxed.rs b/crates/swc_allocator/src/boxed/mod.rs similarity index 57% rename from crates/swc_allocator/src/boxed.rs rename to crates/swc_allocator/src/boxed/mod.rs index 97f9abfe80ba..cfee99b82ff3 100644 --- a/crates/swc_allocator/src/boxed.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -1,6 +1,7 @@ use std::ops::{Deref, DerefMut}; -use rkyv::Archived; +mod rkyv; +mod serde; use crate::alloc::SwcAlloc; @@ -36,6 +37,10 @@ impl Box { SwcAlloc::default(), )) } + + pub fn as_ref(&self) -> &T { + &self.0 + } } impl Deref for Box { @@ -51,39 +56,3 @@ impl DerefMut for Box { &mut self.0 } } - -impl serde::Serialize for Box -where - T: serde::Serialize, -{ - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - self.0.serialize(serializer) - } -} - -impl<'de, T> serde::Deserialize<'de> for Box -where - T: serde::Deserialize<'de>, -{ - fn deserialize(deserializer: D) -> Result, D::Error> - where - D: serde::Deserializer<'de>, - { - Ok(Box(allocator_api2::boxed::Box::deserialize(deserializer)?)) - } -} - -impl rkyv::Archive for Box -where - T: rkyv::Archive, -{ - type Archived = Archived; - type Resolver = ::Resolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - self.0.resolve(pos, resolver, out as *mut Archived); - } -} diff --git a/crates/swc_allocator/src/boxed/rkyv.rs b/crates/swc_allocator/src/boxed/rkyv.rs new file mode 100644 index 000000000000..e941419d6ea0 --- /dev/null +++ b/crates/swc_allocator/src/boxed/rkyv.rs @@ -0,0 +1,24 @@ +use rkyv::{ + boxed::{ArchivedBox, BoxResolver}, + Archive, ArchivePointee, ArchiveUnsized, Archived, Deserialize, DeserializeUnsized, Fallible, + Serialize, SerializeUnsized, +}; + +use super::Box; + +impl Archive for Box { + type Archived = ArchivedBox; + type Resolver = BoxResolver; + + #[inline] + unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { + ArchivedBox::resolve_from_ref(self.as_ref(), pos, resolver, out); + } +} + +impl + ?Sized, S: Fallible + ?Sized> Serialize for Box { + #[inline] + fn serialize(&self, serializer: &mut S) -> Result { + ArchivedBox::serialize_from_ref(self.as_ref(), serializer) + } +} diff --git a/crates/swc_allocator/src/boxed/serde.rs b/crates/swc_allocator/src/boxed/serde.rs new file mode 100644 index 000000000000..17c76e81d71d --- /dev/null +++ b/crates/swc_allocator/src/boxed/serde.rs @@ -0,0 +1,27 @@ +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use super::Box; + +impl Serialize for Box +where + T: Serialize, +{ + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + self.0.serialize(serializer) + } +} + +impl<'de, T> Deserialize<'de> for Box +where + T: Deserialize<'de>, +{ + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + Ok(Box(allocator_api2::boxed::Box::deserialize(deserializer)?)) + } +} From d34c783d748655ef362593d3894f41801849379e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 14:53:30 +0900 Subject: [PATCH 37/65] Fix more --- crates/swc_allocator/src/boxed/rkyv.rs | 3 +-- crates/swc_common/src/eq.rs | 33 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/crates/swc_allocator/src/boxed/rkyv.rs b/crates/swc_allocator/src/boxed/rkyv.rs index e941419d6ea0..67d6a5118a35 100644 --- a/crates/swc_allocator/src/boxed/rkyv.rs +++ b/crates/swc_allocator/src/boxed/rkyv.rs @@ -1,7 +1,6 @@ use rkyv::{ boxed::{ArchivedBox, BoxResolver}, - Archive, ArchivePointee, ArchiveUnsized, Archived, Deserialize, DeserializeUnsized, Fallible, - Serialize, SerializeUnsized, + Archive, ArchiveUnsized, Fallible, Serialize, SerializeUnsized, }; use super::Box; diff --git a/crates/swc_common/src/eq.rs b/crates/swc_common/src/eq.rs index 57d2d8911b49..c8babaf603e8 100644 --- a/crates/swc_common/src/eq.rs +++ b/crates/swc_common/src/eq.rs @@ -63,6 +63,19 @@ where } } +impl EqIgnoreSpan for swc_allocator::Vec +where + T: EqIgnoreSpan, +{ + fn eq_ignore_span(&self, other: &Self) -> bool { + self.len() == other.len() + && self + .iter() + .zip(other.iter()) + .all(|(a, b)| a.eq_ignore_span(b)) + } +} + /// Derive with `#[derive(TypeEq)]`. pub trait TypeEq { /// **Note**: This method should return `true` for non-type values. @@ -172,6 +185,26 @@ macro_rules! deref { deref!(Box, Rc, Arc); +impl EqIgnoreSpan for swc_allocator::boxed::Box +where + N: EqIgnoreSpan, +{ + #[inline] + fn eq_ignore_span(&self, other: &Self) -> bool { + (**self).eq_ignore_span(&**other) + } +} + +impl TypeEq for swc_allocator::boxed::Box +where + N: TypeEq, +{ + #[inline] + fn type_eq(&self, other: &Self) -> bool { + (**self).type_eq(&**other) + } +} + impl<'a, N> EqIgnoreSpan for &'a N where N: EqIgnoreSpan, From a29beda6100798d04f5a71f40273b6b2a3b520a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 15:08:43 +0900 Subject: [PATCH 38/65] WIP --- crates/swc_allocator/src/boxed/rkyv.rs | 38 +++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/swc_allocator/src/boxed/rkyv.rs b/crates/swc_allocator/src/boxed/rkyv.rs index 67d6a5118a35..2c55abf28d37 100644 --- a/crates/swc_allocator/src/boxed/rkyv.rs +++ b/crates/swc_allocator/src/boxed/rkyv.rs @@ -1,6 +1,9 @@ +use std::{alloc, cmp}; + use rkyv::{ boxed::{ArchivedBox, BoxResolver}, - Archive, ArchiveUnsized, Fallible, Serialize, SerializeUnsized, + Archive, ArchivePointee, ArchiveUnsized, Deserialize, DeserializeUnsized, Fallible, Serialize, + SerializeUnsized, }; use super::Box; @@ -21,3 +24,36 @@ impl + ?Sized, S: Fallible + ?Sized> Serialize for Box ArchivedBox::serialize_from_ref(self.as_ref(), serializer) } } + +impl Deserialize, D> for ArchivedBox +where + T: ArchiveUnsized + ?Sized, + T::Archived: DeserializeUnsized, + D: Fallible + ?Sized, +{ + #[inline] + fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + unsafe { + let data_address = self + .get() + .deserialize_unsized(deserializer, |layout| alloc::alloc(layout))?; + let metadata = self.get().deserialize_metadata(deserializer)?; + let ptr = ptr_meta::from_raw_parts_mut(data_address, metadata); + Ok(Box::from_raw(ptr)) + } + } +} + +impl + ?Sized, U: ?Sized> PartialEq> for ArchivedBox { + #[inline] + fn eq(&self, other: &Box) -> bool { + self.get().eq(other.as_ref()) + } +} + +impl + ?Sized, U: ?Sized> PartialOrd> for ArchivedBox { + #[inline] + fn partial_cmp(&self, other: &Box) -> Option { + self.get().partial_cmp(other.as_ref()) + } +} From 3511d58a18a5b01dbb9108c0638f87e932a63629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 17:19:37 +0900 Subject: [PATCH 39/65] Dep version --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0bbf01d1e3e8..ae8f45d1ca9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ resolver = "2" Inflector = "0.11.4" ahash = "0.8.8" + allocator-api2 = "0.2.18" ansi_term = "0.12.1" anyhow = "1.0.81" arbitrary = "1" @@ -95,6 +96,7 @@ resolver = "2" phf = "0.11.2" pretty_assertions = "1.3" proc-macro2 = "1.0.24" + ptr_meta = "0.1.4" quote = "1.0.7" rayon = "1.7.0" regex = "1.5.4" @@ -137,8 +139,6 @@ resolver = "2" wasm-bindgen-futures = "0.4.41" wasmer = { version = "4.2.5", default-features = false } wasmer-wasix = { version = "0.18.0", default-features = false } -allocator-api2 = "0.2.18" -ptr_meta = "0.2.0" [profile.release] # lto = true From 053edf138b5b07a51cb281009c05ffebf1785949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 17:19:44 +0900 Subject: [PATCH 40/65] cargo lockfile --- Cargo.lock | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 002435a92df9..3e88c830f125 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -426,7 +426,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b6372023ac861f6e6dc89c8344a8f398fb42aaba2b5dbc649ca0c0e9dbcb627" dependencies = [ "bytecheck_derive", - "ptr_meta 0.1.4", + "ptr_meta", "simdutf8", ] @@ -2870,16 +2870,7 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" dependencies = [ - "ptr_meta_derive 0.1.4", -] - -[[package]] -name = "ptr_meta" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcada80daa06c42ed5f48c9a043865edea5dc44cbf9ac009fda3b89526e28607" -dependencies = [ - "ptr_meta_derive 0.2.0", + "ptr_meta_derive", ] [[package]] @@ -2893,17 +2884,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "ptr_meta_derive" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bca9224df2e20e7c5548aeb5f110a0f3b77ef05f8585139b7148b59056168ed2" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "pulldown-cmark" version = "0.8.0" @@ -3135,7 +3115,7 @@ dependencies = [ "bytes", "hashbrown 0.12.3", "indexmap 1.9.3", - "ptr_meta 0.1.4", + "ptr_meta", "rend", "rkyv_derive", "seahash", @@ -3722,7 +3702,7 @@ version = "0.1.1" dependencies = [ "allocator-api2", "bumpalo", - "ptr_meta 0.2.0", + "ptr_meta", "rkyv", "scoped-tls", "serde", From 94500f0a6052c03753c340a69f517e3981fe49a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 17:20:12 +0900 Subject: [PATCH 41/65] as_ref() --- crates/swc_allocator/src/boxed/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/swc_allocator/src/boxed/mod.rs b/crates/swc_allocator/src/boxed/mod.rs index cfee99b82ff3..a686f72ad784 100644 --- a/crates/swc_allocator/src/boxed/mod.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -37,8 +37,10 @@ impl Box { SwcAlloc::default(), )) } +} - pub fn as_ref(&self) -> &T { +impl AsRef for Box { + fn as_ref(&self) -> &T { &self.0 } } From fd1de5ecb812d6537f748138c7aa41b0d6b90def Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 17:21:39 +0900 Subject: [PATCH 42/65] Take --- crates/swc_common/src/util/take.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/swc_common/src/util/take.rs b/crates/swc_common/src/util/take.rs index 8dfd21394c27..c099a3b93c7c 100644 --- a/crates/swc_common/src/util/take.rs +++ b/crates/swc_common/src/util/take.rs @@ -54,3 +54,18 @@ impl Take for Span { DUMMY_SP } } + +impl Take for swc_allocator::boxed::Box +where + T: Take, +{ + fn dummy() -> Self { + swc_allocator::boxed::Box::new(T::dummy()) + } +} + +impl Take for swc_allocator::Vec { + fn dummy() -> Self { + swc_allocator::Vec::default() + } +} From 5dd8489ea5fe8e30be425644b06411134e335045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 17:23:54 +0900 Subject: [PATCH 43/65] Spanned --- crates/swc_common/src/pos.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/swc_common/src/pos.rs b/crates/swc_common/src/pos.rs index 9fb8d3ced480..07196bc52e9c 100644 --- a/crates/swc_common/src/pos.rs +++ b/crates/swc_common/src/pos.rs @@ -190,3 +190,12 @@ where } } } + +impl Spanned for swc_allocator::boxed::Box +where + T: Spanned, +{ + fn span(&self) -> Span { + self.as_ref().span() + } +} From e19c20bcc79c341e059747712c753149bdd7518d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 17:27:54 +0900 Subject: [PATCH 44/65] `Default` --- crates/swc_allocator/src/boxed/mod.rs | 9 +++++++++ crates/swc_allocator/src/lib.rs | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/swc_allocator/src/boxed/mod.rs b/crates/swc_allocator/src/boxed/mod.rs index a686f72ad784..477970c554a4 100644 --- a/crates/swc_allocator/src/boxed/mod.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -16,6 +16,15 @@ use crate::alloc::SwcAlloc; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Box(pub(crate) allocator_api2::boxed::Box); +impl Default for Box +where + T: Default, +{ + fn default() -> Self { + Box::new(Default::default()) + } +} + impl Box { #[inline(always)] pub fn new(value: T) -> Self { diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index a2c0c9ac0997..7f56632a4cff 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -121,7 +121,7 @@ where fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { unsafe { let data_address = - (&**self).deserialize_unsized(deserializer, |layout| std::alloc::alloc(layout))?; + (**self).deserialize_unsized(deserializer, |layout| std::alloc::alloc(layout))?; let metadata = self.as_slice().deserialize_metadata(deserializer)?; let ptr = ptr_meta::from_raw_parts_mut(data_address, metadata); Ok(Box::<[T]>::from_raw(ptr).into()) From 6720faf97dd309a5fb648c5c7760d68437ca0daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 17:30:10 +0900 Subject: [PATCH 45/65] Move vec --- crates/swc_allocator/src/lib.rs | 85 +---------------------------- crates/swc_allocator/src/vec/mod.rs | 83 ++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 83 deletions(-) create mode 100644 crates/swc_allocator/src/vec/mod.rs diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 7f56632a4cff..5a85d03f448e 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -5,16 +5,12 @@ use std::ops::{Deref, DerefMut}; use bumpalo::Bump; -use rkyv::{vec::ArchivedVec, DeserializeUnsized}; -use serde_derive::{Deserialize, Serialize}; -use crate::{ - alloc::{SwcAlloc, ALLOC}, - boxed::Box, -}; +use crate::alloc::ALLOC; mod alloc; pub mod boxed; +pub mod vec; #[derive(Default)] pub struct Allocator { @@ -51,80 +47,3 @@ impl DerefMut for Allocator { &mut self.alloc } } - -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] -#[repr(transparent)] -pub struct Vec(allocator_api2::vec::Vec); - -impl Deref for Vec { - type Target = [T]; - - fn deref(&self) -> &[T] { - &self.0 - } -} - -impl DerefMut for Vec { - fn deref_mut(&mut self) -> &mut [T] { - &mut self.0 - } -} - -impl Default for Vec { - fn default() -> Self { - Self(allocator_api2::vec::Vec::new_in(SwcAlloc::default())) - } -} - -impl IntoIterator for Vec { - type IntoIter = allocator_api2::vec::IntoIter; - type Item = T; - - fn into_iter(self) -> Self::IntoIter { - self.0.into_iter() - } -} - -impl From> for Vec { - fn from(v: Box<[T]>) -> Self { - Self(allocator_api2::vec::Vec::from(v.0)) - } -} - -impl rkyv::Archive for Vec -where - T: rkyv::Archive, -{ - type Archived = rkyv::vec::ArchivedVec; - type Resolver = rkyv::vec::VecResolver; - - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - rkyv::vec::ArchivedVec::resolve_from_slice(self, pos, resolver, out); - } -} - -impl, S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer + ?Sized> - rkyv::Serialize for Vec -{ - #[inline] - fn serialize(&self, serializer: &mut S) -> Result { - ArchivedVec::::serialize_from_slice(self, serializer) - } -} - -impl rkyv::Deserialize, D> - for ArchivedVec -where - [T::Archived]: rkyv::DeserializeUnsized<[T], D>, -{ - #[inline] - fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { - unsafe { - let data_address = - (**self).deserialize_unsized(deserializer, |layout| std::alloc::alloc(layout))?; - let metadata = self.as_slice().deserialize_metadata(deserializer)?; - let ptr = ptr_meta::from_raw_parts_mut(data_address, metadata); - Ok(Box::<[T]>::from_raw(ptr).into()) - } - } -} diff --git a/crates/swc_allocator/src/vec/mod.rs b/crates/swc_allocator/src/vec/mod.rs new file mode 100644 index 000000000000..d17521e6235f --- /dev/null +++ b/crates/swc_allocator/src/vec/mod.rs @@ -0,0 +1,83 @@ +use std::ops::{Deref, DerefMut}; + +use rkyv::{vec::ArchivedVec, DeserializeUnsized}; +use serde_derive::{Deserialize, Serialize}; + +use crate::{alloc::SwcAlloc, boxed::Box}; + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[repr(transparent)] +pub struct Vec(allocator_api2::vec::Vec); + +impl Deref for Vec { + type Target = [T]; + + fn deref(&self) -> &[T] { + &self.0 + } +} + +impl DerefMut for Vec { + fn deref_mut(&mut self) -> &mut [T] { + &mut self.0 + } +} + +impl Default for Vec { + fn default() -> Self { + Self(allocator_api2::vec::Vec::new_in(SwcAlloc::default())) + } +} + +impl IntoIterator for Vec { + type IntoIter = allocator_api2::vec::IntoIter; + type Item = T; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + +impl From> for Vec { + fn from(v: Box<[T]>) -> Self { + Self(allocator_api2::vec::Vec::from(v.0)) + } +} + +impl rkyv::Archive for Vec +where + T: rkyv::Archive, +{ + type Archived = rkyv::vec::ArchivedVec; + type Resolver = rkyv::vec::VecResolver; + + unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { + rkyv::vec::ArchivedVec::resolve_from_slice(self, pos, resolver, out); + } +} + +impl, S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer + ?Sized> + rkyv::Serialize for Vec +{ + #[inline] + fn serialize(&self, serializer: &mut S) -> Result { + ArchivedVec::::serialize_from_slice(self, serializer) + } +} + +impl rkyv::Deserialize, D> + for ArchivedVec +where + [T::Archived]: rkyv::DeserializeUnsized<[T], D>, +{ + #[inline] + fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + unsafe { + let data_address = + (**self).deserialize_unsized(deserializer, |layout| std::alloc::alloc(layout))?; + let metadata = self.as_slice().deserialize_metadata(deserializer)?; + let ptr = ptr_meta::from_raw_parts_mut(data_address, metadata); + Ok(Box::<[T]>::from_raw(ptr).into()) + } + } +} From 9605763ae8cb18d6fe84e3f3e3fed21e79c552a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 17:30:39 +0900 Subject: [PATCH 46/65] Fix swc_common --- crates/swc_common/src/eq.rs | 2 +- crates/swc_common/src/util/take.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/swc_common/src/eq.rs b/crates/swc_common/src/eq.rs index c8babaf603e8..ffd615011b0b 100644 --- a/crates/swc_common/src/eq.rs +++ b/crates/swc_common/src/eq.rs @@ -63,7 +63,7 @@ where } } -impl EqIgnoreSpan for swc_allocator::Vec +impl EqIgnoreSpan for swc_allocator::vec::Vec where T: EqIgnoreSpan, { diff --git a/crates/swc_common/src/util/take.rs b/crates/swc_common/src/util/take.rs index c099a3b93c7c..376a987940f8 100644 --- a/crates/swc_common/src/util/take.rs +++ b/crates/swc_common/src/util/take.rs @@ -64,8 +64,8 @@ where } } -impl Take for swc_allocator::Vec { +impl Take for swc_allocator::vec::Vec { fn dummy() -> Self { - swc_allocator::Vec::default() + Default::default() } } From d8f4880da43a08181eaf578275717e2c02447f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 17:32:07 +0900 Subject: [PATCH 47/65] Update import --- crates/swc_ecma_ast/src/class.rs | 2 +- crates/swc_ecma_ast/src/decl.rs | 2 +- crates/swc_ecma_ast/src/expr.rs | 2 +- crates/swc_ecma_ast/src/function.rs | 2 +- crates/swc_ecma_ast/src/ident.rs | 2 +- crates/swc_ecma_ast/src/jsx.rs | 2 +- crates/swc_ecma_ast/src/lit.rs | 2 +- crates/swc_ecma_ast/src/module.rs | 2 +- crates/swc_ecma_ast/src/module_decl.rs | 2 +- crates/swc_ecma_ast/src/operators.rs | 2 +- crates/swc_ecma_ast/src/prop.rs | 2 +- crates/swc_ecma_ast/src/source_map.rs | 2 +- crates/swc_ecma_ast/src/stmt.rs | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/swc_ecma_ast/src/class.rs b/crates/swc_ecma_ast/src/class.rs index 86f013fdc13e..ab07be5089f8 100644 --- a/crates/swc_ecma_ast/src/class.rs +++ b/crates/swc_ecma_ast/src/class.rs @@ -1,5 +1,5 @@ use is_macro::Is; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP}; use crate::{ diff --git a/crates/swc_ecma_ast/src/decl.rs b/crates/swc_ecma_ast/src/decl.rs index 639fbc41035b..d82bdac3150b 100644 --- a/crates/swc_ecma_ast/src/decl.rs +++ b/crates/swc_ecma_ast/src/decl.rs @@ -1,6 +1,6 @@ use is_macro::Is; use string_enum::StringEnum; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP}; use crate::{ diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index 7a6a6dcfc980..9e6fc290cd2f 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -3,7 +3,7 @@ use std::{borrow::Cow, mem::transmute}; use is_macro::Is; use string_enum::StringEnum; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::Atom; use swc_common::{ ast_node, util::take::Take, BytePos, EqIgnoreSpan, Span, Spanned, SyntaxContext, DUMMY_SP, diff --git a/crates/swc_ecma_ast/src/function.rs b/crates/swc_ecma_ast/src/function.rs index b6dedc6505f1..510b3a8205d8 100644 --- a/crates/swc_ecma_ast/src/function.rs +++ b/crates/swc_ecma_ast/src/function.rs @@ -1,5 +1,5 @@ use is_macro::Is; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP}; use crate::{ diff --git a/crates/swc_ecma_ast/src/ident.rs b/crates/swc_ecma_ast/src/ident.rs index 54b4e1ca29a6..e2057232ceb9 100644 --- a/crates/swc_ecma_ast/src/ident.rs +++ b/crates/swc_ecma_ast/src/ident.rs @@ -5,7 +5,7 @@ use std::{ }; use phf::phf_set; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::{js_word, Atom}; use swc_common::{ ast_node, util::take::Take, BytePos, EqIgnoreSpan, Mark, Span, Spanned, SyntaxContext, DUMMY_SP, diff --git a/crates/swc_ecma_ast/src/jsx.rs b/crates/swc_ecma_ast/src/jsx.rs index 9a59c9457b4a..0bf698918079 100644 --- a/crates/swc_ecma_ast/src/jsx.rs +++ b/crates/swc_ecma_ast/src/jsx.rs @@ -1,5 +1,5 @@ use is_macro::Is; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::Atom; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/lit.rs b/crates/swc_ecma_ast/src/lit.rs index 1ff1edb12e7c..95f2d103931a 100644 --- a/crates/swc_ecma_ast/src/lit.rs +++ b/crates/swc_ecma_ast/src/lit.rs @@ -5,7 +5,7 @@ use std::{ }; use num_bigint::BigInt as BigIntValue; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::{js_word, Atom}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/module.rs b/crates/swc_ecma_ast/src/module.rs index 54aa541c2f58..72c2b5b13a3b 100644 --- a/crates/swc_ecma_ast/src/module.rs +++ b/crates/swc_ecma_ast/src/module.rs @@ -1,5 +1,5 @@ use is_macro::Is; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::Atom; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/module_decl.rs b/crates/swc_ecma_ast/src/module_decl.rs index be5b9b561e40..0c872d75f71d 100644 --- a/crates/swc_ecma_ast/src/module_decl.rs +++ b/crates/swc_ecma_ast/src/module_decl.rs @@ -1,5 +1,5 @@ use is_macro::Is; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::Atom; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/operators.rs b/crates/swc_ecma_ast/src/operators.rs index d2718a6e9d4d..fd4eff7fdb50 100644 --- a/crates/swc_ecma_ast/src/operators.rs +++ b/crates/swc_ecma_ast/src/operators.rs @@ -1,5 +1,5 @@ use string_enum::StringEnum; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::EqIgnoreSpan; #[derive(StringEnum, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, EqIgnoreSpan, Default)] diff --git a/crates/swc_ecma_ast/src/prop.rs b/crates/swc_ecma_ast/src/prop.rs index 69610ba0e23a..1b303c9fd4af 100644 --- a/crates/swc_ecma_ast/src/prop.rs +++ b/crates/swc_ecma_ast/src/prop.rs @@ -1,5 +1,5 @@ use is_macro::Is; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; use crate::{ diff --git a/crates/swc_ecma_ast/src/source_map.rs b/crates/swc_ecma_ast/src/source_map.rs index 604abe7b5969..f50021dd69d4 100644 --- a/crates/swc_ecma_ast/src/source_map.rs +++ b/crates/swc_ecma_ast/src/source_map.rs @@ -1,6 +1,6 @@ use std::{rc::Rc, sync::Arc}; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{BytePos, SourceMap, SourceMapper, SourceMapperDyn, Span, Spanned}; use crate::list::ListFormat; diff --git a/crates/swc_ecma_ast/src/stmt.rs b/crates/swc_ecma_ast/src/stmt.rs index f2d12075115c..b532eafd8229 100644 --- a/crates/swc_ecma_ast/src/stmt.rs +++ b/crates/swc_ecma_ast/src/stmt.rs @@ -1,5 +1,5 @@ use is_macro::Is; -use swc_allocator::{boxed::Box, Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP}; use crate::{ From 32b9ace122ee37bf5edc27516b4b8712b1ac0594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 17:33:28 +0900 Subject: [PATCH 48/65] Fix deref --- crates/swc_allocator/src/vec/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/swc_allocator/src/vec/mod.rs b/crates/swc_allocator/src/vec/mod.rs index d17521e6235f..e96e5a26204e 100644 --- a/crates/swc_allocator/src/vec/mod.rs +++ b/crates/swc_allocator/src/vec/mod.rs @@ -10,15 +10,15 @@ use crate::{alloc::SwcAlloc, boxed::Box}; pub struct Vec(allocator_api2::vec::Vec); impl Deref for Vec { - type Target = [T]; + type Target = allocator_api2::vec::Vec; - fn deref(&self) -> &[T] { + fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for Vec { - fn deref_mut(&mut self) -> &mut [T] { + fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } From b3669258e00327cbfe6c74ce43793aa81e4dcc9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 22:15:42 +0900 Subject: [PATCH 49/65] iteratr0o --- crates/swc_allocator/src/vec/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/swc_allocator/src/vec/mod.rs b/crates/swc_allocator/src/vec/mod.rs index e96e5a26204e..45d60aac9acd 100644 --- a/crates/swc_allocator/src/vec/mod.rs +++ b/crates/swc_allocator/src/vec/mod.rs @@ -37,6 +37,23 @@ impl IntoIterator for Vec { self.0.into_iter() } } +impl<'a, T> IntoIterator for &'a Vec { + type IntoIter = std::slice::Iter<'a, T>; + type Item = &'a T; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + +impl<'a, T> IntoIterator for &'a mut Vec { + type IntoIter = std::slice::IterMut<'a, T>; + type Item = &'a mut T; + + fn into_iter(self) -> Self::IntoIter { + self.iter_mut() + } +} impl From> for Vec { fn from(v: Box<[T]>) -> Self { From caed2b1bae2785a891fca55b844802149f4054df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 22:20:30 +0900 Subject: [PATCH 50/65] FromIter --- crates/swc_allocator/src/vec/mod.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/swc_allocator/src/vec/mod.rs b/crates/swc_allocator/src/vec/mod.rs index 45d60aac9acd..fc24f653e49a 100644 --- a/crates/swc_allocator/src/vec/mod.rs +++ b/crates/swc_allocator/src/vec/mod.rs @@ -55,6 +55,14 @@ impl<'a, T> IntoIterator for &'a mut Vec { } } +impl FromIterator for Vec { + fn from_iter>(iter: I) -> Self { + let mut vec = Vec::default(); + vec.extend(iter); + vec + } +} + impl From> for Vec { fn from(v: Box<[T]>) -> Self { Self(allocator_api2::vec::Vec::from(v.0)) From a7237915eb21047b0b0deb37f91da45664db54cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 13 Jul 2024 22:21:30 +0900 Subject: [PATCH 51/65] More AST fix --- crates/swc_ecma_ast/src/expr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index 9e6fc290cd2f..abf703d2112a 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -529,7 +529,7 @@ impl ObjectLit { /// /// Returns [None] if this is not a valid for `with` of [crate::ImportDecl]. pub fn as_import_with(&self) -> Option { - let mut values = vec![]; + let mut values = Vec::default(); for prop in &self.props { match prop { PropOrSpread::Spread(..) => return None, @@ -1402,7 +1402,7 @@ impl TryFrom> for AssignTarget { type Error = Box; fn try_from(p: Box) -> Result { - (*p).try_into().map_err(Box::new) + p.unbox().try_into().map_err(Box::new) } } From 13fc6600dd97970605576e883fde1639dad848f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 00:42:21 +0900 Subject: [PATCH 52/65] Vec::new() --- crates/swc_allocator/src/vec/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/swc_allocator/src/vec/mod.rs b/crates/swc_allocator/src/vec/mod.rs index fc24f653e49a..f3825c15880c 100644 --- a/crates/swc_allocator/src/vec/mod.rs +++ b/crates/swc_allocator/src/vec/mod.rs @@ -9,6 +9,12 @@ use crate::{alloc::SwcAlloc, boxed::Box}; #[repr(transparent)] pub struct Vec(allocator_api2::vec::Vec); +impl Vec { + pub fn new() -> Self { + Default::default() + } +} + impl Deref for Vec { type Target = allocator_api2::vec::Vec; From 9200ca2556e63ececa084686be1258ce28640df9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 00:47:48 +0900 Subject: [PATCH 53/65] fix more AST --- crates/swc_ecma_ast/src/pat.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/swc_ecma_ast/src/pat.rs b/crates/swc_ecma_ast/src/pat.rs index ab42072eb61a..0fcf2d74c188 100644 --- a/crates/swc_ecma_ast/src/pat.rs +++ b/crates/swc_ecma_ast/src/pat.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; use crate::{ From 812cf9eddee15fb8e8fbc1c7265e316e615385ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 00:49:08 +0900 Subject: [PATCH 54/65] fix more --- crates/swc_ecma_ast/src/expr.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index abf703d2112a..5e8a40158be3 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -1503,7 +1503,8 @@ impl TryFrom> for SimpleAssignTarget { type Error = Box; fn try_from(e: Box) -> Result { - Ok(match *e { + let e = e.unbox(); + Ok(match e { Expr::Ident(i) => SimpleAssignTarget::Ident(i.into()), Expr::Member(m) => SimpleAssignTarget::Member(m), Expr::SuperProp(s) => SimpleAssignTarget::SuperProp(s), @@ -1514,7 +1515,7 @@ impl TryFrom> for SimpleAssignTarget { Expr::TsNonNull(n) => SimpleAssignTarget::TsNonNull(n), Expr::TsTypeAssertion(a) => SimpleAssignTarget::TsTypeAssertion(a), Expr::TsInstantiation(a) => SimpleAssignTarget::TsInstantiation(a), - _ => return Err(e), + _ => return Err(Box::new(e)), }) } } From 445142f7e8165ffe00c819de69031ee378d953ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 01:08:45 +0900 Subject: [PATCH 55/65] AsMut --- crates/swc_allocator/src/boxed/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/swc_allocator/src/boxed/mod.rs b/crates/swc_allocator/src/boxed/mod.rs index 477970c554a4..069a34841133 100644 --- a/crates/swc_allocator/src/boxed/mod.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -54,6 +54,12 @@ impl AsRef for Box { } } +impl AsMut for Box { + fn as_mut(&mut self) -> &mut T { + &mut self.0 + } +} + impl Deref for Box { type Target = T; From ab3c63eae9afbc43efdc3885358da93013ed059b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4/Donny?= Date: Sun, 14 Jul 2024 02:38:05 +0900 Subject: [PATCH 56/65] impl From for Box --- crates/swc_allocator/src/boxed/mod.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/swc_allocator/src/boxed/mod.rs b/crates/swc_allocator/src/boxed/mod.rs index 069a34841133..ae1cc750bd61 100644 --- a/crates/swc_allocator/src/boxed/mod.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -16,6 +16,13 @@ use crate::alloc::SwcAlloc; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Box(pub(crate) allocator_api2::boxed::Box); +impl From for Box { + #[inline(always)] + fn from(v: T) -> Self { + Box::new(v) + } +} + impl Default for Box where T: Default, From 7477b739c30101a2c0c2e4396e37ba0a119c05a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 03:23:37 +0900 Subject: [PATCH 57/65] More ast work --- crates/swc_ecma_ast/src/decl.rs | 2 -- crates/swc_ecma_ast/src/expr.rs | 2 +- crates/swc_ecma_ast/src/macros.rs | 16 ++++++++++++++-- crates/swc_ecma_ast/src/pat.rs | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/swc_ecma_ast/src/decl.rs b/crates/swc_ecma_ast/src/decl.rs index d82bdac3150b..3f5d28984af8 100644 --- a/crates/swc_ecma_ast/src/decl.rs +++ b/crates/swc_ecma_ast/src/decl.rs @@ -71,8 +71,6 @@ decl_from!( macro_rules! decl_from_boxed { ($($variant_ty:ty),*) => { $( - bridge_from!(Box, Decl, $variant_ty); - bridge_from!(Box, Decl, Box<$variant_ty>); bridge_from!(crate::Stmt, Decl, Box<$variant_ty>); bridge_from!(crate::ModuleItem, crate::Stmt, Box<$variant_ty>); )* diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index 5e8a40158be3..fb6fcead4fc5 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -437,7 +437,7 @@ bridge_expr_from!(ClassExpr, Class); macro_rules! boxed_expr { ($T:ty) => { - bridge_from!(Box, Expr, $T); + bridge_into!(Box, Expr, $T); }; } diff --git a/crates/swc_ecma_ast/src/macros.rs b/crates/swc_ecma_ast/src/macros.rs index 8cea2656f6f8..e5112ffb51ba 100644 --- a/crates/swc_ecma_ast/src/macros.rs +++ b/crates/swc_ecma_ast/src/macros.rs @@ -214,10 +214,22 @@ macro_rules! bridge_from { }; } +macro_rules! bridge_into { + ($dst:ty, $bridge:ty, $src:ty) => { + impl Into<$dst> for $src { + #[cfg_attr(not(debug_assertions), inline(always))] + fn into(src: $src) -> $dst { + let src: $bridge = src.into(); + src.into() + } + } + }; +} + macro_rules! bridge_expr_from { ($bridge:ty, $src:ty) => { bridge_from!(crate::Expr, $bridge, $src); - bridge_from!(Box, crate::Expr, $src); + bridge_into!(Box, crate::Expr, $src); }; } @@ -225,6 +237,6 @@ macro_rules! bridge_pat_from { ($bridge:ty, $src:ty) => { bridge_from!(crate::Pat, $bridge, $src); bridge_from!(crate::Param, crate::Pat, $src); - bridge_from!(Box, crate::Pat, $src); + bridge_into!(Box, crate::Pat, $src); }; } diff --git a/crates/swc_ecma_ast/src/pat.rs b/crates/swc_ecma_ast/src/pat.rs index 0fcf2d74c188..3ac5bb07809d 100644 --- a/crates/swc_ecma_ast/src/pat.rs +++ b/crates/swc_ecma_ast/src/pat.rs @@ -72,7 +72,7 @@ bridge_pat_from!(BindingIdent, Id); macro_rules! pat_to_other { ($T:ty) => { bridge_from!(crate::Param, crate::Pat, $T); - bridge_from!(Box, crate::Pat, $T); + bridge_into!(Box, crate::Pat, $T); }; } From 583c2ed1276332ecb233bfa9bff08d8363f8e12b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 03:30:26 +0900 Subject: [PATCH 58/65] More AST work --- crates/swc_ecma_ast/src/expr.rs | 3 +-- crates/swc_ecma_ast/src/macros.rs | 8 ++++---- crates/swc_ecma_ast/src/pat.rs | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index fb6fcead4fc5..0ffd98b51922 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -169,7 +169,7 @@ pub enum Expr { Invalid(Invalid), } -bridge_from!(Box, Box, JSXElement); +bridge_from!(Expr, Box, JSXElement); // Memory layout depends on the version of rustc. // #[cfg(target_pointer_width = "64")] @@ -468,7 +468,6 @@ boxed_expr!(ParenExpr); boxed_expr!(JSXMemberExpr); boxed_expr!(JSXNamespacedName); boxed_expr!(JSXEmptyExpr); -boxed_expr!(Box); boxed_expr!(JSXFragment); boxed_expr!(TsTypeAssertion); boxed_expr!(TsSatisfiesExpr); diff --git a/crates/swc_ecma_ast/src/macros.rs b/crates/swc_ecma_ast/src/macros.rs index e5112ffb51ba..74cd9262b8e0 100644 --- a/crates/swc_ecma_ast/src/macros.rs +++ b/crates/swc_ecma_ast/src/macros.rs @@ -218,8 +218,8 @@ macro_rules! bridge_into { ($dst:ty, $bridge:ty, $src:ty) => { impl Into<$dst> for $src { #[cfg_attr(not(debug_assertions), inline(always))] - fn into(src: $src) -> $dst { - let src: $bridge = src.into(); + fn into(self) -> $dst { + let src: $bridge = self.into(); src.into() } } @@ -229,7 +229,7 @@ macro_rules! bridge_into { macro_rules! bridge_expr_from { ($bridge:ty, $src:ty) => { bridge_from!(crate::Expr, $bridge, $src); - bridge_into!(Box, crate::Expr, $src); + // bridge_into!(Box, crate::Expr, $src); }; } @@ -237,6 +237,6 @@ macro_rules! bridge_pat_from { ($bridge:ty, $src:ty) => { bridge_from!(crate::Pat, $bridge, $src); bridge_from!(crate::Param, crate::Pat, $src); - bridge_into!(Box, crate::Pat, $src); + // bridge_into!(Box, crate::Pat, $src); }; } diff --git a/crates/swc_ecma_ast/src/pat.rs b/crates/swc_ecma_ast/src/pat.rs index 3ac5bb07809d..86c40c45828d 100644 --- a/crates/swc_ecma_ast/src/pat.rs +++ b/crates/swc_ecma_ast/src/pat.rs @@ -72,7 +72,7 @@ bridge_pat_from!(BindingIdent, Id); macro_rules! pat_to_other { ($T:ty) => { bridge_from!(crate::Param, crate::Pat, $T); - bridge_into!(Box, crate::Pat, $T); + // bridge_into!(Box, crate::Pat, $T); }; } From ad7e3c42cc0dfd846ebf0e218f28e97f119e32b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 03:41:01 +0900 Subject: [PATCH 59/65] boxed() --- crates/swc_allocator/src/boxed/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/swc_allocator/src/boxed/mod.rs b/crates/swc_allocator/src/boxed/mod.rs index ae1cc750bd61..f3f4f2a3230b 100644 --- a/crates/swc_allocator/src/boxed/mod.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -23,6 +23,20 @@ impl From for Box { } } +pub trait IntoBox: Sized { + fn boxed(self) -> Box; +} + +impl IntoBox for V +where + V: Into, +{ + #[inline(always)] + fn boxed(self) -> Box { + Box::new(self.into()) + } +} + impl Default for Box where T: Default, From c69accc910208df6215ae147d3136ac8539d7324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 03:41:44 +0900 Subject: [PATCH 60/65] Fix AST --- crates/swc_ecma_ast/src/ident.rs | 2 +- crates/swc_ecma_ast/src/stmt.rs | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/crates/swc_ecma_ast/src/ident.rs b/crates/swc_ecma_ast/src/ident.rs index e2057232ceb9..cc286fb10cfc 100644 --- a/crates/swc_ecma_ast/src/ident.rs +++ b/crates/swc_ecma_ast/src/ident.rs @@ -5,7 +5,7 @@ use std::{ }; use phf::phf_set; -use swc_allocator::{boxed::Box, vec::Vec}; +use swc_allocator::boxed::Box; use swc_atoms::{js_word, Atom}; use swc_common::{ ast_node, util::take::Take, BytePos, EqIgnoreSpan, Mark, Span, Spanned, SyntaxContext, DUMMY_SP, diff --git a/crates/swc_ecma_ast/src/stmt.rs b/crates/swc_ecma_ast/src/stmt.rs index b532eafd8229..22ba3d6f23d4 100644 --- a/crates/swc_ecma_ast/src/stmt.rs +++ b/crates/swc_ecma_ast/src/stmt.rs @@ -24,11 +24,7 @@ pub struct BlockStmt { impl Take for BlockStmt { fn dummy() -> Self { - BlockStmt { - span: DUMMY_SP, - stmts: vec![], - ctxt: Default::default(), - } + Default::default() } } From 1f29c7524016f5a57c1eec5b4f19c0ee7156fc31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 03:42:42 +0900 Subject: [PATCH 61/65] More AST work --- crates/swc_ecma_ast/src/expr.rs | 2 +- crates/swc_ecma_ast/src/macros.rs | 12 ------------ crates/swc_ecma_ast/src/prop.rs | 11 +++++++---- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index 0ffd98b51922..440e7f2232c8 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -437,7 +437,7 @@ bridge_expr_from!(ClassExpr, Class); macro_rules! boxed_expr { ($T:ty) => { - bridge_into!(Box, Expr, $T); + bridge_from!(Box, Expr, $T); }; } diff --git a/crates/swc_ecma_ast/src/macros.rs b/crates/swc_ecma_ast/src/macros.rs index 74cd9262b8e0..eafd85239e30 100644 --- a/crates/swc_ecma_ast/src/macros.rs +++ b/crates/swc_ecma_ast/src/macros.rs @@ -214,18 +214,6 @@ macro_rules! bridge_from { }; } -macro_rules! bridge_into { - ($dst:ty, $bridge:ty, $src:ty) => { - impl Into<$dst> for $src { - #[cfg_attr(not(debug_assertions), inline(always))] - fn into(self) -> $dst { - let src: $bridge = self.into(); - src.into() - } - } - }; -} - macro_rules! bridge_expr_from { ($bridge:ty, $src:ty) => { bridge_from!(crate::Expr, $bridge, $src); diff --git a/crates/swc_ecma_ast/src/prop.rs b/crates/swc_ecma_ast/src/prop.rs index 1b303c9fd4af..2bf90e754dfb 100644 --- a/crates/swc_ecma_ast/src/prop.rs +++ b/crates/swc_ecma_ast/src/prop.rs @@ -1,5 +1,8 @@ use is_macro::Is; -use swc_allocator::{boxed::Box, vec::Vec}; +use swc_allocator::{ + boxed::{Box, IntoBox}, + vec::Vec, +}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; use crate::{ @@ -133,15 +136,15 @@ impl From for MemberProp { PropName::Computed(p) => MemberProp::Computed(p), PropName::Str(p) => MemberProp::Computed(ComputedPropName { span: DUMMY_SP, - expr: p.into(), + expr: p.boxed(), }), PropName::Num(p) => MemberProp::Computed(ComputedPropName { span: DUMMY_SP, - expr: p.into(), + expr: p.boxed(), }), PropName::BigInt(p) => MemberProp::Computed(ComputedPropName { span: DUMMY_SP, - expr: p.into(), + expr: p.boxed(), }), } } From 8d7bd1388577415d93e05f1852fa7d2312316633 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 10:19:05 +0900 Subject: [PATCH 62/65] Rename --- crates/swc_allocator/src/alloc.rs | 10 ++++++---- crates/swc_allocator/src/boxed/mod.rs | 8 ++++---- crates/swc_allocator/src/vec/mod.rs | 10 +++++----- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 42198d8e01b3..9326f1db3e6d 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -6,12 +6,14 @@ use crate::Allocator; scoped_thread_local!(pub(crate) static ALLOC: Allocator); +pub(crate) struct SwcAlloc {} + #[derive(Clone, Copy)] -pub struct SwcAlloc { +pub struct CachedAlloc { pub(crate) is_custom: bool, } -impl Default for SwcAlloc { +impl Default for CachedAlloc { fn default() -> Self { Self { is_custom: ALLOC.is_set(), @@ -19,7 +21,7 @@ impl Default for SwcAlloc { } } -impl SwcAlloc { +impl CachedAlloc { /// `true` is passed to `f` if the box is allocated with a custom allocator. fn with_allocator(&self, f: impl FnOnce(&dyn allocator_api2::alloc::Allocator) -> T) -> T { if self.is_custom { @@ -33,7 +35,7 @@ impl SwcAlloc { } } -unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { +unsafe impl allocator_api2::alloc::Allocator for CachedAlloc { fn allocate(&self, layout: Layout) -> Result, allocator_api2::alloc::AllocError> { self.with_allocator(|a| a.allocate(layout)) } diff --git a/crates/swc_allocator/src/boxed/mod.rs b/crates/swc_allocator/src/boxed/mod.rs index f3f4f2a3230b..2c6d67975ccf 100644 --- a/crates/swc_allocator/src/boxed/mod.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -3,7 +3,7 @@ use std::ops::{Deref, DerefMut}; mod rkyv; mod serde; -use crate::alloc::SwcAlloc; +use crate::alloc::CachedAlloc; /// A special `Box` which has size of [`std::boxed::Box`] but **may** be /// allocated with a custom allocator. @@ -14,7 +14,7 @@ use crate::alloc::SwcAlloc; /// The last bit is 1 if the box is allocated with a custom allocator. #[repr(transparent)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Box(pub(crate) allocator_api2::boxed::Box); +pub struct Box(pub(crate) allocator_api2::boxed::Box); impl From for Box { #[inline(always)] @@ -51,7 +51,7 @@ impl Box { pub fn new(value: T) -> Self { Self(allocator_api2::boxed::Box::new_in( value, - SwcAlloc::default(), + CachedAlloc::default(), )) } @@ -64,7 +64,7 @@ impl Box { pub unsafe fn from_raw(raw: *mut T) -> Self { Self(allocator_api2::boxed::Box::from_raw_in( raw, - SwcAlloc::default(), + CachedAlloc::default(), )) } } diff --git a/crates/swc_allocator/src/vec/mod.rs b/crates/swc_allocator/src/vec/mod.rs index f3825c15880c..fb190a33474c 100644 --- a/crates/swc_allocator/src/vec/mod.rs +++ b/crates/swc_allocator/src/vec/mod.rs @@ -3,11 +3,11 @@ use std::ops::{Deref, DerefMut}; use rkyv::{vec::ArchivedVec, DeserializeUnsized}; use serde_derive::{Deserialize, Serialize}; -use crate::{alloc::SwcAlloc, boxed::Box}; +use crate::{alloc::CachedAlloc, boxed::Box}; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[repr(transparent)] -pub struct Vec(allocator_api2::vec::Vec); +pub struct Vec(allocator_api2::vec::Vec); impl Vec { pub fn new() -> Self { @@ -16,7 +16,7 @@ impl Vec { } impl Deref for Vec { - type Target = allocator_api2::vec::Vec; + type Target = allocator_api2::vec::Vec; fn deref(&self) -> &Self::Target { &self.0 @@ -31,12 +31,12 @@ impl DerefMut for Vec { impl Default for Vec { fn default() -> Self { - Self(allocator_api2::vec::Vec::new_in(SwcAlloc::default())) + Self(allocator_api2::vec::Vec::new_in(CachedAlloc::default())) } } impl IntoIterator for Vec { - type IntoIter = allocator_api2::vec::IntoIter; + type IntoIter = allocator_api2::vec::IntoIter; type Item = T; fn into_iter(self) -> Self::IntoIter { From 71403bc772d5e8bf17d4d4aedbba35951eb6a519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 10:24:23 +0900 Subject: [PATCH 63/65] SwcAlloc --- crates/swc_allocator/src/alloc.rs | 31 ++++++++++----------------- crates/swc_allocator/src/boxed/mod.rs | 8 +++---- crates/swc_allocator/src/vec/mod.rs | 10 ++++----- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 9326f1db3e6d..2d51e4e77474 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -6,36 +6,27 @@ use crate::Allocator; scoped_thread_local!(pub(crate) static ALLOC: Allocator); -pub(crate) struct SwcAlloc {} +#[derive(Debug, Clone, Copy, Default)] +pub struct SwcAlloc; -#[derive(Clone, Copy)] -pub struct CachedAlloc { - pub(crate) is_custom: bool, -} - -impl Default for CachedAlloc { - fn default() -> Self { - Self { - is_custom: ALLOC.is_set(), - } - } -} - -impl CachedAlloc { +impl SwcAlloc { /// `true` is passed to `f` if the box is allocated with a custom allocator. - fn with_allocator(&self, f: impl FnOnce(&dyn allocator_api2::alloc::Allocator) -> T) -> T { - if self.is_custom { + fn with_allocator( + &self, + f: impl FnOnce(&dyn allocator_api2::alloc::Allocator, bool) -> T, + ) -> T { + if ALLOC.is_set() { ALLOC.with(|a| { // - f(&&**a as &dyn allocator_api2::alloc::Allocator) + f(&&**a as &dyn allocator_api2::alloc::Allocator, true) }) } else { - f(&allocator_api2::alloc::Global) + f(&allocator_api2::alloc::Global, false) } } } -unsafe impl allocator_api2::alloc::Allocator for CachedAlloc { +unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { fn allocate(&self, layout: Layout) -> Result, allocator_api2::alloc::AllocError> { self.with_allocator(|a| a.allocate(layout)) } diff --git a/crates/swc_allocator/src/boxed/mod.rs b/crates/swc_allocator/src/boxed/mod.rs index 2c6d67975ccf..d5dc3fe0736b 100644 --- a/crates/swc_allocator/src/boxed/mod.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -1,10 +1,10 @@ use std::ops::{Deref, DerefMut}; +use crate::alloc::SwcAlloc; + mod rkyv; mod serde; -use crate::alloc::CachedAlloc; - /// A special `Box` which has size of [`std::boxed::Box`] but **may** be /// allocated with a custom allocator. /// @@ -14,7 +14,7 @@ use crate::alloc::CachedAlloc; /// The last bit is 1 if the box is allocated with a custom allocator. #[repr(transparent)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Box(pub(crate) allocator_api2::boxed::Box); +pub struct Box(pub(crate) allocator_api2::boxed::Box); impl From for Box { #[inline(always)] @@ -64,7 +64,7 @@ impl Box { pub unsafe fn from_raw(raw: *mut T) -> Self { Self(allocator_api2::boxed::Box::from_raw_in( raw, - CachedAlloc::default(), + SwcAlloc::default(), )) } } diff --git a/crates/swc_allocator/src/vec/mod.rs b/crates/swc_allocator/src/vec/mod.rs index fb190a33474c..f3825c15880c 100644 --- a/crates/swc_allocator/src/vec/mod.rs +++ b/crates/swc_allocator/src/vec/mod.rs @@ -3,11 +3,11 @@ use std::ops::{Deref, DerefMut}; use rkyv::{vec::ArchivedVec, DeserializeUnsized}; use serde_derive::{Deserialize, Serialize}; -use crate::{alloc::CachedAlloc, boxed::Box}; +use crate::{alloc::SwcAlloc, boxed::Box}; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[repr(transparent)] -pub struct Vec(allocator_api2::vec::Vec); +pub struct Vec(allocator_api2::vec::Vec); impl Vec { pub fn new() -> Self { @@ -16,7 +16,7 @@ impl Vec { } impl Deref for Vec { - type Target = allocator_api2::vec::Vec; + type Target = allocator_api2::vec::Vec; fn deref(&self) -> &Self::Target { &self.0 @@ -31,12 +31,12 @@ impl DerefMut for Vec { impl Default for Vec { fn default() -> Self { - Self(allocator_api2::vec::Vec::new_in(CachedAlloc::default())) + Self(allocator_api2::vec::Vec::new_in(SwcAlloc::default())) } } impl IntoIterator for Vec { - type IntoIter = allocator_api2::vec::IntoIter; + type IntoIter = allocator_api2::vec::IntoIter; type Item = T; fn into_iter(self) -> Self::IntoIter { From 04dfd41b0846daa46e1abb661201292b192d872f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 10:39:43 +0900 Subject: [PATCH 64/65] Some work --- crates/swc_allocator/src/alloc.rs | 92 +++++++++++++++++++++++++-- crates/swc_allocator/src/boxed/mod.rs | 5 +- 2 files changed, 87 insertions(+), 10 deletions(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 2d51e4e77474..936223380b0d 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -1,5 +1,6 @@ use std::{alloc::Layout, ptr::NonNull}; +use allocator_api2::alloc::Global; use scoped_tls::scoped_thread_local; use crate::Allocator; @@ -26,20 +27,61 @@ impl SwcAlloc { } } +/// Set the last bit to 1 +fn mark_ptr_as_arena_mode(ptr: NonNull<[u8]>) -> NonNull<[u8]> { + let ptr = ptr.as_ptr() as *mut () as usize | 1; + unsafe { NonNull::new_unchecked(ptr as *mut [u8]) } +} + +fn is_ptr_in_arena_mode(ptr: NonNull) -> bool { + let ptr = ptr.as_ptr() as usize; + ptr & 1 == 1 +} + unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { fn allocate(&self, layout: Layout) -> Result, allocator_api2::alloc::AllocError> { - self.with_allocator(|a| a.allocate(layout)) + self.with_allocator(|a, is_arena_mode| { + let ptr = a.allocate(layout)?; + + if is_arena_mode { + Ok(mark_ptr_as_arena_mode(ptr)) + } else { + Ok(ptr) + } + }) } fn allocate_zeroed( &self, layout: Layout, ) -> Result, allocator_api2::alloc::AllocError> { - self.with_allocator(|a| a.allocate_zeroed(layout)) + self.with_allocator(|a, is_arena_mode| { + let ptr = a.allocate_zeroed(layout)?; + + if is_arena_mode { + Ok(mark_ptr_as_arena_mode(ptr)) + } else { + Ok(ptr) + } + }) } unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { - self.with_allocator(|a| a.deallocate(ptr, layout)) + if is_ptr_in_arena_mode(ptr) { + debug_assert!( + ALLOC.is_set(), + "Deallocating a pointer allocated with arena mode with a non-arena mode allocator" + ); + + ALLOC.with(|alloc| { + unsafe { + // Safety: We are in unsafe fn + (&**alloc).deallocate(ptr, layout) + } + }) + } else { + Global.deallocate(ptr, layout) + } } unsafe fn grow( @@ -48,7 +90,15 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { old_layout: Layout, new_layout: Layout, ) -> Result, allocator_api2::alloc::AllocError> { - self.with_allocator(|a| a.grow(ptr, old_layout, new_layout)) + self.with_allocator(|a, is_arena_mode| { + let ptr = a.grow(ptr, old_layout, new_layout)?; + + if is_arena_mode { + Ok(mark_ptr_as_arena_mode(ptr)) + } else { + Ok(ptr) + } + }) } unsafe fn grow_zeroed( @@ -57,7 +107,22 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { old_layout: Layout, new_layout: Layout, ) -> Result, allocator_api2::alloc::AllocError> { - self.with_allocator(|a| a.grow_zeroed(ptr, old_layout, new_layout)) + self.with_allocator(|a, is_arena_mode| { + if cfg!(debug_assertions) && is_ptr_in_arena_mode(ptr) { + debug_assert!( + is_arena_mode, + "Growing a pointer allocated with arena mode with a non-arena mode allocator" + ); + } + + let ptr = a.grow_zeroed(ptr, old_layout, new_layout)?; + + if is_arena_mode { + Ok(mark_ptr_as_arena_mode(ptr)) + } else { + Ok(ptr) + } + }) } unsafe fn shrink( @@ -66,7 +131,22 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { old_layout: Layout, new_layout: Layout, ) -> Result, allocator_api2::alloc::AllocError> { - self.with_allocator(|a| a.shrink(ptr, old_layout, new_layout)) + self.with_allocator(|a, is_arena_mode| { + if cfg!(debug_assertions) && is_ptr_in_arena_mode(ptr) { + debug_assert!( + is_arena_mode, + "Shrinking a pointer allocated with arena mode with a non-arena mode allocator" + ); + } + + let ptr = a.shrink(ptr, old_layout, new_layout)?; + + if is_arena_mode { + Ok(mark_ptr_as_arena_mode(ptr)) + } else { + Ok(ptr) + } + }) } fn by_ref(&self) -> &Self diff --git a/crates/swc_allocator/src/boxed/mod.rs b/crates/swc_allocator/src/boxed/mod.rs index d5dc3fe0736b..78c5fc68c045 100644 --- a/crates/swc_allocator/src/boxed/mod.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -49,10 +49,7 @@ where impl Box { #[inline(always)] pub fn new(value: T) -> Self { - Self(allocator_api2::boxed::Box::new_in( - value, - CachedAlloc::default(), - )) + Self(allocator_api2::boxed::Box::new_in(value, SwcAlloc)) } pub fn unbox(self) -> T { From 8a4fbef6fa670ca0fbded34d0846417f1c9810f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sun, 14 Jul 2024 10:41:46 +0900 Subject: [PATCH 65/65] allcator trait --- crates/swc_allocator/src/alloc.rs | 71 ++++++++++++++----------------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 936223380b0d..f5109002368d 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -90,15 +90,18 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { old_layout: Layout, new_layout: Layout, ) -> Result, allocator_api2::alloc::AllocError> { - self.with_allocator(|a, is_arena_mode| { - let ptr = a.grow(ptr, old_layout, new_layout)?; + if is_ptr_in_arena_mode(ptr) { + debug_assert!( + ALLOC.is_set(), + "Growing a pointer allocated with arena mode with a non-arena mode allocator" + ); - if is_arena_mode { - Ok(mark_ptr_as_arena_mode(ptr)) - } else { - Ok(ptr) - } - }) + ALLOC.with(|alloc| { + return (&**alloc).grow(ptr, old_layout, new_layout); + }) + } else { + Global.grow(ptr, old_layout, new_layout) + } } unsafe fn grow_zeroed( @@ -107,22 +110,18 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { old_layout: Layout, new_layout: Layout, ) -> Result, allocator_api2::alloc::AllocError> { - self.with_allocator(|a, is_arena_mode| { - if cfg!(debug_assertions) && is_ptr_in_arena_mode(ptr) { - debug_assert!( - is_arena_mode, - "Growing a pointer allocated with arena mode with a non-arena mode allocator" - ); - } - - let ptr = a.grow_zeroed(ptr, old_layout, new_layout)?; + if is_ptr_in_arena_mode(ptr) { + debug_assert!( + ALLOC.is_set(), + "Growing a pointer allocated with arena mode with a non-arena mode allocator" + ); - if is_arena_mode { - Ok(mark_ptr_as_arena_mode(ptr)) - } else { - Ok(ptr) - } - }) + ALLOC.with(|alloc| { + return (&**alloc).grow_zeroed(ptr, old_layout, new_layout); + }) + } else { + Global.grow_zeroed(ptr, old_layout, new_layout) + } } unsafe fn shrink( @@ -131,22 +130,18 @@ unsafe impl allocator_api2::alloc::Allocator for SwcAlloc { old_layout: Layout, new_layout: Layout, ) -> Result, allocator_api2::alloc::AllocError> { - self.with_allocator(|a, is_arena_mode| { - if cfg!(debug_assertions) && is_ptr_in_arena_mode(ptr) { - debug_assert!( - is_arena_mode, - "Shrinking a pointer allocated with arena mode with a non-arena mode allocator" - ); - } - - let ptr = a.shrink(ptr, old_layout, new_layout)?; + if is_ptr_in_arena_mode(ptr) { + debug_assert!( + ALLOC.is_set(), + "Shrinking a pointer allocated with arena mode with a non-arena mode allocator" + ); - if is_arena_mode { - Ok(mark_ptr_as_arena_mode(ptr)) - } else { - Ok(ptr) - } - }) + ALLOC.with(|alloc| { + return (&**alloc).shrink(ptr, old_layout, new_layout); + }) + } else { + Global.shrink(ptr, old_layout, new_layout) + } } fn by_ref(&self) -> &Self