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

perf: Arena based ast #9805

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Cargo.lock

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

22 changes: 22 additions & 0 deletions crates/ast_node_arena/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
authors = ["강동윤 <[email protected]>"]
description = "Macros for ast nodes."
documentation = "https://rustdoc.swc.rs/ast_node/"
edition = "2021"
license = "Apache-2.0"
name = "ast_node_arena"
repository = "https://github.com/swc-project/swc.git"
version = "3.0.0"

[lib]
bench = false
proc-macro = true

[dependencies]
proc-macro2 = { workspace = true }
quote = { workspace = true }

swc_macros_common = { version = "1.0.0", path = "../swc_macros_common" }
[dependencies.syn]
features = ["derive", "fold", "parsing", "printing", "visit-mut"]
workspace = true
43 changes: 43 additions & 0 deletions crates/ast_node_arena/src/ast_node_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use swc_macros_common::prelude::*;
use syn::{
parse::{Parse, ParseStream},
*,
};

#[derive(Clone)]
pub struct Args {
pub ty: Literal,
}

impl Parse for Args {
fn parse(i: ParseStream<'_>) -> syn::Result<Self> {
Ok(Args { ty: i.parse()? })
}
}

pub fn expand_struct(args: Args, i: DeriveInput) -> Vec<ItemImpl> {
let mut items = Vec::new();
// let generics = i.generics.clone();
// let item_ident = Ident::new("Item", i.ident.span());

{
let ty = &i.ident;
let type_str = &args.ty;
let has_lifetime = i.generics.lifetimes().count() > 0;
let item: ItemImpl = if has_lifetime {
parse_quote!(
impl<'a> ::swc_common::arena::AstNode<'a> for #ty<'a> {
const TYPE: &'static str = #type_str;
}
)
} else {
parse_quote!(
impl<'a> ::swc_common::arena::AstNode<'a> for #ty {
const TYPE: &'static str = #type_str;
}
)
};
items.push(item);
}
items
}
96 changes: 96 additions & 0 deletions crates/ast_node_arena/src/clone_in.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/// References:
/// <https://github.com/oxc-project/oxc/blob/main/tasks/ast_tools/src/derives/clone_in.rs>
///
/// The original code is MIT licensed.
use quote::{format_ident, quote};
use swc_macros_common::prelude::*;
use syn::{parse_quote, DeriveInput, GenericParam, Ident, ItemImpl};

pub fn derive(input: DeriveInput) -> ItemImpl {
let ty_ident = input.ident;
let has_lifetime = input
.generics
.params
.iter()
.any(|p| matches!(p, GenericParam::Lifetime(_)));
match input.data {
syn::Data::Struct(data_struct) => {
let (alloc_ident, body) = if data_struct.fields.is_empty() {
(format_ident!("_"), quote!(#ty_ident))
} else {
let mut is_tuple = false;
let fields = data_struct.fields.into_iter().enumerate().map(|(index, field)| {
let ident = field.ident;
if let Some(ident) = ident {
quote!(#ident: swc_allocator::arena::CloneIn::clone_in(&self.#ident, allocator))
} else {
is_tuple = true;
let index = syn::Index::from(index);
quote!(swc_allocator::arena::CloneIn::clone_in(&self.#index, allocator))
}
}).collect::<Vec<_>>();
(
format_ident!("allocator"),
if is_tuple {
quote!(#ty_ident(#(#fields),* ))
} else {
quote!(#ty_ident { #(#fields),* })
},
)
};
impl_clone_in(&ty_ident, has_lifetime, &alloc_ident, &body)
}
syn::Data::Enum(data_enum) => {
let mut need_alloc = false;
let matches = data_enum.variants.into_iter().map(|variant| {
let ident = variant.ident;
if variant.fields.is_empty() {
quote!(Self :: #ident => #ty_ident :: #ident)
} else {
need_alloc = true;
quote!(Self :: #ident(it) => #ty_ident :: #ident(swc_allocator::arena::CloneIn::clone_in(it, allocator)))
}
}).collect::<Vec<_>>();
let alloc_ident = if need_alloc {
format_ident!("allocator")
} else {
format_ident!("_")
};
let body = quote! {
match self {
#(#matches),*
}
};

impl_clone_in(&ty_ident, has_lifetime, &alloc_ident, &body)
}
syn::Data::Union(_) => unimplemented!(),
}
}

fn impl_clone_in(
ty_ident: &Ident,
has_lifetime: bool,
alloc_ident: &Ident,
body: &TokenStream,
) -> ItemImpl {
if has_lifetime {
parse_quote! {
impl <'old_alloc, 'new_alloc> swc_allocator::arena::CloneIn<'new_alloc> for #ty_ident<'old_alloc> {
type Cloned = #ty_ident<'new_alloc>;
fn clone_in(&self, #alloc_ident: &'new_alloc swc_allocator::arena::Allocator) -> Self::Cloned {
#body
}
}
}
} else {
parse_quote! {
impl <'alloc> swc_allocator::arena::CloneIn<'alloc> for #ty_ident {
type Cloned = #ty_ident;
fn clone_in(&self, #alloc_ident: &'alloc swc_allocator::arena::Allocator) -> Self::Cloned {
#body
}
}
}
}
}
Loading
Loading