What is Julia's maxk(Matlab) that returns the indice of top K largest values?

Normally you would just do

static_topk(x, k) = _topk_static_dispatch(x, Val(k))

rather than PREALLOC_VALS[k].

But in another project I found you can get a tiny performance boost if you create all the Val(i) beforehand and just index it. I didn’t test whether it affects this one though; it’s just a habit (perhaps a bad one).

The ::Val{k} is just to make the function get recompiled each time k changes, so that the static arrays are type stable. (@generated only re-generates the functions when types change, so you couldn’t get around using the Val{k} here).

(I remember reading that in the most recent Julia versions, the compiler tends to be good enough so you don’t need @generated in most scenarios, so you can just let type specialization handle everything.)