Skip to content

View allocation elimination stumped by conditional? #28226

@KristofferC

Description

@KristofferC

Tracked down after performance report on Distances.jl (JuliaStats/Distances.jl#100)

Code to reproduce:

module Distances2

function colwise!(r, a, b)
    @inbounds for j = 1:size(a,2)
        r[j] = evaluate(view(a, :, j), view(b, :, j))
    end
    r
end

@inline function evaluate(a, b)
    length(a) == 0 && return 0.0 # comment out and 0.7 is super fast
    @inbounds begin
        s = 0.0
        @simd for I in eachindex(a, b)
            ai = a[I]
            bi = b[I]
            s += abs2(ai - bi)
        end
        return s
    end
end

end

using BenchmarkTools
z = zeros(41); A = rand(2, 41); B = rand(2, 41);
@btime Distances2.colwise!($z, $A, $B);

On 0.7:

 562.371 ns (82 allocations: 3.84 KiB)

On 0.6:

 255.466 ns (0 allocations: 0 bytes)

Now, if we comment away the length(a) == 0 && return 0.0 line:

0.7:

100.298 ns (0 allocations: 0 bytes)

0.6:

 206.193 ns (0 allocations: 0 bytes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    performanceMust go fasterregressionRegression in behavior compared to a previous version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions