Hey, does anybody know why a generator does allocate memory while an iterator does

Hey, does anybody know why a generator does allocate memory while an iterator doesn’t when assigned to an existing location?

julia> x = randn((1000));

julia> y = 1:1000
1:1000

julia> g = (i for i=1:1000)
Base.Generator{UnitRange{Int64}, typeof(identity)}(identity, 1:1000)

julia> @time x .= g;
  0.000020 seconds (3 allocations: 7.969 KiB)

julia> @time x .= g;
  0.000030 seconds (3 allocations: 7.969 KiB)

julia> @time x .= y;
  0.000048 seconds (3 allocations: 96 bytes)

julia> @time x .= y;
  0.000027 seconds (3 allocations: 96 bytes)

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message :slack:) (More Info)

1 Like

Citing @mcabbott:

“Generators get collected when used with broadcasting”.

This issue goes into a similar direction.

1 Like