Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(allocator): Add a cargo feature #9239

Merged
merged 25 commits into from
Jul 15, 2024
Merged
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions bindings/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bindings/binding_typescript_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ serde-wasm-bindgen = "0.4.5"
serde_json = "1.0.120"
swc_common = "0.35.0"
swc_error_reporters = "0.19.0"
swc_fast_ts_strip = "0.2.0"
swc_fast_ts_strip = "0.2.1"
tracing = { version = "0.1.37", features = ["max_level_off"] }
wasm-bindgen = { version = "0.2.82", features = ["enable-interning"] }
wasm-bindgen-futures = { version = "0.4.41" }
4 changes: 2 additions & 2 deletions bindings/binding_typescript_wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Error;
use swc_common::{errors::ColorConfig, sync::Lrc, SourceMap, GLOBALS};
use swc_error_reporters::handler::{try_with_handler, HandlerOpts};
use swc_fast_ts_strip::Options;
use swc_fast_ts_strip::{Options, TransformOutput};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::{
future_to_promise,
Expand Down Expand Up @@ -34,7 +34,7 @@ pub fn transform_sync(input: JsString, options: JsValue) -> Result<JsValue, JsVa
Ok(serde_wasm_bindgen::to_value(&result)?)
}

fn operate(input: String, options: Options) -> Result<String, Error> {
fn operate(input: String, options: Options) -> Result<TransformOutput, Error> {
let cm = Lrc::new(SourceMap::default());

try_with_handler(
Expand Down
21 changes: 14 additions & 7 deletions crates/swc_allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ name = "swc_allocator"
repository = { workspace = true }
version = "0.1.3"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]


[features]
rkyv = ["dep:rkyv"]
serde = ["dep:serde", "dep:serde_derive"]
rkyv = ["dep:rkyv"]
scoped = []
serde = ["dep:serde", "dep:serde_derive"]

[dependencies]
allocator-api2 = { workspace = true, features = ["serde"] }
bumpalo = { workspace = true, features = [
"allocator-api2",
"boxed",
"collections",
"allocator-api2",
"boxed",
"collections",
] }
ptr_meta = { workspace = true }
rkyv = { workspace = true, optional = true }
Expand All @@ -34,5 +40,6 @@ swc_malloc = { version = "0.5.10", path = "../swc_malloc" }


[[bench]]
harness = false
name = "bench"
features = ["scoped"]
harness = false
name = "bench"
30 changes: 13 additions & 17 deletions crates/swc_allocator/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate swc_malloc;

use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use swc_allocator::{FastAlloc, SwcAllocator};
use swc_allocator::{boxed::Box as SwcBox, vec::Vec as SwcVec, Allocator, FastAlloc};

fn bench_alloc(c: &mut Criterion) {
fn direct_alloc_std(b: &mut Bencher, times: usize) {
Expand All @@ -16,38 +16,35 @@ fn bench_alloc(c: &mut Criterion) {

fn direct_alloc_no_scope(b: &mut Bencher, times: usize) {
b.iter(|| {
let mut vec = swc_allocator::vec::Vec::new();
let mut vec = SwcVec::new();
for i in 0..times {
let item: swc_allocator::boxed::Box<usize> =
black_box(swc_allocator::boxed::Box::new(black_box(i)));
let item: SwcBox<usize> = black_box(SwcBox::new(black_box(i)));
vec.push(item);
}
})
}

fn fast_alloc_no_scope(b: &mut Bencher, times: usize) {
b.iter(|| {
let allocator = FastAlloc::default();
let alloc = FastAlloc::default();

let mut vec = allocator.vec();
let mut vec = SwcVec::new_in(alloc);
for i in 0..times {
let item: swc_allocator::boxed::Box<usize> =
black_box(allocator.alloc(black_box(i)));
let item: SwcBox<usize> = black_box(SwcBox::new_in(black_box(i), alloc));
vec.push(item);
}
})
}

fn direct_alloc_scoped(b: &mut Bencher, times: usize) {
b.iter(|| {
let allocator = SwcAllocator::default();
let allocator = Allocator::default();

allocator.scope(|| {
let mut vec = swc_allocator::vec::Vec::new();
let mut vec = SwcVec::new();

for i in 0..times {
let item: swc_allocator::boxed::Box<usize> =
black_box(swc_allocator::boxed::Box::new(black_box(i)));
let item: SwcBox<usize> = black_box(SwcBox::new(black_box(i)));
vec.push(item);
}
});
Expand All @@ -56,14 +53,13 @@ fn bench_alloc(c: &mut Criterion) {

fn fast_alloc_scoped(b: &mut Bencher, times: usize) {
b.iter(|| {
SwcAllocator::default().scope(|| {
let allocator = FastAlloc::default();
Allocator::default().scope(|| {
let alloc = FastAlloc::default();

let mut vec = allocator.vec();
let mut vec = SwcVec::new_in(alloc);

for i in 0..times {
let item: swc_allocator::boxed::Box<usize> =
black_box(allocator.alloc(black_box(i)));
let item: SwcBox<usize> = black_box(SwcBox::new_in(black_box(i), alloc));
vec.push(item);
}
});
Expand Down
47 changes: 40 additions & 7 deletions crates/swc_allocator/src/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
use std::{alloc::Layout, cell::Cell, mem::transmute, ptr::NonNull};
use std::{
alloc::Layout,
cell::Cell,
mem::transmute,
ops::{Deref, DerefMut},
ptr::NonNull,
};

use allocator_api2::alloc::Global;
use bumpalo::Bump;

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

thread_local! {
static ALLOC: Cell<Option<&'static SwcAllocator>> = const { Cell::new(None) };
static ALLOC: Cell<Option<&'static Allocator>> = const { Cell::new(None) };
}

/// The actual storage for [FastAlloc].
#[derive(Default)]
pub struct SwcAllocator(MemorySpace);
pub struct Allocator {
alloc: Bump,
}

impl SwcAllocator {
impl Allocator {
/// Invokes `f` in a scope where the allocations are done in this allocator.
#[inline(always)]
pub fn scope<'a, F, R>(&'a self, f: F) -> R
Expand All @@ -20,7 +30,7 @@ impl SwcAllocator {
{
let s = unsafe {
// Safery: We are using a scoped API
transmute::<&'a SwcAllocator, &'static SwcAllocator>(self)
transmute::<&'a Allocator, &'static Allocator>(self)
};

ALLOC.set(Some(s));
Expand Down Expand Up @@ -49,7 +59,10 @@ impl FastAlloc {
f: impl FnOnce(&dyn allocator_api2::alloc::Allocator, bool) -> T,
) -> T {
if let Some(arena) = &self.alloc {
f((&&*arena.0) as &dyn allocator_api2::alloc::Allocator, true)
f(
(&&arena.alloc) as &dyn allocator_api2::alloc::Allocator,
true,
)
} else {
f(&allocator_api2::alloc::Global, false)
}
Expand Down Expand Up @@ -159,3 +172,23 @@ unsafe impl allocator_api2::alloc::Allocator for FastAlloc {
self
}
}

impl From<Bump> 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
}
}
51 changes: 32 additions & 19 deletions crates/swc_allocator/src/boxed/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Faster box type.

use std::{
borrow::{Borrow, BorrowMut},
fmt::{self, Debug, Display, Formatter},
Expand All @@ -14,13 +16,7 @@ mod rkyv;
#[cfg(feature = "serde")]
mod serde;

/// 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.
/// Faster alterantive for [`std::boxed::Box`].
#[repr(transparent)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Box<T: ?Sized>(pub(crate) allocator_api2::boxed::Box<T, FastAlloc>);
Expand Down Expand Up @@ -49,15 +45,38 @@ where
}

impl<T> Box<T> {
/// Allocates a new box with `value`.
/// Allocates memory on the heap and then places `x` into it.
///
/// This doesn't actually allocate if `T` is zero-sized.
///
/// # Examples
///
/// ```
/// let five = Box::new(5);
/// ```
///
/// See [`std::boxed::Box::new`].
/// Note: This is slower than using [Self::new_in] with cached [FastAlloc].
#[inline(always)]
pub fn new(value: T) -> Self {
Self(allocator_api2::boxed::Box::new_in(
value,
FastAlloc::default(),
))
Self::new_in(value, Default::default())
}

/// Allocates memory in the given allocator then places `x` into it.
///
/// This doesn't actually allocate if `T` is zero-sized.
///
/// # Examples
///
/// ```
/// #![feature(allocator_api)]
///
/// use std::alloc::System;
///
/// let five = Box::new_in(5, System);
/// ```
#[inline(always)]
pub fn new_in(value: T, alloc: FastAlloc) -> Self {
Self(allocator_api2::boxed::Box::new_in(value, alloc))
}

/// Moves the value out of the box.
Expand Down Expand Up @@ -627,9 +646,3 @@ where
self.0.len()
}
}

impl FastAlloc {
pub fn alloc<T>(&self, t: T) -> Box<T> {
Box(allocator_api2::boxed::Box::new_in(t, self.clone()))
}
}
57 changes: 25 additions & 32 deletions crates/swc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,36 @@
//! API designed after [`oxc_allocator`](https://github.com/oxc-project/oxc/tree/725571aad193ec6ba779c820baeb4a7774533ed7/crates/oxc_allocator/src).

#![allow(clippy::needless_doctest_main)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(missing_docs)]

use std::ops::{Deref, DerefMut};

use bumpalo::Bump;

pub use crate::alloc::SwcAllocator;
pub use crate::alloc::Allocator;

mod alloc;
pub mod boxed;
pub mod vec;

#[derive(Clone)]
/// Fast allocator, effectively working as a cache.
///
/// This type implements [Default] and [Copy]. This type is intended to stored
/// in a variable or a field in a struct before allocating code, and used as the
/// seocnd argument in [crate::boxed::Box::new_in] and
/// [crate::vec::Vec::new_in].
///
/// [crate::boxed::Box::new] and [crate::vec::Vec::new] are slower than using
/// this field because they use [FastAlloc::default] internally, which is slower
/// than store [FastAlloc] in a variable.
///
///
///
/// # Misc
///
/// It implements [`allocator_api2::alloc::Allocator`]. So it can be used as the
/// second argument for [`allocator_api2::boxed::Box`] and
/// [`allocator_api2::vec::Vec`]. But you should prefer using
/// [`crate::boxed::Box`] and [`crate::vec::Vec`], which is a wrapper around the
/// original types.
#[derive(Clone, Copy)]
pub struct FastAlloc {
alloc: Option<&'static SwcAllocator>,
}

#[derive(Default)]
struct MemorySpace {
alloc: Bump,
}

impl From<Bump> for MemorySpace {
fn from(alloc: Bump) -> Self {
Self { alloc }
}
}

impl Deref for MemorySpace {
type Target = Bump;

fn deref(&self) -> &Bump {
&self.alloc
}
}

impl DerefMut for MemorySpace {
fn deref_mut(&mut self) -> &mut Bump {
&mut self.alloc
}
alloc: Option<&'static Allocator>,
}
Loading
Loading