HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.03k stars 648 forks source link

[analyzer] immediately-invoked-by-analyzer anonymous functions are not optimized away #9228

Open nadako opened 4 years ago

nadako commented 4 years ago
class Main {
    static function main() {
        var f = a -> a;
        trace(f(3));

        trace((a -> a)(3));
    }
}
console.log("src/Main.hx:4:",(function(a) {
    return a;
})(3));
console.log("src/Main.hx:6:",3);

This is because IIFE optimization is done before the analyzer optimization, but maybe it should be done both before and after it.

Simn commented 4 years ago

This is difficult because inlining can put bad things in value places. I doubt the current analyzer architecture could support this.

nadako commented 4 years ago

Not sure what you mean by bad things here :)

Simn commented 4 years ago

All of the stuff that used to cause $this on JS and at this point probably causes crashes and fireworks on various other targets.

nadako commented 4 years ago

I guess I need an example. I quickly tried adding Optimizer.reduce_expression tctx call after the analyzer run and it seems to fix the code in the original post, but I guess you're talking about something more complicated?

Simn commented 4 years ago

Check the JS unit test output and look for $this.

nadako commented 4 years ago

Oh, I see, it indeed added a couple of them (while optimizing a lot of other stuff though).