From 384f65afe780d3351c0e013cde215f714f438066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 10 Jul 2024 18:47:14 +0900 Subject: [PATCH] feat(allocator): Initialize package (#9195) --- Cargo.lock | 11 ++- Cargo.toml | 2 + crates/swc_allocator/Cargo.toml | 12 +++ crates/swc_allocator/src/lib.rs | 140 ++++++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 crates/swc_allocator/Cargo.toml create mode 100644 crates/swc_allocator/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index d5fbccc414bf3..585de4a0cae21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -403,9 +403,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.14.0" +version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[package]] name = "bytecheck" @@ -3684,6 +3684,13 @@ dependencies = [ "walkdir", ] +[[package]] +name = "swc_allocator" +version = "0.1.0" +dependencies = [ + "bumpalo", +] + [[package]] name = "swc_atoms" version = "0.6.7" diff --git a/Cargo.toml b/Cargo.toml index f08863ec8b736..66a445736df2a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "xtask", + "crates/swc_allocator", "crates/swc_core", "crates/swc_cli_impl", "crates/dbg-swc", @@ -51,6 +52,7 @@ resolver = "2" base64 = "0.21.0" bitflags = "2.5.0" browserslist-rs = "0.16.0" + bumpalo = "3.16.0" cfg-if = "1.0.0" chrono = "0.4.38" codspeed-criterion-compat = "2.6.0" diff --git a/crates/swc_allocator/Cargo.toml b/crates/swc_allocator/Cargo.toml new file mode 100644 index 0000000000000..3355b6e8e306d --- /dev/null +++ b/crates/swc_allocator/Cargo.toml @@ -0,0 +1,12 @@ +[package] +authors = ["강동윤 "] +description = "A thin wrapper for bumpalo" +documentation = "https://rustdoc.swc.rs/swc_allocator/" +edition = "2021" +license = "Apache-2.0" +name = "swc_allocator" +repository = { workspace = true } +version = "0.1.0" + +[dependencies] +bumpalo = { workspace = true, features = ["boxed", "collections"] } diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs new file mode 100644 index 0000000000000..8ed0238693aa9 --- /dev/null +++ b/crates/swc_allocator/src/lib.rs @@ -0,0 +1,140 @@ +use std::ops::{Deref, DerefMut}; + +use bumpalo::Bump; + +#[derive(Default)] +pub struct Allocator { + alloc: Bump, +} + +impl From for Allocator { + fn from(alloc: Bump) -> Self { + Self { alloc } + } +} + +impl Deref for Allocator { + type Target = Bump; + + fn deref(&self) -> &Bump { + &self.alloc + } +} + +impl DerefMut for Allocator { + fn deref_mut(&mut self) -> &mut Bump { + &mut self.alloc + } +} + +#[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>); + +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)) + } +} + +impl<'alloc, T> Deref for Vec<'alloc, T> { + type Target = [T]; + + fn deref(&self) -> &[T] { + &self.0 + } +} + +impl<'alloc, T> DerefMut for Vec<'alloc, T> { + 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>; + type Item = T; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct String<'alloc>(bumpalo::collections::String<'alloc>); + +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, + } + } +}