Skip to content

Commit

Permalink
feat(allocator): Add a cargo feature (#9239)
Browse files Browse the repository at this point in the history
**Description:**

This PR is a part of #9230
  • Loading branch information
kdy1 committed Jul 15, 2024
1 parent b6c0468 commit 398dc21
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 2,581 deletions.
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" }
Original file line number Diff line number Diff line change
@@ -1,53 +1,82 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`transform in strip-only mode should remove declare enum 1`] = `" "`;
exports[`transform in strip-only mode should remove declare enum 1`] = `
{
"code": " ",
"map": undefined,
}
`;

exports[`transform in strip-only mode should remove declare enum 2`] = `
"
{
"code": "
"
",
"map": undefined,
}
`;

exports[`transform in strip-only mode should remove declare enum 3`] = `
"
{
"code": "
"
",
"map": undefined,
}
`;

exports[`transform in strip-only mode should strip complex expressions 1`] = `
"const foo = {
{
"code": "const foo = {
foo: 1 ,
bar: "bar" ,
} ;
const bar = "bar";"
const bar = "bar";",
"map": undefined,
}
`;

exports[`transform in strip-only mode should strip nonnull assertions 1`] = `
"const foo = 1 ;
const bar = "bar";"
{
"code": "const foo = 1 ;
const bar = "bar";",
"map": undefined,
}
`;

exports[`transform in strip-only mode should strip satisfies 1`] = `
"const foo = 1 ;
const bar = "bar";"
{
"code": "const foo = 1 ;
const bar = "bar";",
"map": undefined,
}
`;

exports[`transform in strip-only mode should strip type annotations 1`] = `
"const foo = 1;
const bar = "bar";"
{
"code": "const foo = 1;
const bar = "bar";",
"map": undefined,
}
`;

exports[`transform in strip-only mode should strip type assertions 1`] = `
"const foo = 1 ;
const bar = "bar";"
{
"code": "const foo = 1 ;
const bar = "bar";",
"map": undefined,
}
`;

exports[`transform in strip-only mode should strip type declarations 1`] = `
"const foo = 1;
{
"code": "const foo = 1;
const bar = "bar";"
const bar = "bar";",
"map": undefined,
}
`;

exports[`transform in strip-only mode should throw an error when it encounters a module 1`] = `
Expand Down Expand Up @@ -78,8 +107,11 @@ exports[`transform in strip-only mode should throw an error when it encounters a
`;

exports[`transform should strip types 1`] = `
"
{
"code": "
export const foo = 1;
"
",
"map": undefined,
}
`;
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
}
}
Loading

0 comments on commit 398dc21

Please sign in to comment.