fix(es/minifier): Don't inline a function if we need referential equality (#4665)

This commit is contained in:
Donny/강동윤 2022-05-15 18:37:34 +09:00 committed by GitHub
parent c5b92a6d83
commit 96cec97b2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 93 additions and 39 deletions

View File

@ -19,7 +19,7 @@ var objA = new A();
void 0 === BOOLEAN || swcHelpers.typeOf(BOOLEAN), swcHelpers.typeOf(!0), swcHelpers.typeOf({ void 0 === BOOLEAN || swcHelpers.typeOf(BOOLEAN), swcHelpers.typeOf(!0), swcHelpers.typeOf({
x: !0, x: !0,
y: !1 y: !1
}), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(!0), swcHelpers.typeOf(A.foo()), swcHelpers.typeOf(void 0 === BOOLEAN ? "undefined" : swcHelpers.typeOf(BOOLEAN)), swcHelpers.typeOf(!0), void 0 === BOOLEAN || swcHelpers.typeOf(BOOLEAN), swcHelpers.typeOf(!0), swcHelpers.typeOf(!0), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n); }), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(foo()), swcHelpers.typeOf(A.foo()), swcHelpers.typeOf(void 0 === BOOLEAN ? "undefined" : swcHelpers.typeOf(BOOLEAN)), swcHelpers.typeOf(!0), void 0 === BOOLEAN || swcHelpers.typeOf(BOOLEAN), swcHelpers.typeOf(foo()), swcHelpers.typeOf(!0), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n);
z: void 0 === BOOLEAN || swcHelpers.typeOf(BOOLEAN); z: void 0 === BOOLEAN || swcHelpers.typeOf(BOOLEAN);
r: swcHelpers.typeOf(foo); r: swcHelpers.typeOf(foo);
z: swcHelpers.typeOf(!0); z: swcHelpers.typeOf(!0);

View File

@ -28,7 +28,7 @@ void 0 === NUMBER || swcHelpers.typeOf(NUMBER), swcHelpers.typeOf(NUMBER1), swcH
y: function(n) { y: function(n) {
return n; return n;
} }
}), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(NUMBER1[0]), swcHelpers.typeOf(1), swcHelpers.typeOf(A.foo()), swcHelpers.typeOf(NUMBER + NUMBER), swcHelpers.typeOf(void 0 === NUMBER ? "undefined" : swcHelpers.typeOf(NUMBER)), swcHelpers.typeOf(swcHelpers.typeOf(swcHelpers.typeOf(NUMBER + NUMBER))), swcHelpers.typeOf(1), void 0 === NUMBER || swcHelpers.typeOf(NUMBER), swcHelpers.typeOf(NUMBER1), swcHelpers.typeOf(1), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(objA.a), M.n; }), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(NUMBER1[0]), swcHelpers.typeOf(foo()), swcHelpers.typeOf(A.foo()), swcHelpers.typeOf(NUMBER + NUMBER), swcHelpers.typeOf(void 0 === NUMBER ? "undefined" : swcHelpers.typeOf(NUMBER)), swcHelpers.typeOf(swcHelpers.typeOf(swcHelpers.typeOf(NUMBER + NUMBER))), swcHelpers.typeOf(1), void 0 === NUMBER || swcHelpers.typeOf(NUMBER), swcHelpers.typeOf(NUMBER1), swcHelpers.typeOf(foo()), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(objA.a), M.n;
z: void 0 === NUMBER || swcHelpers.typeOf(NUMBER); z: void 0 === NUMBER || swcHelpers.typeOf(NUMBER);
x: swcHelpers.typeOf(NUMBER1); x: swcHelpers.typeOf(NUMBER1);
r: swcHelpers.typeOf(foo); r: swcHelpers.typeOf(foo);

View File

