From 6ca01b9c35cc62115277def4179fc4a10d290a47 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Sat, 26 Oct 2024 12:40:08 +0000 Subject: [PATCH] perf(ast): reduce size of `Comment` (#6921) Reduce `Comment` from 20 bytes to 16. Because `Comment` has `#[ast]` attr, it's `#[repr(C)]` which means the fields don't get re-ordered by the compiler, so it contains excess padding. Moving `attached_to: u32` field removes the padding. I am in favour of using `oxc_ast_tools` to re-order fields the same way the compiler does, which will solve this problem for all AST types. But may as well fix this one in meantime. Unlike most AST types, field order doesn't matter for `Comment` - it is not visited, so the field order has no semantic meaning. --- crates/oxc_ast/src/ast/comment.rs | 14 +++++------ .../oxc_ast/src/generated/assert_layouts.rs | 24 +++++++++---------- .../oxc_ast/src/generated/derive_clone_in.rs | 2 +- .../src/generated/derive_content_eq.rs | 4 ++-- .../src/generated/derive_content_hash.rs | 2 +- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/crates/oxc_ast/src/ast/comment.rs b/crates/oxc_ast/src/ast/comment.rs index cd4a23b6c4710..2150eb0886050 100644 --- a/crates/oxc_ast/src/ast/comment.rs +++ b/crates/oxc_ast/src/ast/comment.rs @@ -46,18 +46,18 @@ pub struct Comment { /// The span of the comment text (without leading/trailing delimiters). pub span: Span, - /// Line or block comment - pub kind: CommentKind, - - /// Leading or trailing comment - pub position: CommentPosition, - /// Start of token this leading comment is attached to. /// `/* Leading */ token` /// ^ This start /// NOTE: Trailing comment attachment is not computed yet. pub attached_to: u32, + /// Line or block comment + pub kind: CommentKind, + + /// Leading or trailing comment + pub position: CommentPosition, + /// Whether this comment has a preceding newline. /// Used to avoid becoming a trailing comment in codegen. pub preceded_by_newline: bool, @@ -73,9 +73,9 @@ impl Comment { let span = Span::new(start, end); Self { span, + attached_to: 0, kind, position: CommentPosition::Trailing, - attached_to: 0, preceded_by_newline: false, followed_by_newline: false, } diff --git a/crates/oxc_ast/src/generated/assert_layouts.rs b/crates/oxc_ast/src/generated/assert_layouts.rs index 4ef53a7dc5b43..4018e7fc24d82 100644 --- a/crates/oxc_ast/src/generated/assert_layouts.rs +++ b/crates/oxc_ast/src/generated/assert_layouts.rs @@ -1380,14 +1380,14 @@ const _: () = { assert!(size_of::() == 1usize); assert!(align_of::() == 1usize); - assert!(size_of::() == 20usize); + assert!(size_of::() == 16usize); assert!(align_of::() == 4usize); assert!(offset_of!(Comment, span) == 0usize); - assert!(offset_of!(Comment, kind) == 8usize); - assert!(offset_of!(Comment, position) == 9usize); - assert!(offset_of!(Comment, attached_to) == 12usize); - assert!(offset_of!(Comment, preceded_by_newline) == 16usize); - assert!(offset_of!(Comment, followed_by_newline) == 17usize); + assert!(offset_of!(Comment, attached_to) == 8usize); + assert!(offset_of!(Comment, kind) == 12usize); + assert!(offset_of!(Comment, position) == 13usize); + assert!(offset_of!(Comment, preceded_by_newline) == 14usize); + assert!(offset_of!(Comment, followed_by_newline) == 15usize); assert!(size_of::() == 1usize); assert!(align_of::() == 1usize); @@ -2939,14 +2939,14 @@ const _: () = { assert!(size_of::() == 1usize); assert!(align_of::() == 1usize); - assert!(size_of::() == 20usize); + assert!(size_of::() == 16usize); assert!(align_of::() == 4usize); assert!(offset_of!(Comment, span) == 0usize); - assert!(offset_of!(Comment, kind) == 8usize); - assert!(offset_of!(Comment, position) == 9usize); - assert!(offset_of!(Comment, attached_to) == 12usize); - assert!(offset_of!(Comment, preceded_by_newline) == 16usize); - assert!(offset_of!(Comment, followed_by_newline) == 17usize); + assert!(offset_of!(Comment, attached_to) == 8usize); + assert!(offset_of!(Comment, kind) == 12usize); + assert!(offset_of!(Comment, position) == 13usize); + assert!(offset_of!(Comment, preceded_by_newline) == 14usize); + assert!(offset_of!(Comment, followed_by_newline) == 15usize); assert!(size_of::() == 1usize); assert!(align_of::() == 1usize); diff --git a/crates/oxc_ast/src/generated/derive_clone_in.rs b/crates/oxc_ast/src/generated/derive_clone_in.rs index 0fc290be9981a..67e745b3f0859 100644 --- a/crates/oxc_ast/src/generated/derive_clone_in.rs +++ b/crates/oxc_ast/src/generated/derive_clone_in.rs @@ -4256,9 +4256,9 @@ impl<'alloc> CloneIn<'alloc> for Comment { fn clone_in(&self, allocator: &'alloc Allocator) -> Self::Cloned { Comment { span: CloneIn::clone_in(&self.span, allocator), + attached_to: CloneIn::clone_in(&self.attached_to, allocator), kind: CloneIn::clone_in(&self.kind, allocator), position: CloneIn::clone_in(&self.position, allocator), - attached_to: CloneIn::clone_in(&self.attached_to, allocator), preceded_by_newline: CloneIn::clone_in(&self.preceded_by_newline, allocator), followed_by_newline: CloneIn::clone_in(&self.followed_by_newline, allocator), } diff --git a/crates/oxc_ast/src/generated/derive_content_eq.rs b/crates/oxc_ast/src/generated/derive_content_eq.rs index 7ab94066e3660..17c7f867e290b 100644 --- a/crates/oxc_ast/src/generated/derive_content_eq.rs +++ b/crates/oxc_ast/src/generated/derive_content_eq.rs @@ -4226,9 +4226,9 @@ impl ContentEq for CommentPosition { impl ContentEq for Comment { fn content_eq(&self, other: &Self) -> bool { - ContentEq::content_eq(&self.kind, &other.kind) + ContentEq::content_eq(&self.attached_to, &other.attached_to) + && ContentEq::content_eq(&self.kind, &other.kind) && ContentEq::content_eq(&self.position, &other.position) - && ContentEq::content_eq(&self.attached_to, &other.attached_to) && ContentEq::content_eq(&self.preceded_by_newline, &other.preceded_by_newline) && ContentEq::content_eq(&self.followed_by_newline, &other.followed_by_newline) } diff --git a/crates/oxc_ast/src/generated/derive_content_hash.rs b/crates/oxc_ast/src/generated/derive_content_hash.rs index 72bf9c441178f..528ac0715f163 100644 --- a/crates/oxc_ast/src/generated/derive_content_hash.rs +++ b/crates/oxc_ast/src/generated/derive_content_hash.rs @@ -2388,9 +2388,9 @@ impl ContentHash for CommentPosition { impl ContentHash for Comment { fn content_hash(&self, state: &mut H) { + ContentHash::content_hash(&self.attached_to, state); ContentHash::content_hash(&self.kind, state); ContentHash::content_hash(&self.position, state); - ContentHash::content_hash(&self.attached_to, state); ContentHash::content_hash(&self.preceded_by_newline, state); ContentHash::content_hash(&self.followed_by_newline, state); }