HaxeFoundation / haxe

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

Closures are not inlined when used through another inline closure #11626

Open Speedphoenix opened 3 months ago

Speedphoenix commented 3 months ago

In this situation

class Extensions {
    inline static public function findMin<T>(a:Array<T>, f:T->Float) {
        var minVal = Math.POSITIVE_INFINITY;
        var minItem = null;
        for (item in a) {
            var v = f(item);
            if (v < minVal) {
                minVal = v;
                minItem = item;
            }
        }
        return {item: minItem, val: minVal};
    }

    inline static public function findMaxValue<T>(a:Array<T>, f:T->Float):Float {
        return -findMin(a, i -> -f(i)).val;
    }
}

class Test {
    static function main() {
        var arr = [5, 4, 65, 8];
        trace(Extensions.findMaxValue(arr, e -> e * 2));
    }
}

*The `e -> e 2` closure is not inlined**, even though it's only used in inline functions

On Try Haxe, the JS Source still shows

let arr = [5,4,65,8];
let f = function(e) {
    return e * 2;
};
let minVal = Infinity;
let _g = 0;
while(_g < arr.length) {
    let v = -f(arr[_g++]);
    if(v < minVal) {
        minVal = v;
    }
}
console.log("Test.hx:23:",-minVal);