google / google/closure-compiler
Redundant temporary copy of variable appears
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
```
Closure Compiler (http://github.com/google/closure-compiler)
Version: v20190106
Built on: 2019-01-10 22:30
```
```
var Module;
if (!Module) Module = "__EMSCRIPTEN_PRIVATE_MODULE_EXPORT_NAME_SUBSTITUTION__";
var UTF8Decoder = new TextDecoder('utf8');
function UTF8ArrayToString(u8Array, idx) {
var endPtr = idx;
while (u8Array[endPtr]) ++endPtr;
return UTF8Decoder.decode(u8Array.subarray(idx, endPtr));
}
function UTF8ToString(ptr) {
return UTF8ArrayToString(HEAPU8,ptr);
}
var buffer = new ArrayBuffer(16777216);
var HEAPU8 = new Uint8Array(buffer);
function foo(str) {
console.log(UTF8ToString(str));
}
externalFunction({ "foo": foo }, buffer);
```
with an externs file
```
var externalFunction;
```
produces the following with ADVANCED_OPTIMIZATIONS:
```js
var c;
c || (c = "__EMSCRIPTEN_PRIVATE_MODULE_EXPORT_NAME_SUBSTITUTION__");
var e = new TextDecoder("utf8");
function f(a) {
for (var d = g, b = a; d[b];) ++b;
return e.decode(d.subarray(a, b))
}
var h = new ArrayBuffer(16777216),
g = new Uint8Array(h);
externalFunction({
foo: function(a) {
console.log(f(a))
}
}, h);
```
Looking closer, the way function `f(a)` got generated is suboptimal, there is an odd variable copy `var d = g` that is generated, which looks redundant.
```
function f(a) {
for (var b = a; g[b];) ++b;
return e.decode(g.subarray(a, b))
}
```
What makes this extra surprising is that there is no temporary copy of `u8Array` that developer would have written, but closure somehow made one appear, perhaps as part of inlining `UTF8ToString()` function away?
Contributor guide
Assessment
This issue has not been assessed yet.