HaxeFoundation / haxe

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

Redundant call _hx_funcToField for lua target #11842

Open ze0nni opened 20 hours ago

ze0nni commented 20 hours ago

I profiled my code and found this strange behavior.

package;

typedef Slot = {
        var data: Int;
}

class Main {
        static function main() {}

        static function foo(a: Slot, b: Slot) {
                a.data = b.data;
        }

        static function bar(a: Slot, b: Slot) {
                final data = b.data;
                a.data = data;
        }
}

haxe -lua main.lua -main Main.hx

Main.foo = function(a,b) 
  a.data = _hx_funcToField(b.data); -- <<<<
end
Main.bar = function(a,b) 
  local data = b.data;
  a.data = data;
end

Haxe 4.3.3

Simn commented 7 hours ago

I know nothing about lua but there's indeed some code that generates this for any anon.field = anon.field case. From what I can tell, the _hx_funcToField function is there to deal with situations where we're assigning to a function type at runtime. However, when everything is properly typed like in the case here, I don't see a reason why it would do that.

@jdonaldson Do you happen to remember why it was implemented like that?