COMMUNITY

Partial inline recursive function (Fibonacci optimization)


(Adrian V) #1

now that we have inline at the call site I wonder if it would be possible to partial inline recursive functions, which would be nice to bring Haxe in front of any language in the unavoidable Fibonacci benchmark.

    @:pure
    static function fib(n: Int): Int {
        return n > 1 ? inline fib(n - 1) + fib(n - 2) : n;
    }

could be converted and optimized to:

    @:pure
    static function fib(n: Int): Int {
        //return n > 1 ? (n > 2 ? fib(n - 2) + fib(n - 3): n - 1) + fib(n - 2) : n;
        return n > 2 ? 2 * fib(n - 2) + fib(n - 3) : n > 1 ? n - 1 : n;
    }

this would pulverize any other language :slight_smile: see here: http://try-haxe.mrcdk.com/#3fe8A