Skip to content

ismissing(x) much slower than x === missing #27681

@nalimilan

Description

@nalimilan

Since #27651 a loop involving x === missing can be very fast, but using the more generic ismissing(x) leads to much less efficient code being generated. Apparently, the compiler isn't able to detect that !ismissing(x) implies that x::Int.

function mysum(X)
   s = zero(eltype(X))
   @inbounds @simd for x in X
       if x !== missing
           s += x
       end
   end
   s
end

function mysum2(X)
   s = zero(eltype(X))
   @inbounds @simd for x in X
       if !ismissing(x)
           s += x
       end
   end
   s
end


X1 = rand(Int, 10_000_000);
X2 = Vector{Union{Missing,Int}}(X1);
X3 = ifelse.(rand(length(X2)) .< 0.9, X2, missing);

julia> @btime mysum(X1);
  5.631 ms (1 allocation: 16 bytes)

julia> @btime mysum(X2);

  5.785 ms (1 allocation: 16 bytes)

julia> @btime mysum(X3);
  5.670 ms (1 allocation: 16 bytes)

julia> @btime mysum2(X1);
  5.676 ms (1 allocation: 16 bytes)

julia> @btime mysum2(X2);
  16.377 ms (1 allocation: 16 bytes)

julia> @btime mysum2(X3);
  20.681 ms (1 allocation: 16 bytes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    missing dataBase.missing and related functionalityperformanceMust go faster

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions