Performance of streaming `mean`

Here’s a basic streaming mean function (called avg for convenience):

function avg(itr)
    a = zero(eltype(x))
    for (i, x) in enumerate(itr)
        a += (x - a) / i
    end
    return a
end

Why does it allocate so much on a skipmissing iterator? Is there some issue with the skipmissing iterator implementation?

julia> x = rand(Float32, 100_000_000); sx = skipmissing(x);

julia> @btime avg($sx);
  10.356 s (499999489 allocations: 7.45 GiB)

Huh, this also has a ton of allocations:

julia> @btime avg(1:1_000_000);
  69.484 ms (4998978 allocations: 76.28 MiB)

Am I missing something obvious? :joy:

Oops, dumb mistake… This is what happens when I work in the REPL and try to edit functions.

It should be eltype(itr)? I’m not sure why it works at all :rofl:

Yeah, I should have been working in VS Code. I make stupid editing mistakes when I edit functions in the REPL.