Skip to content

Commit

Permalink
perf(syntax): reorder operator enum variants (#7351)
Browse files Browse the repository at this point in the history
Re-order enum variants of `AssignmentOperator`, `BinaryOperator` and `UnaryOperator`.

* `Exponential` moved to after `Remainder` (so with the rest of the arithmetic operators).
* `Shift*` operators follow arithmetic operators.
* `AssignmentOperator::Bitwise*` ops moved to before `Logical*` ops (so all ops which correspond to `BinaryOperator`s are together).
* `*Or` always before `*And`.
* Plus/Addition always before Minus/Subtraction.

The purpose is to make the various methods on these types maximally efficient:

1. Group together variants so that `AssignmentOperator::is_*` methods can be executed with the minimum number of operations (essentially `variant - min <= max`).
2. Align the variants of `AssignmentOperator` and `BinaryOperator` so that conversion methods added in #7350 become very cheap too (essentially `if variant - min <= max { Some(variant + offset) } else { None }`).
  • Loading branch information
overlookmotel committed Nov 19, 2024
1 parent 2534cde commit 8e3adab
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 111 deletions.
14 changes: 7 additions & 7 deletions crates/oxc_syntax/src/generated/derive_clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ impl<'alloc> CloneIn<'alloc> for AssignmentOperator {
Self::Multiplication => AssignmentOperator::Multiplication,
Self::Division => AssignmentOperator::Division,
Self::Remainder => AssignmentOperator::Remainder,
Self::Exponential => AssignmentOperator::Exponential,
Self::ShiftLeft => AssignmentOperator::ShiftLeft,
Self::ShiftRight => AssignmentOperator::ShiftRight,
Self::ShiftRightZeroFill => AssignmentOperator::ShiftRightZeroFill,
Self::BitwiseOR => AssignmentOperator::BitwiseOR,
Self::BitwiseXOR => AssignmentOperator::BitwiseXOR,
Self::BitwiseAnd => AssignmentOperator::BitwiseAnd,
Self::LogicalAnd => AssignmentOperator::LogicalAnd,
Self::LogicalOr => AssignmentOperator::LogicalOr,
Self::LogicalAnd => AssignmentOperator::LogicalAnd,
Self::LogicalNullish => AssignmentOperator::LogicalNullish,
Self::Exponential => AssignmentOperator::Exponential,
}
}
}
Expand All @@ -70,20 +70,20 @@ impl<'alloc> CloneIn<'alloc> for BinaryOperator {
Self::LessEqualThan => BinaryOperator::LessEqualThan,
Self::GreaterThan => BinaryOperator::GreaterThan,
Self::GreaterEqualThan => BinaryOperator::GreaterEqualThan,
Self::ShiftLeft => BinaryOperator::ShiftLeft,
Self::ShiftRight => BinaryOperator::ShiftRight,
Self::ShiftRightZeroFill => BinaryOperator::ShiftRightZeroFill,
Self::Addition => BinaryOperator::Addition,
Self::Subtraction => BinaryOperator::Subtraction,
Self::Multiplication => BinaryOperator::Multiplication,
Self::Division => BinaryOperator::Division,
Self::Remainder => BinaryOperator::Remainder,
Self::Exponential => BinaryOperator::Exponential,
Self::ShiftLeft => BinaryOperator::ShiftLeft,
Self::ShiftRight => BinaryOperator::ShiftRight,
Self::ShiftRightZeroFill => BinaryOperator::ShiftRightZeroFill,
Self::BitwiseOR => BinaryOperator::BitwiseOR,
Self::BitwiseXOR => BinaryOperator::BitwiseXOR,
Self::BitwiseAnd => BinaryOperator::BitwiseAnd,
Self::In => BinaryOperator::In,
Self::Instanceof => BinaryOperator::Instanceof,
Self::Exponential => BinaryOperator::Exponential,
}
}
}
Expand All @@ -103,8 +103,8 @@ impl<'alloc> CloneIn<'alloc> for UnaryOperator {
type Cloned = UnaryOperator;
fn clone_in(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::UnaryNegation => UnaryOperator::UnaryNegation,
Self::UnaryPlus => UnaryOperator::UnaryPlus,
Self::UnaryNegation => UnaryOperator::UnaryNegation,
Self::LogicalNot => UnaryOperator::LogicalNot,
Self::BitwiseNot => UnaryOperator::BitwiseNot,
Self::Typeof => UnaryOperator::Typeof,
Expand Down
78 changes: 39 additions & 39 deletions crates/oxc_syntax/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,35 @@ impl Serialize for AssignmentOperator {
AssignmentOperator::Remainder => {
serializer.serialize_unit_variant("AssignmentOperator", 5u32, "%=")
}
AssignmentOperator::Exponential => {
serializer.serialize_unit_variant("AssignmentOperator", 6u32, "**=")
}
AssignmentOperator::ShiftLeft => {
serializer.serialize_unit_variant("AssignmentOperator", 6u32, "<<=")
serializer.serialize_unit_variant("AssignmentOperator", 7u32, "<<=")
}
AssignmentOperator::ShiftRight => {
serializer.serialize_unit_variant("AssignmentOperator", 7u32, ">>=")
serializer.serialize_unit_variant("AssignmentOperator", 8u32, ">>=")
}
AssignmentOperator::ShiftRightZeroFill => {
serializer.serialize_unit_variant("AssignmentOperator", 8u32, ">>>=")
serializer.serialize_unit_variant("AssignmentOperator", 9u32, ">>>=")
}
AssignmentOperator::BitwiseOR => {
serializer.serialize_unit_variant("AssignmentOperator", 9u32, "|=")
serializer.serialize_unit_variant("AssignmentOperator", 10u32, "|=")
}
AssignmentOperator::BitwiseXOR => {
serializer.serialize_unit_variant("AssignmentOperator", 10u32, "^=")
serializer.serialize_unit_variant("AssignmentOperator", 11u32, "^=")
}
AssignmentOperator::BitwiseAnd => {
serializer.serialize_unit_variant("AssignmentOperator", 11u32, "&=")
}
AssignmentOperator::LogicalAnd => {
serializer.serialize_unit_variant("AssignmentOperator", 12u32, "&&=")
serializer.serialize_unit_variant("AssignmentOperator", 12u32, "&=")
}
AssignmentOperator::LogicalOr => {
serializer.serialize_unit_variant("AssignmentOperator", 13u32, "||=")
}
AssignmentOperator::LogicalNullish => {
serializer.serialize_unit_variant("AssignmentOperator", 14u32, "??=")
AssignmentOperator::LogicalAnd => {
serializer.serialize_unit_variant("AssignmentOperator", 14u32, "&&=")
}
AssignmentOperator::Exponential => {
serializer.serialize_unit_variant("AssignmentOperator", 15u32, "**=")
AssignmentOperator::LogicalNullish => {
serializer.serialize_unit_variant("AssignmentOperator", 15u32, "??=")
}
}
}
Expand Down Expand Up @@ -89,45 +89,45 @@ impl Serialize for BinaryOperator {
BinaryOperator::GreaterEqualThan => {
serializer.serialize_unit_variant("BinaryOperator", 7u32, ">=")
}
BinaryOperator::ShiftLeft => {
serializer.serialize_unit_variant("BinaryOperator", 8u32, "<<")
}
BinaryOperator::ShiftRight => {
serializer.serialize_unit_variant("BinaryOperator", 9u32, ">>")
}
BinaryOperator::ShiftRightZeroFill => {
serializer.serialize_unit_variant("BinaryOperator", 10u32, ">>>")
}
BinaryOperator::Addition => {
serializer.serialize_unit_variant("BinaryOperator", 11u32, "+")
serializer.serialize_unit_variant("BinaryOperator", 8u32, "+")
}
BinaryOperator::Subtraction => {
serializer.serialize_unit_variant("BinaryOperator", 12u32, "-")
serializer.serialize_unit_variant("BinaryOperator", 9u32, "-")
}
BinaryOperator::Multiplication => {
serializer.serialize_unit_variant("BinaryOperator", 13u32, "*")
serializer.serialize_unit_variant("BinaryOperator", 10u32, "*")
}
BinaryOperator::Division => {
serializer.serialize_unit_variant("BinaryOperator", 14u32, "/")
serializer.serialize_unit_variant("BinaryOperator", 11u32, "/")
}
BinaryOperator::Remainder => {
serializer.serialize_unit_variant("BinaryOperator", 15u32, "%")
serializer.serialize_unit_variant("BinaryOperator", 12u32, "%")
}
BinaryOperator::Exponential => {
serializer.serialize_unit_variant("BinaryOperator", 13u32, "**")
}
BinaryOperator::ShiftLeft => {
serializer.serialize_unit_variant("BinaryOperator", 14u32, "<<")
}
BinaryOperator::ShiftRight => {
serializer.serialize_unit_variant("BinaryOperator", 15u32, ">>")
}
BinaryOperator::ShiftRightZeroFill => {
serializer.serialize_unit_variant("BinaryOperator", 16u32, ">>>")
}
BinaryOperator::BitwiseOR => {
serializer.serialize_unit_variant("BinaryOperator", 16u32, "|")
serializer.serialize_unit_variant("BinaryOperator", 17u32, "|")
}
BinaryOperator::BitwiseXOR => {
serializer.serialize_unit_variant("BinaryOperator", 17u32, "^")
serializer.serialize_unit_variant("BinaryOperator", 18u32, "^")
}
BinaryOperator::BitwiseAnd => {
serializer.serialize_unit_variant("BinaryOperator", 18u32, "&")
serializer.serialize_unit_variant("BinaryOperator", 19u32, "&")
}
BinaryOperator::In => serializer.serialize_unit_variant("BinaryOperator", 19u32, "in"),
BinaryOperator::In => serializer.serialize_unit_variant("BinaryOperator", 20u32, "in"),
BinaryOperator::Instanceof => {
serializer.serialize_unit_variant("BinaryOperator", 20u32, "instanceof")
}
BinaryOperator::Exponential => {
serializer.serialize_unit_variant("BinaryOperator", 21u32, "**")
serializer.serialize_unit_variant("BinaryOperator", 21u32, "instanceof")
}
}
}
Expand All @@ -150,11 +150,11 @@ impl Serialize for LogicalOperator {
impl Serialize for UnaryOperator {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match *self {
UnaryOperator::UnaryNegation => {
serializer.serialize_unit_variant("UnaryOperator", 0u32, "-")
}
UnaryOperator::UnaryPlus => {
serializer.serialize_unit_variant("UnaryOperator", 1u32, "+")
serializer.serialize_unit_variant("UnaryOperator", 0u32, "+")
}
UnaryOperator::UnaryNegation => {
serializer.serialize_unit_variant("UnaryOperator", 1u32, "-")
}
UnaryOperator::LogicalNot => {
serializer.serialize_unit_variant("UnaryOperator", 2u32, "!")
Expand Down
Loading

0 comments on commit 8e3adab

Please sign in to comment.