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

Stack alloc reduce #4191

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions core/ast/src/expression/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ impl From<Expression> for PropertyAccessField {
}
}

impl From<Box<Expression>> for PropertyAccessField {
#[inline]
fn from(expr: Box<Expression>) -> Self {
Self::Expr(expr)
}
}

impl VisitWith for PropertyAccessField {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
Expand Down Expand Up @@ -151,12 +158,12 @@ impl SimplePropertyAccess {
}

/// Creates a `PropertyAccess` AST Expression.
pub fn new<F>(target: Expression, field: F) -> Self
pub fn new<F>(target: Box<Expression>, field: F) -> Self
where
F: Into<PropertyAccessField>,
{
Self {
target: target.into(),
target,
field: field.into(),
}
}
Expand Down Expand Up @@ -222,9 +229,9 @@ impl PrivatePropertyAccess {
/// Creates a `GetPrivateField` AST Expression.
#[inline]
#[must_use]
pub fn new(value: Expression, field: PrivateName) -> Self {
pub fn new(value: Box<Expression>, field: PrivateName) -> Self {
Self {
target: value.into(),
target: value,
field,
}
}
Expand Down
35 changes: 28 additions & 7 deletions core/ast/src/expression/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ impl Call {
}
}

/// Creates a new `Call` AST Expression from boxed function.
#[inline]
#[must_use]
pub fn new_boxed(function: Box<Expression>, args: Box<[Expression]>) -> Self {
Self { function, args }
}

/// Gets the target function of this call expression.
#[inline]
#[must_use]
Expand Down Expand Up @@ -71,6 +78,12 @@ impl From<Call> for Expression {
}
}

impl From<Call> for Box<Expression> {
fn from(call: Call) -> Self {
Box::new(Expression::Call(call))
}
}

impl VisitWith for Call {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
Expand Down Expand Up @@ -140,6 +153,12 @@ impl From<SuperCall> for Expression {
}
}

impl From<SuperCall> for Box<Expression> {
fn from(call: SuperCall) -> Self {
Box::new(Expression::SuperCall(call))
}
}

impl VisitWith for SuperCall {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
Expand Down Expand Up @@ -181,13 +200,9 @@ pub struct ImportCall {

impl ImportCall {
/// Creates a new `ImportCall` AST node.
pub fn new<A>(arg: A) -> Self
where
A: Into<Expression>,
{
Self {
arg: Box::new(arg.into()),
}
#[must_use]
pub fn new(arg: Box<Expression>) -> Self {
Self { arg }
}

/// Retrieves the single argument of the import call.
Expand All @@ -211,6 +226,12 @@ impl From<ImportCall> for Expression {
}
}

impl From<ImportCall> for Box<Expression> {
fn from(call: ImportCall) -> Self {
Box::new(Expression::ImportCall(call))
}
}

impl VisitWith for ImportCall {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
Expand Down
6 changes: 6 additions & 0 deletions core/ast/src/expression/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ impl From<Identifier> for Expression {
}
}

impl From<Identifier> for Box<Expression> {
fn from(local: Identifier) -> Self {
Box::new(Expression::Identifier(local))
}
}

impl VisitWith for Identifier {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
Expand Down
18 changes: 12 additions & 6 deletions core/ast/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,22 @@ pub enum Expression {
Spread(Spread),

/// See [`FunctionExpression`].
FunctionExpression(FunctionExpression),
FunctionExpression(Box<FunctionExpression>),

/// See [`ArrowFunction`].
ArrowFunction(ArrowFunction),
ArrowFunction(Box<ArrowFunction>),

/// See [`AsyncArrowFunction`].
AsyncArrowFunction(AsyncArrowFunction),
AsyncArrowFunction(Box<AsyncArrowFunction>),

/// See [`GeneratorExpression`].
GeneratorExpression(GeneratorExpression),
GeneratorExpression(Box<GeneratorExpression>),

/// See [`AsyncFunctionExpression`].
AsyncFunctionExpression(AsyncFunctionExpression),
AsyncFunctionExpression(Box<AsyncFunctionExpression>),

/// See [`AsyncGeneratorExpression`].
AsyncGeneratorExpression(AsyncGeneratorExpression),
AsyncGeneratorExpression(Box<AsyncGeneratorExpression>),

/// See [`ClassExpression`].
ClassExpression(Box<ClassExpression>),
Expand Down Expand Up @@ -277,6 +277,12 @@ impl Expression {
}
expression
}

/// Create boxed expression.
#[must_use]
pub fn boxed(f: impl FnOnce() -> Self) -> Box<Self> {
Box::new(f())
}
}

impl From<Expression> for Statement {
Expand Down
6 changes: 6 additions & 0 deletions core/ast/src/expression/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ impl From<New> for Expression {
}
}

impl From<New> for Box<Expression> {
fn from(new: New) -> Self {
Box::new(Expression::New(new))
}
}

impl VisitWith for New {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
Expand Down
7 changes: 7 additions & 0 deletions core/ast/src/expression/operator/assign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ impl Assign {
}
}

/// Creates an `Assign` AST Expression with boxed values.
#[inline]
#[must_use]
pub fn new_boxed(op: AssignOp, lhs: Box<AssignTarget>, rhs: Box<Expression>) -> Self {
Self { op, lhs, rhs }
}

/// Gets the operator of the assignment operation.
#[inline]
#[must_use]
Expand Down
38 changes: 38 additions & 0 deletions core/ast/src/expression/operator/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ impl Binary {
}
}

/// Creates a `BinOp` AST Expression with boxed values.
#[inline]
#[must_use]
pub fn new_boxed(op: BinaryOp, lhs: Box<Expression>, rhs: Box<Expression>) -> Self {
Self { op, lhs, rhs }
}

/// Creates an `Box<Expression::Binary>` from boxed values.
// Non-inline to reduce stack alloc size: most likely, it will be inlined in release anyway.
#[must_use]
pub fn new_boxed_expr(
op: BinaryOp,
lhs: Box<Expression>,
rhs: Box<Expression>,
) -> Box<Expression> {
Box::new(Self::new_boxed(op, lhs, rhs).into())
}

/// Gets the binary operation of the Expression.
#[inline]
#[must_use]
Expand Down Expand Up @@ -105,6 +123,13 @@ impl From<Binary> for Expression {
}
}

impl From<Binary> for Box<Expression> {
#[inline]
fn from(op: Binary) -> Self {
Box::new(Expression::Binary(op))
}
}

impl VisitWith for Binary {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
Expand Down Expand Up @@ -147,6 +172,13 @@ impl BinaryInPrivate {
}
}

/// Creates a `BinaryInPrivate` AST Expression.
#[inline]
#[must_use]
pub fn new_boxed(lhs: PrivateName, rhs: Box<Expression>) -> Self {
Self { lhs, rhs }
}

/// Gets the left hand side of the binary operation.
#[inline]
#[must_use]
Expand Down Expand Up @@ -180,6 +212,12 @@ impl From<BinaryInPrivate> for Expression {
}
}

impl From<BinaryInPrivate> for Box<Expression> {
fn from(op: BinaryInPrivate) -> Self {
Box::new(Expression::BinaryInPrivate(op))
}
}

impl VisitWith for BinaryInPrivate {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
Expand Down
19 changes: 15 additions & 4 deletions core/ast/src/expression/operator/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ impl Conditional {
/// Creates a `Conditional` AST Expression.
#[inline]
#[must_use]
pub fn new(condition: Expression, if_true: Expression, if_false: Expression) -> Self {
pub fn new(
condition: Box<Expression>,
if_true: Box<Expression>,
if_false: Box<Expression>,
) -> Self {
Self {
condition: Box::new(condition),
if_true: Box::new(if_true),
if_false: Box::new(if_false),
condition,
if_true,
if_false,
}
}
}
Expand All @@ -81,6 +85,13 @@ impl From<Conditional> for Expression {
}
}

impl From<Conditional> for Box<Expression> {
#[inline]
fn from(cond_op: Conditional) -> Self {
Box::new(Expression::Conditional(cond_op))
}
}

impl VisitWith for Conditional {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
Expand Down
13 changes: 13 additions & 0 deletions core/ast/src/expression/operator/unary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ impl Unary {
}
}

/// Creates a new `UnaryOp` AST Expression with boxed expression.
#[inline]
#[must_use]
pub fn new_boxed(op: UnaryOp, target: Box<Expression>) -> Self {
Self { op, target }
}

/// Gets the unary operation of the Expression.
#[inline]
#[must_use]
Expand Down Expand Up @@ -83,6 +90,12 @@ impl From<Unary> for Expression {
}
}

impl From<Unary> for Box<Expression> {
fn from(op: Unary) -> Self {
Box::new(Expression::Unary(op))
}
}

impl VisitWith for Unary {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
Expand Down
9 changes: 9 additions & 0 deletions core/ast/src/expression/operator/update/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ impl UpdateOp {
Self::DecrementPost | Self::DecrementPre => "--",
}
}

/// Return true if self is Inc op
#[must_use]
pub const fn is_inc(self) -> bool {
match self {
Self::IncrementPost | Self::IncrementPre => true,
Self::DecrementPost | Self::DecrementPre => false,
}
}
}