@ -28,7 +28,7 @@ void 0 === STRING || swcHelpers.typeOf(STRING), swcHelpers.typeOf(STRING1), swcH
y: function(s) { y: function(s) {
return s; return s;
} }
}), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(STRING1[0]), swcHelpers.typeOf("abc"), swcHelpers.typeOf(A.foo()), swcHelpers.typeOf(STRING + STRING), swcHelpers.typeOf(STRING.charAt(0)), swcHelpers.typeOf(void 0 === STRING ? "undefined" : swcHelpers.typeOf(STRING)), swcHelpers.typeOf(swcHelpers.typeOf(swcHelpers.typeOf(STRING + STRING))), swcHelpers.typeOf(""), void 0 === STRING || swcHelpers.typeOf(STRING), swcHelpers.typeOf(STRING1), swcHelpers.typeOf("abc"), swcHelpers.typeOf(objA.a), M.n; }), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(STRING1[0]), swcHelpers.typeOf(foo()), swcHelpers.typeOf(A.foo()), swcHelpers.typeOf(STRING + STRING), swcHelpers.typeOf(STRING.charAt(0)), swcHelpers.typeOf(void 0 === STRING ? "undefined" : swcHelpers.typeOf(STRING)), swcHelpers.typeOf(swcHelpers.typeOf(swcHelpers.typeOf(STRING + STRING))), swcHelpers.typeOf(""), void 0 === STRING || swcHelpers.typeOf(STRING), swcHelpers.typeOf(STRING1), swcHelpers.typeOf(foo()), swcHelpers.typeOf(objA.a), M.n;
z: void 0 === STRING || swcHelpers.typeOf(STRING); z: void 0 === STRING || swcHelpers.typeOf(STRING);
x: swcHelpers.typeOf(STRING1); x: swcHelpers.typeOf(STRING1);
r: swcHelpers.typeOf(foo); r: swcHelpers.typeOf(foo);

View File

