Skip to content

Commit

Permalink
refactor: do not check reduce_vars option exists when running inline_…
Browse files Browse the repository at this point in the history
…into_symbolref
  • Loading branch information
fabiosantoscode committed Feb 2, 2023
1 parent 7f6187a commit 4020eb3
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 115 deletions.
233 changes: 119 additions & 114 deletions lib/compress/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ import "../size.js";
import "./evaluate.js";
import "./drop-side-effect-free.js";
import "./reduce-vars.js";
import { is_lhs } from "./inference.js";
import {
SQUEEZED,
INLINED,
Expand All @@ -114,6 +113,13 @@ import {
retain_top_func,
} from "./common.js";

/**
* Module that contains the inlining logic.
*
* @module
*
* The stars of the show are `inline_into_symbolref` and `inline_into_call`.
*/

function within_array_or_object_literal(compressor) {
var node, level = 0;
Expand Down Expand Up @@ -144,136 +150,135 @@ function scope_encloses_variables_in_this_scope(scope, pulled_scope) {

export function inline_into_symbolref(self, compressor) {
const parent = compressor.parent();
if (compressor.option("reduce_vars") && is_lhs(self, parent) !== self) {
const def = self.definition();
const nearest_scope = compressor.find_scope();
if (compressor.top_retain && def.global && compressor.top_retain(def)) {
def.fixed = false;
def.single_use = false;
return self;
}

let fixed = self.fixed_value();
let single_use = def.single_use
&& !(parent instanceof AST_Call
&& (parent.is_callee_pure(compressor))
|| has_annotation(parent, _NOINLINE))
&& !(parent instanceof AST_Export
&& fixed instanceof AST_Lambda
&& fixed.name);

if (single_use && fixed instanceof AST_Node) {
single_use =
!fixed.has_side_effects(compressor)
&& !fixed.may_throw(compressor);
}
const def = self.definition();
const nearest_scope = compressor.find_scope();
if (compressor.top_retain && def.global && compressor.top_retain(def)) {
def.fixed = false;
def.single_use = false;
return self;
}

if (single_use && (fixed instanceof AST_Lambda || fixed instanceof AST_Class)) {
if (retain_top_func(fixed, compressor)) {
single_use = false;
} else if (def.scope !== self.scope
&& (def.escaped == 1
|| has_flag(fixed, INLINED)
|| within_array_or_object_literal(compressor)
|| !compressor.option("reduce_funcs"))) {
single_use = false;
} else if (is_recursive_ref(compressor, def)) {
single_use = false;
} else if (def.scope !== self.scope || def.orig[0] instanceof AST_SymbolFunarg) {
single_use = fixed.is_constant_expression(self.scope);
if (single_use == "f") {
var scope = self.scope;
do {
if (scope instanceof AST_Defun || is_func_expr(scope)) {
set_flag(scope, INLINED);
}
} while (scope = scope.parent_scope);
}
let fixed = self.fixed_value();
let single_use = def.single_use
&& !(parent instanceof AST_Call
&& (parent.is_callee_pure(compressor))
|| has_annotation(parent, _NOINLINE))
&& !(parent instanceof AST_Export
&& fixed instanceof AST_Lambda
&& fixed.name);

if (single_use && fixed instanceof AST_Node) {
single_use =
!fixed.has_side_effects(compressor)
&& !fixed.may_throw(compressor);
}

if (single_use && (fixed instanceof AST_Lambda || fixed instanceof AST_Class)) {
if (retain_top_func(fixed, compressor)) {
single_use = false;
} else if (def.scope !== self.scope
&& (def.escaped == 1
|| has_flag(fixed, INLINED)
|| within_array_or_object_literal(compressor)
|| !compressor.option("reduce_funcs"))) {
single_use = false;
} else if (is_recursive_ref(compressor, def)) {
single_use = false;
} else if (def.scope !== self.scope || def.orig[0] instanceof AST_SymbolFunarg) {
single_use = fixed.is_constant_expression(self.scope);
if (single_use == "f") {
var scope = self.scope;
do {
if (scope instanceof AST_Defun || is_func_expr(scope)) {
set_flag(scope, INLINED);
}
} while (scope = scope.parent_scope);
}
}
}

if (single_use && (fixed instanceof AST_Lambda || fixed instanceof AST_Class)) {
single_use =
def.scope === self.scope
&& !scope_encloses_variables_in_this_scope(nearest_scope, fixed)
|| parent instanceof AST_Call
&& parent.expression === self
&& !scope_encloses_variables_in_this_scope(nearest_scope, fixed)
&& !(fixed.name && fixed.name.definition().recursive_refs > 0);
}
if (single_use && (fixed instanceof AST_Lambda || fixed instanceof AST_Class)) {
single_use =
def.scope === self.scope
&& !scope_encloses_variables_in_this_scope(nearest_scope, fixed)
|| parent instanceof AST_Call
&& parent.expression === self
&& !scope_encloses_variables_in_this_scope(nearest_scope, fixed)
&& !(fixed.name && fixed.name.definition().recursive_refs > 0);
}

if (single_use && fixed) {
if (fixed instanceof AST_DefClass) {
set_flag(fixed, SQUEEZED);
fixed = make_node(AST_ClassExpression, fixed, fixed);
}
if (fixed instanceof AST_Defun) {
set_flag(fixed, SQUEEZED);
fixed = make_node(AST_Function, fixed, fixed);
if (single_use && fixed) {
if (fixed instanceof AST_DefClass) {
set_flag(fixed, SQUEEZED);
fixed = make_node(AST_ClassExpression, fixed, fixed);
}
if (fixed instanceof AST_Defun) {
set_flag(fixed, SQUEEZED);
fixed = make_node(AST_Function, fixed, fixed);
}
if (def.recursive_refs > 0 && fixed.name instanceof AST_SymbolDefun) {
const defun_def = fixed.name.definition();
let lambda_def = fixed.variables.get(fixed.name.name);
let name = lambda_def && lambda_def.orig[0];
if (!(name instanceof AST_SymbolLambda)) {
name = make_node(AST_SymbolLambda, fixed.name, fixed.name);
name.scope = fixed;
fixed.name = name;
lambda_def = fixed.def_function(name);
}
if (def.recursive_refs > 0 && fixed.name instanceof AST_SymbolDefun) {
const defun_def = fixed.name.definition();
let lambda_def = fixed.variables.get(fixed.name.name);
let name = lambda_def && lambda_def.orig[0];
if (!(name instanceof AST_SymbolLambda)) {
name = make_node(AST_SymbolLambda, fixed.name, fixed.name);
name.scope = fixed;
fixed.name = name;
lambda_def = fixed.def_function(name);
walk(fixed, node => {
if (node instanceof AST_SymbolRef && node.definition() === defun_def) {
node.thedef = lambda_def;
lambda_def.references.push(node);
}
walk(fixed, node => {
if (node instanceof AST_SymbolRef && node.definition() === defun_def) {
node.thedef = lambda_def;
lambda_def.references.push(node);
}
});
});
}
if (
(fixed instanceof AST_Lambda || fixed instanceof AST_Class)
&& fixed.parent_scope !== nearest_scope
) {
fixed = fixed.clone(true, compressor.get_toplevel());

nearest_scope.add_child_scope(fixed);
}
return fixed.optimize(compressor);
}

// multiple uses
if (fixed) {
let replace;

if (fixed instanceof AST_This) {
if (!(def.orig[0] instanceof AST_SymbolFunarg)
&& def.references.every((ref) =>
def.scope === ref.scope
)) {
replace = fixed;
}
} else {
var ev = fixed.evaluate(compressor);
if (
(fixed instanceof AST_Lambda || fixed instanceof AST_Class)
&& fixed.parent_scope !== nearest_scope
ev !== fixed
&& (compressor.option("unsafe_regexp") || !(ev instanceof RegExp))
) {
fixed = fixed.clone(true, compressor.get_toplevel());

nearest_scope.add_child_scope(fixed);
replace = make_node_from_constant(ev, fixed);
}
return fixed.optimize(compressor);
}

// multiple uses
if (fixed) {
let replace;
if (replace) {
const name_length = self.size(compressor);
const replace_size = replace.size(compressor);

if (fixed instanceof AST_This) {
if (!(def.orig[0] instanceof AST_SymbolFunarg)
&& def.references.every((ref) =>
def.scope === ref.scope
)) {
replace = fixed;
}
} else {
var ev = fixed.evaluate(compressor);
if (
ev !== fixed
&& (compressor.option("unsafe_regexp") || !(ev instanceof RegExp))
) {
replace = make_node_from_constant(ev, fixed);
}
let overhead = 0;
if (compressor.option("unused") && !compressor.exposed(def)) {
overhead =
(name_length + 2 + replace_size) /
(def.references.length - def.assignments);
}

if (replace) {
const name_length = self.size(compressor);
const replace_size = replace.size(compressor);

let overhead = 0;
if (compressor.option("unused") && !compressor.exposed(def)) {
overhead =
(name_length + 2 + replace_size) /
(def.references.length - def.assignments);
}

if (replace_size <= name_length + overhead) {
return replace;
}
if (replace_size <= name_length + overhead) {
return replace;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/compress/reduce-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ function mark_lambda(tw, descend, compressor) {
let previous_safe_ids;
if (this instanceof AST_Defun || this.uses_arguments || this.pinned()) {
previous_safe_ids = tw.safe_ids;
tw.safe_ids = Object.create(null)
tw.safe_ids = Object.create(null);
} else {
push(tw);
}
Expand Down

0 comments on commit 4020eb3

Please sign in to comment.