closure-compiler icon indicating copy to clipboard operation
closure-compiler copied to clipboard

Array constructor with length is not eliminated

Open KimlikDAO-bot opened this issue 2 years ago • 8 comments
trafficstars

Consider the code:

/** @const {!Array<number>} */
const a = [1];

/** @const {!Array<number>} */
const b = Array(10);
b[0] = 2;

/** @const {!Array<number>} */
const c = [];
c.push(3);

/** @const {!Array<number>} */
const d = Array(10);
d.push(4);

When compiled with

yarn google-closure-compiler -O ADVANCED --js a.js --rewrite_polyfills=false 

we get

Array(10)[0]=2;Array(10).push(4);

Arrays created with the Array(len) constructor are not eliminated.

google-closure-compiler version: v20230103

KimlikDAO-bot avatar Jan 22 '23 21:01 KimlikDAO-bot

The peephole optimizations remove the cases of an array created with an array literal.

Presumably we could update whichever one does that so it also removes arrays created with the Array() constructor.

However, I don't think we're likely to prioritize fixing this. It's just not likely to be worth the effort.

I'll mark this as "help wanted", since I think it would be a good small contribution that could be made from outside our team.

brad4d avatar Jan 23 '23 21:01 brad4d

Thank you! We'll look into fixing this

KimlikDAO-bot avatar Jan 23 '23 23:01 KimlikDAO-bot

I want to work in this issue

jobedrony avatar Aug 06 '23 11:08 jobedrony

Is it possible I work on this issue?

PTheocharis avatar Mar 15 '24 21:03 PTheocharis

@brad4d Could you point to some relevant files in the code?

KimlikDAO-bot avatar May 29 '24 18:05 KimlikDAO-bot

I would start by adding a test case here:

https://github.com/google/closure-compiler/blob/d77e3adfeee582d56c27420039f3ccb6c3e860ad/test/com/google/javascript/jscomp/PeepholeRemoveDeadCodeTest.java#L1476

fold("new Array(10);", "");

Then investigate how to make PeepholeRemoveDeadCode pass this test.

brad4d avatar Jun 04 '24 15:06 brad4d