Skip to content

Commit

Permalink
feat(allocator): Feature gate nightly via macros (#9274)
Browse files Browse the repository at this point in the history
**Description:**

We can make swc faster without dropping support for stable rustc.
  • Loading branch information
kdy1 committed Jul 18, 2024
1 parent 04d8a36 commit a31fb58
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 67 deletions.
9 changes: 5 additions & 4 deletions crates/swc_allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ version = "0.1.6"

[features]
default = ["scoped"]
nightly = []
rkyv = ["dep:rkyv"]
scoped = []
serde = ["dep:serde", "dep:serde_derive"]
scoped = ["nightly"]
serde = ["dep:serde", "dep:serde_derive", "allocator-api2/serde"]

[dependencies]
allocator-api2 = { workspace = true, features = ["serde"] }
allocator-api2 = { workspace = true }
bumpalo = { workspace = true, features = [
"allocator-api2",
"boxed",
"collections",
"allocator-api2",
] }
ptr_meta = { workspace = true }
rkyv = { workspace = true, optional = true }
Expand Down
26 changes: 8 additions & 18 deletions crates/swc_allocator/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
ptr::NonNull,
};

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

use crate::FastAlloc;
Expand Down Expand Up @@ -66,11 +66,11 @@ impl Default for FastAlloc {

impl FastAlloc {
/// `true` is passed to `f` if the box is allocated with a custom allocator.
#[cfg(feature = "scoped")]
fn with_allocator<T>(
&self,
f: impl FnOnce(&dyn allocator_api2::alloc::Allocator, bool) -> T,
) -> T {
#[cfg(feature = "scoped")]
if let Some(arena) = &self.alloc {
return f(
(&&arena.alloc) as &dyn allocator_api2::alloc::Allocator,
Expand All @@ -80,13 +80,6 @@ impl FastAlloc {

f(&allocator_api2::alloc::Global, false)
}

/// `true` is passed to `f` if the box is allocated with a custom allocator.
#[cfg(not(feature = "scoped"))]
#[inline(always)]
fn with_allocator<T>(&self, f: impl FnOnce(allocator_api2::alloc::Global, bool) -> T) -> T {
f(allocator_api2::alloc::Global, false)
}
}

fn mark_ptr_as_arena_mode(ptr: NonNull<[u8]>) -> NonNull<[u8]> {
Expand All @@ -95,7 +88,7 @@ fn mark_ptr_as_arena_mode(ptr: NonNull<[u8]>) -> NonNull<[u8]> {

unsafe impl allocator_api2::alloc::Allocator for FastAlloc {
#[inline]
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, allocator_api2::alloc::AllocError> {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
self.with_allocator(|a, is_arena_mode| {
let ptr = a.allocate(layout)?;

Expand All @@ -108,10 +101,7 @@ unsafe impl allocator_api2::alloc::Allocator for FastAlloc {
}

#[inline]
fn allocate_zeroed(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, allocator_api2::alloc::AllocError> {
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
self.with_allocator(|a, is_arena_mode| {
let ptr = a.allocate_zeroed(layout)?;

Expand All @@ -131,7 +121,7 @@ unsafe impl allocator_api2::alloc::Allocator for FastAlloc {
return;
}

Global.deallocate(ptr, layout)
allocator_api2::alloc::Global.deallocate(ptr, layout)
}

#[inline]
Expand All @@ -140,7 +130,7 @@ unsafe impl allocator_api2::alloc::Allocator for FastAlloc {
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, allocator_api2::alloc::AllocError> {
) -> Result<NonNull<[u8]>, AllocError> {
self.with_allocator(|alloc, is_arena_mode| {
let ptr = alloc.grow(ptr, old_layout, new_layout)?;

Expand All @@ -158,7 +148,7 @@ unsafe impl allocator_api2::alloc::Allocator for FastAlloc {
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, allocator_api2::alloc::AllocError> {
) -> Result<NonNull<[u8]>, AllocError> {
self.with_allocator(|alloc, is_arena_mode| {
let ptr = alloc.grow_zeroed(ptr, old_layout, new_layout)?;

Expand All @@ -176,7 +166,7 @@ unsafe impl allocator_api2::alloc::Allocator for FastAlloc {
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, allocator_api2::alloc::AllocError> {
) -> Result<NonNull<[u8]>, AllocError> {
self.with_allocator(|alloc, is_arena_mode| {
let ptr = alloc.shrink(ptr, old_layout, new_layout)?;

Expand Down
53 changes: 53 additions & 0 deletions crates/swc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@

#![allow(clippy::needless_doctest_main)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(
feature = "nightly",
feature(allocator_api, fundamental, with_negative_coherence)
)]
#![deny(missing_docs)]
#![allow(clippy::derivable_impls)]

pub use crate::alloc::Allocator;

mod alloc;
#[cfg(feature = "nightly")]
pub mod boxed;
#[cfg(feature = "nightly")]
pub mod vec;

/// Fast allocator, effectively working as a cache.
Expand Down Expand Up @@ -73,3 +79,50 @@ impl FastAlloc {
}
}
}

/// This expands to the given tokens if the `nightly` feature is enabled.
#[cfg(feature = "nightly")]
#[macro_export]
macro_rules! nightly_only {
(
$($item:item)*
) => {
$(
#[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
$item
)*
};
}

/// This expands to the given tokens if the `nightly` feature is enabled.
#[cfg(not(feature = "nightly"))]
#[macro_export]
macro_rules! nightly_only {
(
$($item:item)*
) => {};
}

/// Usage: `swc_allocator::Type!(Vec<T>)` or `swc_allocator::Type!(Box<T>)`.
#[macro_export]
macro_rules! Type {
(Box<$($tt:tt)*>) => {
#[cfg(feature = "nightly")]
$crate::boxed::Box<$crate::Type!($($tt)*)>

#[cfg(not(feature = "nightly"))]
std::boxed::Box<$crate::Type!($($tt)*)>
};

(Vec<$($tt:tt)*>) => {
#[cfg(feature = "nightly")]
$crate::vec::Vec<$crate::Type!($($tt)*)>

#[cfg(not(feature = "nightly"))]
std::vec::Vec<$crate::Type!($($tt)*)>
};

($t:ty) => {
$t
};
}
59 changes: 32 additions & 27 deletions crates/swc_common/src/eq.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{cell::RefCell, rc::Rc, sync::Arc};

use num_bigint::BigInt;
use swc_allocator::nightly_only;

use crate::{BytePos, Span};

Expand Down Expand Up @@ -63,18 +64,20 @@ where
}
}

impl<T> EqIgnoreSpan for swc_allocator::vec::Vec<T>
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))
nightly_only!(
impl<T> EqIgnoreSpan for swc_allocator::vec::Vec<T>
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 {
Expand Down Expand Up @@ -185,25 +188,27 @@ macro_rules! deref {

deref!(Box, Rc, Arc);

impl<N> EqIgnoreSpan for swc_allocator::boxed::Box<N>
where
N: EqIgnoreSpan,
{
#[inline]
fn eq_ignore_span(&self, other: &Self) -> bool {
(**self).eq_ignore_span(&**other)
swc_allocator::nightly_only!(
impl<N> EqIgnoreSpan for swc_allocator::boxed::Box<N>
where
N: EqIgnoreSpan,
{
#[inline]
fn eq_ignore_span(&self, other: &Self) -> bool {
(**self).eq_ignore_span(&**other)
}
}
}

impl<N> TypeEq for swc_allocator::boxed::Box<N>
where
N: TypeEq,
{
#[inline]
fn type_eq(&self, other: &Self) -> bool {
(**self).type_eq(&**other)
impl<N> TypeEq for swc_allocator::boxed::Box<N>
where
N: TypeEq,
{
#[inline]
fn type_eq(&self, other: &Self) -> bool {
(**self).type_eq(&**other)
}
}
}
);

impl<'a, N> EqIgnoreSpan for &'a N
where
Expand Down
16 changes: 9 additions & 7 deletions crates/swc_common/src/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,13 @@ where
}
}

impl<T> Spanned for swc_allocator::boxed::Box<T>
where
T: Spanned,
{
fn span(&self) -> Span {
self.as_ref().span()
swc_allocator::nightly_only!(
impl<T> Spanned for swc_allocator::boxed::Box<T>
where
T: Spanned,
{
fn span(&self) -> Span {
self.as_ref().span()
}
}
}
);
24 changes: 13 additions & 11 deletions crates/swc_common/src/util/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,19 @@ impl Take for Span {
}
}

impl<T> Take for swc_allocator::boxed::Box<T>
where
T: Take,
{
fn dummy() -> Self {
swc_allocator::boxed::Box::new(T::dummy())
swc_allocator::nightly_only!(
impl<T> Take for swc_allocator::boxed::Box<T>
where
T: Take,
{
fn dummy() -> Self {
swc_allocator::boxed::Box::new(T::dummy())
}
}
}

impl<T> Take for swc_allocator::vec::Vec<T> {
fn dummy() -> Self {
Default::default()
impl<T> Take for swc_allocator::vec::Vec<T> {
fn dummy() -> Self {
Default::default()
}
}
}
);

0 comments on commit a31fb58

Please sign in to comment.