@ -8,13 +8,6 @@ function curry(f, ...a) {
return (...b)=>f(...a, ...b) return (...b)=>f(...a, ...b)
; ;
} }
function curry2(f, t, u) {
return f(...t, ...u);
}
function callApi(method) {
return (...args)=>method(...args, {})
;
}
concat([], []), concat([ concat([], []), concat([
'hello' 'hello'
], [ ], [
@ -52,22 +45,26 @@ concat([], []), concat([
[ [
'def' 'def'
] ]
]), curry((a, b, c, d)=>0 ]);
), curry((a, b, c, d)=>0 const fn1 = (a, b, c, d)=>0
, 1), curry((a, b, c, d)=>0 ;
, 1, 'abc'), curry((a, b, c, d)=>0 curry(fn1), curry(fn1, 1), curry(fn1, 1, 'abc'), curry(fn1, 1, 'abc', !0), curry(fn1, 1, 'abc', !0, [
, 1, 'abc', !0), curry((a, b, c, d)=>0
, 1, 'abc', !0, [
'x', 'x',
'y' 'y'
]), curry((x, b, ...args)=>0 ]);
), curry((x, b, ...args)=>0 const fn2 = (x, b, ...args)=>0
, 1), curry((x, b, ...args)=>0 ;
, 1, !0), curry((x, b, ...args)=>0 curry(fn2), curry(fn2, 1), curry(fn2, 1, !0), curry(fn2, 1, !0, 'abc', 'def');
, 1, !0, 'abc', 'def'), curry((...args)=>0 const fn3 = (...args)=>0
), curry((...args)=>0 ;
, 'abc', 'def'), curry((...args)=>0 function curry2(f, t, u) {
, ...sa), curry2(fn10, [ return f(...t, ...u);
}
function callApi(method) {
return (...args)=>method(...args, {})
;
}
curry(fn3), curry(fn3, 'abc', 'def'), curry(fn3, ...sa), curry2(fn10, [
'hello', 'hello',
42 42
], [ ], [

View File

@ -0,0 +1,50 @@
#!/usr/bin/env bash
#
# Usage:
#
# From your clone of next.js, you can invoke this script like
#
# /absolute/path/to/this/script.sh examples/foo
#
# This script will
#
# - build native binary (`next-swc`)
# - install dependnecies using `yarn``
# - remove some dependencies (`next`, `react`, `react-dom`)
# - yarn next build examples/foo
# - yarn next start examples/foo
set -eu
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
dir="$1"
nextJsDir="$(pwd)"
# Ensure that next-swc is up to date
echo "----- ⚠️ Building next-swc"
(cd ./packages/next-swc && yarn build-native)
# Install dependencies
echo "----- ⚠️ Installing dependencies"
if [ ! -d "$dir/node_modules" ]; then
if test -f "$dir/yarn.lock"; then
echo " Using yarn"
# (cd $dir && yarn)
else
echo " Using yarn"
# (cd $dir && npm ci)
fi
fi
echo "----- ⚠️ Removing cache"
(cd $dir && rm -rf .next)
echo "----- ⚠️ Replacing swc binary"
mv packages/next-swc/native/*.node $dir/../node_modules/@next/swc-*/
ls -alh $dir/../node_modules/@next/swc-*/
# Build and start
echo "----- ⚠️ Building the app using next"
(cd $dir && npx next build)
(cd $dir && npx next start)

View File

@ -157,7 +157,10 @@ where
op: op!("!"), arg, .. op: op!("!"), arg, ..
}) => arg.is_lit(), }) => arg.is_lit(),
Expr::This(..) => usage.is_fn_local, Expr::This(..) => usage.is_fn_local,
Expr::Arrow(arr) => is_arrow_simple_enough_for_copy(arr), Expr::Arrow(arr) => {
!(usage.used_as_arg && usage.ref_count > 1)
&& is_arrow_simple_enough_for_copy(arr)
}
_ => false, _ => false,
} }
{ {
@ -251,7 +254,7 @@ where
} }
if usage.used_as_arg && !usage.is_fn_local { if usage.used_as_arg && !usage.is_fn_local {
if let Expr::Fn(..) = &**init { if let Expr::Fn(..) | Expr::Arrow(..) = &**init {
return; return;
} }
} }
@ -466,7 +469,12 @@ where
if let Some(usage) = self.data.vars.get(&i.to_id()) { if let Some(usage) = self.data.vars.get(&i.to_id()) {
if usage.declared_as_catch_param { if usage.declared_as_catch_param {
log_abort!("inline: [x] Declared as a catch parameter"); log_abort!("inline: Declared as a catch parameter");
return;
}
if usage.used_as_arg && usage.ref_count > 1 {
log_abort!("inline: Used as an arugment");
return; return;
} }

View File

@ -1,23 +1,13 @@
asm/asm_mixed/input.js asm/asm_mixed/input.js
collapse_vars/collapse_vars_do_while/input.js collapse_vars/collapse_vars_do_while/input.js
collapse_vars/collapse_vars_lvalues/input.js
collapse_vars/collapse_vars_lvalues_drop_assign/input.js
collapse_vars/collapse_vars_misc1/input.js collapse_vars/collapse_vars_misc1/input.js
collapse_vars/collapse_vars_short_circuited_conditions/input.js
collapse_vars/collapse_vars_side_effects_2/input.js
collapse_vars/issue_2497/input.js collapse_vars/issue_2497/input.js
conditionals/ifs_5/input.js
drop_unused/keep_assign/input.js drop_unused/keep_assign/input.js
drop_unused/reassign_const/input.js drop_unused/reassign_const/input.js
drop_unused/var_catch_toplevel/input.js drop_unused/var_catch_toplevel/input.js
evaluate/issue_2535_1/input.js evaluate/issue_2535_1/input.js
harmony/array_literal_with_spread_4a/input.js harmony/array_literal_with_spread_4a/input.js
harmony/default_assign/input.js
if_return/if_var_return/input.js
inline/dont_inline_funcs_into_default_param_2/input.js inline/dont_inline_funcs_into_default_param_2/input.js
inline/inline_into_scope_conflict/input.js
issue_1034/non_hoisted_function_after_return_2a/input.js
issue_1034/non_hoisted_function_after_return_2b/input.js
issue_1052/defun_else_if_return/input.js issue_1052/defun_else_if_return/input.js
issue_1052/defun_if_return/input.js issue_1052/defun_if_return/input.js
issue_281/inner_var_for_in_1/input.js issue_281/inner_var_for_in_1/input.js

View File

@ -532,6 +532,7 @@ expression/pow_with_parentheses/input.js
expression/pow_with_unary_between_brackets/input.js expression/pow_with_unary_between_brackets/input.js
functions/avoid_generating_duplicate_functions_compared_together/input.js functions/avoid_generating_duplicate_functions_compared_together/input.js
functions/avoid_generating_duplicate_functions_compared_together_2/input.js functions/avoid_generating_duplicate_functions_compared_together_2/input.js
functions/avoid_generating_duplicate_functions_compared_together_3/input.js
functions/avoid_generating_duplicate_functions_compared_together_4/input.js functions/avoid_generating_duplicate_functions_compared_together_4/input.js
functions/deduplicate_parenthesis/input.js functions/deduplicate_parenthesis/input.js
functions/drop_lone_use_strict/input.js functions/drop_lone_use_strict/input.js

View File

@ -39,11 +39,15 @@ collapse_vars/collapse_vars_assignment/input.js
collapse_vars/collapse_vars_constants/input.js collapse_vars/collapse_vars_constants/input.js
collapse_vars/collapse_vars_do_while_drop_assign/input.js collapse_vars/collapse_vars_do_while_drop_assign/input.js
collapse_vars/collapse_vars_if/input.js collapse_vars/collapse_vars_if/input.js
collapse_vars/collapse_vars_lvalues/input.js
collapse_vars/collapse_vars_lvalues_drop_assign/input.js
collapse_vars/collapse_vars_object/input.js collapse_vars/collapse_vars_object/input.js
collapse_vars/collapse_vars_repeated/input.js collapse_vars/collapse_vars_repeated/input.js
collapse_vars/collapse_vars_self_reference/input.js collapse_vars/collapse_vars_self_reference/input.js
collapse_vars/collapse_vars_short_circuit/input.js collapse_vars/collapse_vars_short_circuit/input.js
collapse_vars/collapse_vars_short_circuited_conditions/input.js
collapse_vars/collapse_vars_side_effects_1/input.js collapse_vars/collapse_vars_side_effects_1/input.js
collapse_vars/collapse_vars_side_effects_2/input.js
collapse_vars/collapse_vars_switch/input.js collapse_vars/collapse_vars_switch/input.js
collapse_vars/collapse_vars_unary/input.js collapse_vars/collapse_vars_unary/input.js
collapse_vars/cond_branch_1/input.js collapse_vars/cond_branch_1/input.js
@ -91,6 +95,7 @@ collapse_vars/var_defs/input.js
collapse_vars/var_side_effects_2/input.js collapse_vars/var_side_effects_2/input.js
conditionals/equality_conditionals_false/input.js conditionals/equality_conditionals_false/input.js
conditionals/hoist_decl/input.js conditionals/hoist_decl/input.js
conditionals/ifs_5/input.js
conditionals/ifs_6/input.js conditionals/ifs_6/input.js
conditionals/ifs_same_consequent/input.js conditionals/ifs_same_consequent/input.js
conditionals/issue_1154/input.js conditionals/issue_1154/input.js
@ -210,7 +215,6 @@ export/name_cache_import_star_as_name_from_module/input.js
export/name_cache_mangle_export_default_class/input.js export/name_cache_mangle_export_default_class/input.js
export/name_cache_mangle_export_default_function/input.js export/name_cache_mangle_export_default_function/input.js
export/name_cache_mangle_local_import_and_export_aliases/input.js export/name_cache_mangle_local_import_and_export_aliases/input.js
functions/avoid_generating_duplicate_functions_compared_together_3/input.js
functions/duplicate_arg_var/input.js functions/duplicate_arg_var/input.js
functions/empty_body/input.js functions/empty_body/input.js
functions/hoist_funs/input.js functions/hoist_funs/input.js
@ -280,6 +284,7 @@ harmony/array_spread_of_sequence/input.js
harmony/class_name_can_be_mangled/input.js harmony/class_name_can_be_mangled/input.js
harmony/class_name_can_be_preserved_with_reserved/input.js harmony/class_name_can_be_preserved_with_reserved/input.js
harmony/classes_extending_classes_out_of_pure_iifes/input.js harmony/classes_extending_classes_out_of_pure_iifes/input.js
harmony/default_assign/input.js
harmony/expansion/input.js harmony/expansion/input.js
harmony/fat_arrow_as_param/input.js harmony/fat_arrow_as_param/input.js
harmony/import_statement_mangling/input.js harmony/import_statement_mangling/input.js
@ -335,14 +340,18 @@ ie8/issue_2254_1/input.js
ie8/issue_2254_2/input.js ie8/issue_2254_2/input.js
ie8/reduce_vars/input.js ie8/reduce_vars/input.js
if_return/if_return_same_value/input.js if_return/if_return_same_value/input.js
if_return/if_var_return/input.js
if_return/issue_2747/input.js if_return/issue_2747/input.js
if_return/issue_512/input.js if_return/issue_512/input.js
inline/do_not_repeat_when_variable_larger_than_inlined_node/input.js inline/do_not_repeat_when_variable_larger_than_inlined_node/input.js
inline/inline_annotation/input.js inline/inline_annotation/input.js
inline/inline_into_scope_conflict/input.js
inline/inline_within_extends_1/input.js inline/inline_within_extends_1/input.js
inline/inline_within_extends_2/input.js inline/inline_within_extends_2/input.js
issue_1034/non_hoisted_function_after_return/input.js issue_1034/non_hoisted_function_after_return/input.js
issue_1034/non_hoisted_function_after_return_2a/input.js
issue_1034/non_hoisted_function_after_return_2a_strict/input.js issue_1034/non_hoisted_function_after_return_2a_strict/input.js
issue_1034/non_hoisted_function_after_return_2b/input.js
issue_1034/non_hoisted_function_after_return_2b_strict/input.js issue_1034/non_hoisted_function_after_return_2b_strict/input.js
issue_1034/non_hoisted_function_after_return_strict/input.js issue_1034/non_hoisted_function_after_return_strict/input.js
issue_1052/deeply_nested/input.js issue_1052/deeply_nested/input.js

View File

@ -34,7 +34,6 @@ use testing::assert_eq;
#[testing::fixture( #[testing::fixture(
"tests/terser/compress/**/input.js", "tests/terser/compress/**/input.js",
exclude( exclude(
"functions/avoid_generating_duplicate_functions_compared_together_3/",
// We don't care about ie8 // We don't care about ie8
"ie8", "ie8",
// tests with infinite loops // tests with infinite loops