Repeated compilation for same function with extreme type instability

Thankfully, I have created a minimal reproducible example:

f1() = rand() > 0.5 ? 1 : rand()
f2() = rand() > 0.5 ? 1 : rand()
f3() = rand() > 0.5 ? 1 : rand()
f4() = rand() > 0.5 ? 1 : rand()
f5() = rand() > 0.5 ? 1 : rand()
f6() = rand() > 0.5 ? 1 : rand()

ff() = f1() + f2() + f3() + f4() + f5() + f6()

@time ff()

0.007261 seconds (7.20 k allocations: 485.763 KiB, 98.70% compilation time)

@time (f1(), f2(), f3(), f4(), f5(), f6())

0.000019 seconds (6 allocations: 240 bytes)

Is there a way in Julia to mark ff() to be executed by interpretation instead of time consuming re-compilation?

2 Likes