perf(ast)!: reduce size of WithClause by Boxing it (#5677)

relates #5601
This commit is contained in:
Boshen 2024-09-10 14:01:36 +00:00
parent f9e3a41dd2
commit ee4fb42d70
12 changed files with 136 additions and 88 deletions

View file

@ -2311,7 +2311,7 @@ pub struct ImportDeclaration<'a> {
pub specifiers: Option<Vec<'a, ImportDeclarationSpecifier<'a>>>,
pub source: StringLiteral<'a>,
/// Some(vec![]) for empty assertion
pub with_clause: Option<WithClause<'a>>,
pub with_clause: Option<Box<'a, WithClause<'a>>>,
/// `import type { foo } from 'bar'`
pub import_kind: ImportOrExportKind,
}
@ -2452,7 +2452,7 @@ pub struct ExportNamedDeclaration<'a> {
/// `export type { foo }`
pub export_kind: ImportOrExportKind,
/// Some(vec![]) for empty assertion
pub with_clause: Option<WithClause<'a>>,
pub with_clause: Option<Box<'a, WithClause<'a>>>,
}
/// Export Default Declaration
@ -2497,7 +2497,7 @@ pub struct ExportAllDeclaration<'a> {
pub exported: Option<ModuleExportName<'a>>,
pub source: StringLiteral<'a>,
/// Will be `Some(vec![])` for empty assertion
pub with_clause: Option<WithClause<'a>>, // Some(vec![]) for empty assertion
pub with_clause: Option<Box<'a, WithClause<'a>>>, // Some(vec![]) for empty assertion
pub export_kind: ImportOrExportKind, // `export type *`
}

View file

@ -174,7 +174,7 @@ impl<'a> AstBuilder<'a> {
self.vec(),
None,
ImportOrExportKind::Value,
None,
None::<WithClause>,
))
}
@ -191,7 +191,7 @@ impl<'a> AstBuilder<'a> {
specifiers,
source,
ImportOrExportKind::Value,
None,
None::<WithClause>,
))
}

View file

