google / google/closure-compiler

Optimization regression - excess function parameters are no longer optimized out

Open
#3,193 5 comments 0 reactions 0 assignees View on GitHub
internal-issue-created triage-done
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

`src.js`

```js
function foo(a, b) {
console.log(a);
}
setTimeout(foo, 0); // pin a reference to foo so it does not get optimized out
foo("hello", "this_is_redundant");
```

Old Closure:

```
C:\emsdk\emscripten\incoming>java -jar third_party\closure-compiler_old\compiler.jar --version
Closure Compiler (http://github.com/google/closure-compiler)
Version: v20171023
Built on: 2017-10-26 19:00
```

```
C:\emsdk\emscripten\incoming>java -jar third_party\closure-compiler_old\compiler.jar --js=src.js --js_output_file=out.js --compilation_level=ADVANCED_OPTIMIZATIONS

C:\emsdk\emscripten\incoming>type out.js
function a(b){console.log(b)}setTimeout(a,0);a("hello");
```

Closure optimized out the unnecessary `"this_is_redundant"` input string.

New Closure:

```
C:\emsdk\emscripten\incoming>java -jar third_party\closure-compiler\compiler.jar --version
Closure Compiler (http://github.com/google/closure-compiler)
Version: v20181210
Built on: 2018-12-12 22:32
```

```
C:\emsdk\emscripten\incoming>java -jar third_party\closure-compiler\compiler.jar --js=src.js --js_output_file=out.js --compilation_level=ADVANCED_OPTIMIZATIONS

C:\code\emsdk\emscripten\incoming>type out.js
function a(b){console.log(b)}setTimeout(a,0);a("hello","this_is_redundant");
```

the optimization unfortunately no longer occurs, but `"this_is_redundant"` appears in the output.

In new Closure, the optimization does not occur in this case either:

`src2.js`

```js
function foo(a) {
console.log(a);
}
setTimeout(foo, 0); // pin a reference to foo so it does not get optimized out
foo("hello", "this_is_redundant");
```

although a warning is issued. In old Closure, this version was also optimized.

Curiously, new Closure does still optimize this variant of the code, where input parameters are reversed:

`src3.js`

```js
function foo(a, b) {
console.log(b);
}
setTimeout(foo, 0); // pin a reference to foo so it does not get optimized out
foo("this_is_redundant", "hello");
```

becomes

```js
setTimeout(function(b,a){console.log(a)},0);console.log("hello");
```

so if a redundant parameter appears first, it does get optimized. But if it appears last, it no longer does.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.