Call FastClosures.jl `@closure` in Array Comprehensions?

No, this is intentional. The intent was to contrive a scenario where neophytes to Julia (and even seasoned veterans) could inadvertently cause boxing and poor performance where they didn’t expect to.

This is a better example, using more common patterns:

julia> function test1()
           x = if true;  collect(1:1000)  else  collect(1:1000)  end
           [x[i] for i in x]
       end;

julia> function test2()
           if true;  x=collect(1:1000)  else  x=collect(1:1000)  end
           [x[i] for i in x]
       end;

julia> @btime test1();
  725.197 ns (2 allocations: 15.88 KiB)

julia> @btime test2();
  23.600 μs (985 allocations: 31.27 KiB)

Even for people who’ve read #15276, it might not be anticipated that a box would be formed here, because the fact that a comprehension creates a lambda is not obvious or necessary.

2 Likes