Skip to content

Commit

Permalink
Reduce size of AssignmentTargetMaybeDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Mar 14, 2024
1 parent 3e777ba commit 781bf3a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
10 changes: 9 additions & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,11 @@ pub struct AssignmentExpression<'a> {
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[serde(untagged)]
pub enum AssignmentTarget<'a> {
// First discriminant 1, to allow discriminant for `AssignmentTargetMaybeDefault`'s other variant
// to fit in the niche before this, so `AssignmentTargetMaybeDefault` can be 24 bytes, not 32
#[cfg(feature = "raw")]
SimpleAssignmentTarget(SimpleAssignmentTarget<'a>) = 1,
#[cfg(not(feature = "raw"))]
SimpleAssignmentTarget(SimpleAssignmentTarget<'a>),
AssignmentTargetPattern(AssignmentTargetPattern<'a>),
}
Expand Down Expand Up @@ -1136,13 +1141,16 @@ pub struct AssignmentTargetRest<'a> {
pub target: AssignmentTarget<'a>,
}

#[ast_node]
// No `#[ast_node]` because don't want this type to be `repr(u8)` so that discriminants of `AssignmentTarget`
// can be folded into this, so this type is 24 bytes, not 32
#[derive(Debug, Hash, SerAttrs)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "raw", derive(Inspect))]
#[serde(untagged)]
pub enum AssignmentTargetMaybeDefault<'a> {
AssignmentTarget(AssignmentTarget<'a>),
// Compiler assigns this discrimant 0
AssignmentTargetWithDefault(Box<'a, AssignmentTargetWithDefault<'a>>),
}

Expand Down
1 change: 0 additions & 1 deletion crates/oxc_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ fn size_asserts() {
assert_eq_size!(ast::ClassElement, [u8; 16]);
assert_eq_size!(ast::ExportDefaultDeclarationKind, [u8; 16]);
assert_eq_size!(ast::AssignmentTargetPattern, [u8; 16]);
#[cfg(not(feature = "raw"))]
assert_eq_size!(ast::AssignmentTargetMaybeDefault, [u8; 24]);
assert_eq_size!(ast::AssignmentTargetProperty, [u8; 16]);
assert_eq_size!(ast::TSLiteral, [u8; 16]);
Expand Down
4 changes: 4 additions & 0 deletions napi/parser/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ typesByName.Declaration.niche = {offset: 0, size: 1, value: 255};
flattenEnumVariants('ExportDefaultDeclarationKind', 'Expression', 'Expression');
typesByName.Expression.niche = {offset: 0, size: 1, value: 255};

typesByName.AssignmentTargetMaybeDefault.variants[1].discriminant = 0;
flattenEnumVariants('AssignmentTargetMaybeDefault', 'AssignmentTarget', 'AssignmentTarget');
typesByName.AssignmentTargetMaybeDefault.niche = {offset: 0, size: 1, value: 255};

// Link up types.
// Delete skipped fields from structs.
const structs = [];
Expand Down

0 comments on commit 781bf3a

Please sign in to comment.