Repository: swc-project/swc
File: crates/swc_ecma_minifier/src/compress/optimize/inline.rs (Rust)

Code before:
    }

    match &*e.body {
        BlockStmtOrExpr::BlockStmt(s) => is_block_stmt_of_fn_simple_enough_for_copy(s),
        BlockStmtOrExpr::Expr(e) => is_arrow_body_simple_enough_for_copy(e),
    }
}

fn is_arrow_body_simple_enough_for_copy(e: &Expr) -> Option<u8> {
    match e {
        Expr::Ident(..) | Expr::Lit(..) => return Some(1),
        Expr::Member(MemberExpr { obj, prop, .. }) if !prop.is_computed() => {
            return Some(is_arrow_body_simple_enough_for_copy(obj)? + 2)
        }
        Expr::Call(c) => {
            let mut arg_cost = 0;
            for arg in &c.args {
                let cost = is_arrow_body_simple_enough_for_copy(&arg.expr)?;
                arg_cost += cost;
                arg_cost += if arg.spread.is_some() { 3 } else { 0 };
            }

            return Some(arg_cost + 2);
        }
        Expr::Unary(u) => return Some(is_arrow_body_simple_enough_for_copy(&u.arg)? + 1),

        Expr::Bin(b) => {
            return Some(
                is_arrow_body_simple_enough_for_copy(&b.left)?
                    + is_arrow_body_simple_enough_for_copy(&b.right)?
                    + 2,
            )
        }
        _ => {}
    }

    None
}

fn is_block_stmt_of_fn_simple_enough_for_copy(b: &BlockStmt) -> Option<u8> {
    if b.stmts.len() == 1 {
        if let Stmt::Return(ret) = &b.stmts[0] {
            return ret
                .arg
                .as_deref()
                .map_or(Some(0), is_arrow_body_simple_enough_for_copy);
        }
    }

    None
}

Review comment (suggestion):
This is a very rough estimation, it might cause some problems later -- but at this point perf improves.

Code after:
    if e.is_async {
        return None;
    }

    match &*e.body {
        BlockStmtOrExpr::BlockStmt(s) => is_block_stmt_of_fn_simple_enough_for_copy(s),
        BlockStmtOrExpr::Expr(e) => is_arrow_body_simple_enough_for_copy(e),
    }
}

fn is_arrow_body_simple_enough_for_copy(e: &Expr) -> Option<u8> {
    match e {
        Expr::Ident(..) | Expr::Lit(..) => return Some(1),
        Expr::Member(MemberExpr { obj, prop, .. }) if !prop.is_computed() => {
            return Some(is_arrow_body_simple_enough_for_copy(obj)? + 2)
        }
        Expr::Call(c) => {
            let mut cost = is_arrow_body_simple_enough_for_copy(c.callee.as_expr()?.deref())?;
            for arg in &c.args {
                let arg_cost = is_arrow_body_simple_enough_for_copy(&arg.expr)?;
                cost += arg_cost;
                cost += if arg.spread.is_some() { 3 } else { 0 };
            }

            return Some(cost + 2);
        }
        Expr::Unary(u) => return Some(is_arrow_body_simple_enough_for_copy(&u.arg)? + 1),

        Expr::Bin(b) => {
            return Some(
                is_arrow_body_simple_enough_for_copy(&b.left)?
                    + is_arrow_body_simple_enough_for_copy(&b.right)?
                    + 2,
            )
        }
        _ => {}
    }

    None
}

fn is_block_stmt_of_fn_simple_enough_for_copy(b: &BlockStmt) -> Option<u8> {
    if b.stmts.len() == 1 {
        if let Stmt::Return(ret) = &b.stmts[0] {
            return ret
                .arg
                .as_deref()
                .map_or(Some(0), is_arrow_body_simple_enough_for_copy);
        }
    }