Hacker News new | ask | show | jobs
by returningfory2 113 days ago
Yep realized this after that my second solution (push a 0 if n is incremented) has the same branch prediction problem.

I think yours works. Alternatively in the loop:

    if out.len() < n {
        out.push(0);
    }
    out[n] = x;
    n += (x > threshold) as usize;
In this case the if will be predicted well because it only triggers log(N) times, given how the std lib extends vectors.