HaxeFoundation / HaxeFoundation/haxe
modulo operator on constants could be optimized away like arithmetics
- Dominant language
- Haxe
- Stars
- 6.9k
- Forks
- 715
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 11
Description
```haxe
function main() {
for (i in 0...10) {
if (i % 2 == 0) {
trace(i);
}
}
}
```
Generates
```js
function Main_main() {
if(0 % 2 == 0) {
console.log("src/Main.hx:4:",0);
}
if(1 % 2 == 0) {
console.log("src/Main.hx:4:",1);
}
if(2 % 2 == 0) {
console.log("src/Main.hx:4:",2);
}
if(3 % 2 == 0) {
console.log("src/Main.hx:4:",3);
}
if(4 % 2 == 0) {
console.log("src/Main.hx:4:",4);
}
if(5 % 2 == 0) {
console.log("src/Main.hx:4:",5);
}
if(6 % 2 == 0) {
console.log("src/Main.hx:4:",6);
}
if(7 % 2 == 0) {
console.log("src/Main.hx:4:",7);
}
if(8 % 2 == 0) {
console.log("src/Main.hx:4:",8);
}
if(9 % 2 == 0) {
console.log("src/Main.hx:4:",9);
}
}
```
But I believe it should be safe to optimize it to
```haxe
function Main_main() {
console.log("src/Main.hx:4:",0);
console.log("src/Main.hx:4:",2);
console.log("src/Main.hx:4:",4);
console.log("src/Main.hx:4:",6);
console.log("src/Main.hx:4:",8);
}
```
Contributor guide
Research direction
The issue names no source file or test; start with the Haxe example and its generated JavaScript, then locate the compiler path responsible for constant arithmetic and loop optimization. Done means the modulo and comparison are eliminated for this case and the output contains only the even-number trace calls.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100