A weird operation to optimize

This is another version, in an attempt to make the code neater:

function weird_cumsum3!(out,in,I,J)
    Jrng = 1:J[end]
    @views out[Jrng] .= in[I[Jrng]]
    @views cumsum!(out[Jrng], out[Jrng])
    @views reverse(out) .= out[reverse(J)]
    return nothing
end

It suffers from some tiny allocations. Not sure where they stem from. But is still quite fast. Perhaps for large lengths, cumsum! might have optimizations.