Unclear allocation behaviour with built-in sum()

In 1.3, you re-use the name A many times. Writing sum(A[i] for i in ...) creates a closure for the generator. And I’m quite sure that Julia isn’t able to prove that A doesn’t get renamed in the midst of computing that sum of the generator. And so it dramatically pessimizes each and every access with a box to allow it to handle a wild rename.

In short, without looking any deeper, this is almost certainly Julia#15276, one of the last remaining infamous issue numbers I have memorized. The standard workarounds are to either:

  • use a distinct name for each of those A = assignments
  • introduce a let A=A block around each @threads loop to simplify the scope that Julia has to examine.
5 Likes