feat(allocator): Feature gate nightly via macros (#9274)

**Description:**

We can make swc faster without dropping support for stable rustc.
This commit is contained in:
Donny/강동윤 2024-07-18 23:03:58 +09:00 committed by GitHub
parent 04d8a369cd
commit a31fb58399
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 120 additions and 67 deletions

View File

@ -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 }

View File

@ -6,7 +6,7 @@ use std::{
ptr::NonNull,
};
use allocator_api2::alloc::Global;
use allocator_api2::alloc::AllocError;
use bumpalo::Bump;
use crate::FastAlloc;
@ -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,
@ -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]> {
@ -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)?;
@ -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)?;
@ -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]
@ -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)?;
@ -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)?;
@ -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)?;

View File

@ -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.
@ -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
};
}

View File

@ -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};
@ -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 {
@ -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

View File

@ -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()
}
}
}
);

View File

@ -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()
}
}
}
);