JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.43k stars 5.45k forks source link

Changes in behavior of `@allocated` with `.=` in `@testset` #27270

Open tkoolen opened 6 years ago

tkoolen commented 6 years ago

I found this while trying to get TypeSortedCollections up to speed with the new broadcast framework. Consider the following two test sets:

using Compat.Test

@testset "broadcast!" begin
    a = zeros(3)
    b = 1.0
    broadcast!(identity, a, b)
    @test (@allocated broadcast!(identity, a, b)) == 0
end

@testset ".=" begin
    a = zeros(3)
    b = 1.0
    a .= b
    @test (@allocated a .= b) == 0
end

On 0.6.2, both tests pass, but on

julia> versioninfo()
Julia Version 0.7.0-DEV.5221
Commit deaefef5e3 (2018-05-25 16:40 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin17.5.0)
  CPU: Intel(R) Core(TM) i7-3820QM CPU @ 2.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, ivybridge)
Environment:
  JULIA_PKGDIR = /Users/twan/code/julia/RigidBodyDynamics

the first test passes, but the second fails with 3408 bytes of allocation returned from @allocated.

Just wondering if this is expected, what the root cause might be, and whether this could lead to actual performance problems in production code.

martinholters commented 6 years ago

Note that

foo!(a, b) = a .= b
@testset ".=" begin
    a = zeros(3)
    b = 1.0
    foo!(a,b)
    @test (@allocated foo!(a, b)) == 0
end

passes, so I wouldn't expect it to cause unexpected allocations in production code, but I don't know what the root cause is.