HaxeFoundation / HaxeFoundation/haxe

[analyzer] local propagation vs closures

Open
#9,257 3 comments 0 reactions 0 assignees View on GitHub
enhancement feature-analyzer
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

I wonder if we could apply local fusion and const propagation and optimize `a` and `b` away in this case:

```haxe
class Main {
static function main() {
var v = Math.random();
var a = "hello";
var b = v;
function f() {
trace(a);
trace(b);
}
trace(a);
trace(b);
trace(v);
f();
f();
}
}
```

Currently this generates:
```js
Main.main = function() {
var v = Math.random();
var a = "hello";
var b = v;
var f = function() {
console.log("src/Main.hx:7:",a);
console.log("src/Main.hx:8:",b);
};
console.log("src/Main.hx:10:",a);
console.log("src/Main.hx:11:",b);
console.log("src/Main.hx:12:",v);
f();
f();
};
```

However it should be safe to optimize it to:
```js
Main.main = function() {
var v = Math.random();
var f = function() {
console.log("src/Main.hx:7:","hello");
console.log("src/Main.hx:8:",v);
};
console.log("src/Main.hx:10:","hello");
console.log("src/Main.hx:11:",v);
console.log("src/Main.hx:12:",v);
f();
f();
};
```

Because `a` and `b` are never modified... I remember our capture var handling filter deals with modification, so maybe we could use that info for optimization, but I might be wrong.

Contributor guide

Open the contributing guide

Research direction

Start with the analyzer's local fusion and const propagation logic, then inspect the capture-var handling referenced in the issue. Compile the provided Haxe example to JavaScript and compare the generated output; done means safely eliminating unchanged locals while preserving the closure's behavior and output.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.