impl std::fmt::Display for UpdateOp {
Expand Down
13 changes: 8 additions & 5 deletions core/ast/src/expression/optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,8 @@ impl Optional {
/// Creates a new `Optional` expression.
#[inline]
#[must_use]
pub fn new(target: Expression, chain: Box<[OptionalOperation]>) -> Self {
Self {
target: Box::new(target),
chain,
}
pub fn new(target: Box<Expression>, chain: Box<[OptionalOperation]>) -> Self {
Self { target, chain }
}

/// Gets the target of this `Optional` expression.
Expand All @@ -236,6 +233,12 @@ impl From<Optional> for Expression {
}
}

impl From<Optional> for Box<Expression> {
fn from(opt: Optional) -> Self {
Box::new(Expression::Optional(opt))
}
}

impl ToInternedString for Optional {
fn to_interned_string(&self, interner: &Interner) -> String {
let mut buf = self.target.to_interned_string(interner);
Expand Down
10 changes: 8 additions & 2 deletions core/ast/src/expression/tagged_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ impl TaggedTemplate {
#[inline]
#[must_use]
pub fn new(
tag: Expression,
tag: Box<Expression>,
raws: Box<[Sym]>,
cookeds: Box<[Option<Sym>]>,
exprs: Box<[Expression]>,
identifier: u64,
) -> Self {
Self {
tag: tag.into(),
tag,
raws,
cookeds,
exprs,
Expand Down Expand Up @@ -105,6 +105,12 @@ impl From<TaggedTemplate> for Expression {
}
}

impl From<Box<TaggedTemplate>> for Box<Expression> {
fn from(boxed_template: Box<TaggedTemplate>) -> Self {
Box::new(Expression::TaggedTemplate(*boxed_template))
}
}

impl VisitWith for TaggedTemplate {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
Expand Down
Loading
Loading