@ -1409,7 +1409,7 @@ impl<'a> ModuleDeclaration<'a> {
}
}
pub fn with_clause(&self) -> Option<&WithClause<'a>> {
pub fn with_clause(&self) -> Option<&Box<'a, WithClause<'a>>> {
match self {
Self::ImportDeclaration(decl) => decl.with_clause.as_ref(),
Self::ExportAllDeclaration(decl) => decl.with_clause.as_ref(),

View file

@ -706,13 +706,13 @@ const _: () = {
assert!(offset_of!(ImportExpression, source) == 8usize);
assert!(offset_of!(ImportExpression, arguments) == 24usize);
assert!(size_of::<ImportDeclaration>() == 136usize);
assert!(size_of::<ImportDeclaration>() == 80usize);
assert!(align_of::<ImportDeclaration>() == 8usize);
assert!(offset_of!(ImportDeclaration, span) == 0usize);
assert!(offset_of!(ImportDeclaration, specifiers) == 8usize);
assert!(offset_of!(ImportDeclaration, source) == 40usize);
assert!(offset_of!(ImportDeclaration, with_clause) == 64usize);
assert!(offset_of!(ImportDeclaration, import_kind) == 128usize);
assert!(offset_of!(ImportDeclaration, import_kind) == 72usize);
assert!(size_of::<ImportDeclarationSpecifier>() == 16usize);
assert!(align_of::<ImportDeclarationSpecifier>() == 8usize);
@ -749,7 +749,7 @@ const _: () = {
assert!(size_of::<ImportAttributeKey>() == 32usize);
assert!(align_of::<ImportAttributeKey>() == 8usize);
assert!(size_of::<ExportNamedDeclaration>() == 152usize);
assert!(size_of::<ExportNamedDeclaration>() == 96usize);
assert!(align_of::<ExportNamedDeclaration>() == 8usize);
assert!(offset_of!(ExportNamedDeclaration, span) == 0usize);
assert!(offset_of!(ExportNamedDeclaration, declaration) == 8usize);
@ -764,13 +764,13 @@ const _: () = {
assert!(offset_of!(ExportDefaultDeclaration, declaration) == 8usize);
assert!(offset_of!(ExportDefaultDeclaration, exported) == 24usize);
assert!(size_of::<ExportAllDeclaration>() == 144usize);
assert!(size_of::<ExportAllDeclaration>() == 88usize);
assert!(align_of::<ExportAllDeclaration>() == 8usize);
assert!(offset_of!(ExportAllDeclaration, span) == 0usize);
assert!(offset_of!(ExportAllDeclaration, exported) == 8usize);
assert!(offset_of!(ExportAllDeclaration, source) == 48usize);
assert!(offset_of!(ExportAllDeclaration, with_clause) == 72usize);
assert!(offset_of!(ExportAllDeclaration, export_kind) == 136usize);
assert!(offset_of!(ExportAllDeclaration, export_kind) == 80usize);
assert!(size_of::<ExportSpecifier>() == 96usize);
assert!(align_of::<ExportSpecifier>() == 8usize);
@ -2261,13 +2261,13 @@ const _: () = {
assert!(offset_of!(ImportExpression, source) == 8usize);
assert!(offset_of!(ImportExpression, arguments) == 16usize);
assert!(size_of::<ImportDeclaration>() == 84usize);
assert!(size_of::<ImportDeclaration>() == 48usize);
assert!(align_of::<ImportDeclaration>() == 4usize);
assert!(offset_of!(ImportDeclaration, span) == 0usize);
assert!(offset_of!(ImportDeclaration, specifiers) == 8usize);
assert!(offset_of!(ImportDeclaration, source) == 24usize);
assert!(offset_of!(ImportDeclaration, with_clause) == 40usize);
assert!(offset_of!(ImportDeclaration, import_kind) == 80usize);
assert!(offset_of!(ImportDeclaration, import_kind) == 44usize);
assert!(size_of::<ImportDeclarationSpecifier>() == 8usize);
assert!(align_of::<ImportDeclarationSpecifier>() == 4usize);
@ -2304,7 +2304,7 @@ const _: () = {
assert!(size_of::<ImportAttributeKey>() == 20usize);
assert!(align_of::<ImportAttributeKey>() == 4usize);
assert!(size_of::<ExportNamedDeclaration>() == 92usize);
assert!(size_of::<ExportNamedDeclaration>() == 56usize);
assert!(align_of::<ExportNamedDeclaration>() == 4usize);
assert!(offset_of!(ExportNamedDeclaration, span) == 0usize);
assert!(offset_of!(ExportNamedDeclaration, declaration) == 8usize);
@ -2319,13 +2319,13 @@ const _: () = {
assert!(offset_of!(ExportDefaultDeclaration, declaration) == 8usize);
assert!(offset_of!(ExportDefaultDeclaration, exported) == 16usize);
assert!(size_of::<ExportAllDeclaration>() == 92usize);
assert!(size_of::<ExportAllDeclaration>() == 56usize);
assert!(align_of::<ExportAllDeclaration>() == 4usize);
assert!(offset_of!(ExportAllDeclaration, span) == 0usize);
assert!(offset_of!(ExportAllDeclaration, exported) == 8usize);
assert!(offset_of!(ExportAllDeclaration, source) == 32usize);
assert!(offset_of!(ExportAllDeclaration, with_clause) == 48usize);
assert!(offset_of!(ExportAllDeclaration, export_kind) == 88usize);
assert!(offset_of!(ExportAllDeclaration, export_kind) == 52usize);
assert!(size_of::<ExportSpecifier>() == 60usize);
assert!(align_of::<ExportSpecifier>() == 4usize);

View file

@ -6740,14 +6740,17 @@ impl<'a> AstBuilder<'a> {
/// - with_clause: Some(vec![]) for empty assertion
/// - import_kind: `import type { foo } from 'bar'`
#[inline]
pub fn module_declaration_import_declaration(
pub fn module_declaration_import_declaration<T1>(
self,
span: Span,
specifiers: Option<Vec<'a, ImportDeclarationSpecifier<'a>>>,
source: StringLiteral<'a>,
with_clause: Option<WithClause<'a>>,
with_clause: T1,
import_kind: ImportOrExportKind,
) -> ModuleDeclaration<'a> {
) -> ModuleDeclaration<'a>
where
T1: IntoIn<'a, Option<Box<'a, WithClause<'a>>>>,
{
ModuleDeclaration::ImportDeclaration(self.alloc(self.import_declaration(
span,
specifiers,
@ -6777,14 +6780,17 @@ impl<'a> AstBuilder<'a> {
/// - with_clause: Will be `Some(vec![])` for empty assertion
/// - export_kind
#[inline]
pub fn module_declaration_export_all_declaration(
pub fn module_declaration_export_all_declaration<T1>(
self,
span: Span,
exported: Option<ModuleExportName<'a>>,
source: StringLiteral<'a>,
with_clause: Option<WithClause<'a>>,
with_clause: T1,
export_kind: ImportOrExportKind,
) -> ModuleDeclaration<'a> {
) -> ModuleDeclaration<'a>
where
T1: IntoIn<'a, Option<Box<'a, WithClause<'a>>>>,
{
ModuleDeclaration::ExportAllDeclaration(self.alloc(self.export_all_declaration(
span,
exported,
@ -6852,15 +6858,18 @@ impl<'a> AstBuilder<'a> {
/// - export_kind: `export type { foo }`
/// - with_clause: Some(vec![]) for empty assertion
#[inline]
pub fn module_declaration_export_named_declaration(
pub fn module_declaration_export_named_declaration<T1>(
self,
span: Span,
declaration: Option<Declaration<'a>>,
specifiers: Vec<'a, ExportSpecifier<'a>>,
source: Option<StringLiteral<'a>>,
export_kind: ImportOrExportKind,
with_clause: Option<WithClause<'a>>,
) -> ModuleDeclaration<'a> {
with_clause: T1,
) -> ModuleDeclaration<'a>
where
T1: IntoIn<'a, Option<Box<'a, WithClause<'a>>>>,
{
ModuleDeclaration::ExportNamedDeclaration(self.alloc(self.export_named_declaration(
span,
declaration,
@ -7082,15 +7091,24 @@ impl<'a> AstBuilder<'a> {
/// - with_clause: Some(vec![]) for empty assertion
/// - import_kind: `import type { foo } from 'bar'`
#[inline]
pub fn import_declaration(
pub fn import_declaration<T1>(
self,
span: Span,
specifiers: Option<Vec<'a, ImportDeclarationSpecifier<'a>>>,
source: StringLiteral<'a>,
with_clause: Option<WithClause<'a>>,
with_clause: T1,
import_kind: ImportOrExportKind,
) -> ImportDeclaration<'a> {
ImportDeclaration { span, specifiers, source, with_clause, import_kind }
) -> ImportDeclaration<'a>
where
T1: IntoIn<'a, Option<Box<'a, WithClause<'a>>>>,
{
ImportDeclaration {
span,
specifiers,
source,
with_clause: with_clause.into_in(self.allocator),
import_kind,
}
}
/// Builds a [`ImportDeclaration`] and stores it in the memory arena.
@ -7104,14 +7122,17 @@ impl<'a> AstBuilder<'a> {
/// - with_clause: Some(vec![]) for empty assertion
/// - import_kind: `import type { foo } from 'bar'`
#[inline]
pub fn alloc_import_declaration(
pub fn alloc_import_declaration<T1>(
self,
span: Span,
specifiers: Option<Vec<'a, ImportDeclarationSpecifier<'a>>>,
source: StringLiteral<'a>,
with_clause: Option<WithClause<'a>>,
with_clause: T1,
import_kind: ImportOrExportKind,
) -> Box<'a, ImportDeclaration<'a>> {
) -> Box<'a, ImportDeclaration<'a>>
where
T1: IntoIn<'a, Option<Box<'a, WithClause<'a>>>>,
{
Box::new_in(
self.import_declaration(span, specifiers, source, with_clause, import_kind),
self.allocator,
@ -7455,16 +7476,26 @@ impl<'a> AstBuilder<'a> {
/// - export_kind: `export type { foo }`
/// - with_clause: Some(vec![]) for empty assertion
#[inline]
pub fn export_named_declaration(
pub fn export_named_declaration<T1>(
self,
span: Span,
declaration: Option<Declaration<'a>>,
specifiers: Vec<'a, ExportSpecifier<'a>>,
source: Option<StringLiteral<'a>>,
export_kind: ImportOrExportKind,
with_clause: Option<WithClause<'a>>,
) -> ExportNamedDeclaration<'a> {
ExportNamedDeclaration { span, declaration, specifiers, source, export_kind, with_clause }
with_clause: T1,
) -> ExportNamedDeclaration<'a>
where
T1: IntoIn<'a, Option<Box<'a, WithClause<'a>>>>,
{
ExportNamedDeclaration {
span,
declaration,
specifiers,
source,
export_kind,
with_clause: with_clause.into_in(self.allocator),
}
}
/// Builds a [`ExportNamedDeclaration`] and stores it in the memory arena.
@ -7479,15 +7510,18 @@ impl<'a> AstBuilder<'a> {
/// - export_kind: `export type { foo }`
/// - with_clause: Some(vec![]) for empty assertion
#[inline]
pub fn alloc_export_named_declaration(
pub fn alloc_export_named_declaration<T1>(
self,
span: Span,
declaration: Option<Declaration<'a>>,
specifiers: Vec<'a, ExportSpecifier<'a>>,
source: Option<StringLiteral<'a>>,
export_kind: ImportOrExportKind,
with_clause: Option<WithClause<'a>>,
) -> Box<'a, ExportNamedDeclaration<'a>> {
with_clause: T1,
) -> Box<'a, ExportNamedDeclaration<'a>>
where
T1: IntoIn<'a, Option<Box<'a, WithClause<'a>>>>,
{
Box::new_in(
self.export_named_declaration(
span,
@ -7548,15 +7582,24 @@ impl<'a> AstBuilder<'a> {
/// - with_clause: Will be `Some(vec![])` for empty assertion
/// - export_kind
#[inline]
pub fn export_all_declaration(
pub fn export_all_declaration<T1>(
self,
span: Span,
exported: Option<ModuleExportName<'a>>,
source: StringLiteral<'a>,
with_clause: Option<WithClause<'a>>,
with_clause: T1,
export_kind: ImportOrExportKind,
) -> ExportAllDeclaration<'a> {
ExportAllDeclaration { span, exported, source, with_clause, export_kind }
) -> ExportAllDeclaration<'a>
where
T1: IntoIn<'a, Option<Box<'a, WithClause<'a>>>>,
{
ExportAllDeclaration {
span,
exported,
source,
with_clause: with_clause.into_in(self.allocator),
export_kind,
}
}
/// Builds a [`ExportAllDeclaration`] and stores it in the memory arena.
@ -7570,14 +7613,17 @@ impl<'a> AstBuilder<'a> {
/// - with_clause: Will be `Some(vec![])` for empty assertion
/// - export_kind
#[inline]
pub fn alloc_export_all_declaration(
pub fn alloc_export_all_declaration<T1>(
self,
span: Span,
exported: Option<ModuleExportName<'a>>,
source: StringLiteral<'a>,
with_clause: Option<WithClause<'a>>,
with_clause: T1,
export_kind: ImportOrExportKind,
) -> Box<'a, ExportAllDeclaration<'a>> {
) -> Box<'a, ExportAllDeclaration<'a>>
where
T1: IntoIn<'a, Option<Box<'a, WithClause<'a>>>>,
{
Box::new_in(
self.export_all_declaration(span, exported, source, with_clause, export_kind),
self.allocator,

View file

@ -719,10 +719,10 @@ impl<'a> Gen for ImportDeclaration<'a> {
p.print_char(b'"');
p.print_str(self.source.value.as_str());
p.print_char(b'"');
if self.with_clause.is_some() {
if let Some(with_clause) = &self.with_clause {
p.print_hard_space();
with_clause.gen(p, ctx);
}
self.with_clause.gen(p, ctx);
p.print_semicolon_after_statement();
return;
}
@ -802,20 +802,12 @@ impl<'a> Gen for ImportDeclaration<'a> {
p.print_str(" from ");
}
self.source.gen(p, ctx);
if self.with_clause.is_some() {
if let Some(with_clause) = &self.with_clause {
p.print_hard_space();
}
self.with_clause.gen(p, ctx);
p.add_source_mapping(self.span.end);
p.print_semicolon_after_statement();
}
}
impl<'a> Gen for Option<WithClause<'a>> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
if let Some(with_clause) = self {
with_clause.gen(p, ctx);
}
p.add_source_mapping(self.span.end);
p.print_semicolon_after_statement();
}
}
@ -976,10 +968,10 @@ impl<'a> Gen for ExportAllDeclaration<'a> {
p.print_str(" from ");
self.source.gen(p, ctx);
if self.with_clause.is_some() {
if let Some(with_clause) = &self.with_clause {
p.print_hard_space();
with_clause.gen(p, ctx);
}
self.with_clause.gen(p, ctx);
p.print_semicolon_after_statement();
}
}

View file

@ -309,8 +309,14 @@ impl<'a> IsolatedDeclarations<'a> {
if need_empty_export_marker {
let specifiers = self.ast.vec();
let kind = ImportOrExportKind::Value;
let empty_export =
self.ast.alloc_export_named_declaration(SPAN, None, specifiers, None, kind, None);
let empty_export = self.ast.alloc_export_named_declaration(
SPAN,
None,
specifiers,
None,
kind,
None::<WithClause>,
);
new_ast_stmts
.push(Statement::from(ModuleDeclaration::ExportNamedDeclaration(empty_export)));
}

View file

@ -185,9 +185,13 @@ impl<'a> InjectGlobalVariables<'a> {
let specifiers = Some(self.ast.vec1(self.inject_import_to_specifier(inject)));
let source = self.ast.string_literal(SPAN, inject.source.as_str());
let kind = ImportOrExportKind::Value;
let import_decl = self
.ast
.module_declaration_import_declaration(SPAN, specifiers, source, None, kind);
let import_decl = self.ast.module_declaration_import_declaration(
SPAN,
specifiers,
source,
None::<WithClause>,
kind,
);
self.ast.statement_module_declaration(import_decl)
});
program.body.splice(0..0, imports);

View file

@ -347,7 +347,7 @@ impl<'a> ParserImpl<'a> {
self.ast.vec(),
None,
ImportOrExportKind::Value,
None,
None::<WithClause>,
))
}

View file

@ -108,7 +108,7 @@ impl<'a> ModuleImports<'a> {
SPAN,
Some(specifiers),
StringLiteral::new(SPAN, source),
None,
None::<WithClause>,
ImportOrExportKind::Value,
);
self.ast.statement_module_declaration(import_stmt)

View file

@ -8064,10 +8064,10 @@ impl<'a, 't> ImportDeclarationWithoutSpecifiers<'a, 't> {
}
#[inline]
pub fn with_clause(self) -> &'t Option<WithClause<'a>> {
pub fn with_clause(self) -> &'t Option<Box<'a, WithClause<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_IMPORT_DECLARATION_WITH_CLAUSE)
as *const Option<WithClause<'a>>)
as *const Option<Box<'a, WithClause<'a>>>)
}
}
@ -8102,10 +8102,10 @@ impl<'a, 't> ImportDeclarationWithoutSource<'a, 't> {
}
#[inline]
pub fn with_clause(self) -> &'t Option<WithClause<'a>> {
pub fn with_clause(self) -> &'t Option<Box<'a, WithClause<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_IMPORT_DECLARATION_WITH_CLAUSE)
as *const Option<WithClause<'a>>)
as *const Option<Box<'a, WithClause<'a>>>)
}
}
@ -8413,10 +8413,10 @@ impl<'a, 't> ExportNamedDeclarationWithoutDeclaration<'a, 't> {
}
#[inline]
pub fn with_clause(self) -> &'t Option<WithClause<'a>> {
pub fn with_clause(self) -> &'t Option<Box<'a, WithClause<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_EXPORT_NAMED_DECLARATION_WITH_CLAUSE)
as *const Option<WithClause<'a>>)
as *const Option<Box<'a, WithClause<'a>>>)
}
}
}
@ -8461,10 +8461,10 @@ impl<'a, 't> ExportNamedDeclarationWithoutSpecifiers<'a, 't> {
}
#[inline]
pub fn with_clause(self) -> &'t Option<WithClause<'a>> {
pub fn with_clause(self) -> &'t Option<Box<'a, WithClause<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_EXPORT_NAMED_DECLARATION_WITH_CLAUSE)
as *const Option<WithClause<'a>>)
as *const Option<Box<'a, WithClause<'a>>>)
}
}
}
@ -8509,10 +8509,10 @@ impl<'a, 't> ExportNamedDeclarationWithoutSource<'a, 't> {
}
#[inline]
pub fn with_clause(self) -> &'t Option<WithClause<'a>> {
pub fn with_clause(self) -> &'t Option<Box<'a, WithClause<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_EXPORT_NAMED_DECLARATION_WITH_CLAUSE)
as *const Option<WithClause<'a>>)
as *const Option<Box<'a, WithClause<'a>>>)
}
}
}
@ -8652,10 +8652,10 @@ impl<'a, 't> ExportAllDeclarationWithoutExported<'a, 't> {
}
#[inline]
pub fn with_clause(self) -> &'t Option<WithClause<'a>> {
pub fn with_clause(self) -> &'t Option<Box<'a, WithClause<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_EXPORT_ALL_DECLARATION_WITH_CLAUSE)
as *const Option<WithClause<'a>>)
as *const Option<Box<'a, WithClause<'a>>>)
}
}
@ -8690,10 +8690,10 @@ impl<'a, 't> ExportAllDeclarationWithoutSource<'a, 't> {
}
#[inline]
pub fn with_clause(self) -> &'t Option<WithClause<'a>> {
pub fn with_clause(self) -> &'t Option<Box<'a, WithClause<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_EXPORT_ALL_DECLARATION_WITH_CLAUSE)
as *const Option<WithClause<'a>>)
as *const Option<Box<'a, WithClause<'a>>>)
}
}

View file

@ -2785,10 +2785,10 @@ pub(crate) unsafe fn walk_import_declaration<'a, Tr: Traverse<'a>>(
);
if let Some(field) = &mut *((node as *mut u8)
.add(ancestor::OFFSET_IMPORT_DECLARATION_WITH_CLAUSE)
as *mut Option<WithClause>)
as *mut Option<Box<WithClause>>)
{
ctx.retag_stack(AncestorType::ImportDeclarationWithClause);
walk_with_clause(traverser, field as *mut _, ctx);
walk_with_clause(traverser, (&mut **field) as *mut _, ctx);
}
ctx.pop_stack(pop_token);
traverser.exit_import_declaration(&mut *node, ctx);
@ -2974,10 +2974,10 @@ pub(crate) unsafe fn walk_export_named_declaration<'a, Tr: Traverse<'a>>(
}
if let Some(field) = &mut *((node as *mut u8)
.add(ancestor::OFFSET_EXPORT_NAMED_DECLARATION_WITH_CLAUSE)
as *mut Option<WithClause>)
as *mut Option<Box<WithClause>>)
{
ctx.retag_stack(AncestorType::ExportNamedDeclarationWithClause);
walk_with_clause(traverser, field as *mut _, ctx);
walk_with_clause(traverser, (&mut **field) as *mut _, ctx);
}
ctx.pop_stack(pop_token);
traverser.exit_export_named_declaration(&mut *node, ctx);
@ -3032,10 +3032,10 @@ pub(crate) unsafe fn walk_export_all_declaration<'a, Tr: Traverse<'a>>(
);
if let Some(field) = &mut *((node as *mut u8)
.add(ancestor::OFFSET_EXPORT_ALL_DECLARATION_WITH_CLAUSE)
as *mut Option<WithClause>)
as *mut Option<Box<WithClause>>)
{
ctx.retag_stack(AncestorType::ExportAllDeclarationWithClause);
walk_with_clause(traverser, field as *mut _, ctx);
walk_with_clause(traverser, (&mut **field) as *mut _, ctx);
}
ctx.pop_stack(pop_token);
traverser.exit_export_all_declaration(&mut *node, ctx);