Repeated compilation for same function with extreme type instability

Here is a minimal reproducible example: Repeated compilation for same function with extreme type instability - #5 by guoyongzhi


My function consistently experiences prolonged compilation times with each execution. However, upon @snoopr inspection, I consistently receive empty invalidations. What steps should I take to address this issue?

using WordCloud
using SnoopCompileCore
using SnoopCompile
wordcloud("aa bb cc dd")
invalidations = @snoopr wordcloud("aa bb cc dd")
@show length(uinvalidated(invalidations))
@time wordcloud("aa bb cc dd")

length(uinvalidated(invalidations)) = 0
0.669331 seconds (438.85 k allocations: 30.645 MiB, 75.80% compilation time: 4% of which was recompilation)

I think this issue might be related to unstable types, as Iā€™m certain the problem lies within function WordCloud.getstylescheme, where there are some types determined by random numbers.

using WordCloud
WordCloud.getstylescheme(["a"], [1]);
@time WordCloud.getstylescheme(["a"], [1]);

0.181821 seconds (204.89 k allocations: 13.782 MiB, 98.69% compilation time)


Are there any inspection tools in Julia for this kind of issues?
Is there a way in Julia to mark a dynamic function to be executed by interpretation instead of time consuming re-compilation?

Maybe you can try @nospecialize:

https://docs.julialang.org/en/v1/base/base/#Base.@nospecialize

The random number is generated within the function body, and since the function can accept zero arguments, the @nospecialize decorator can not help me. Thank you regardless.

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?

1 Like