Skip to content

performance gain when kernel operation is wrapped with a function #37558

@johnnychen94

Description

@johnnychen94

This originates from JuliaArrays/OffsetArrays.jl#136 (comment)

function overflow_check(r, offset::T) where T
    if offset > 0 && last(r) > typemax(T) - offset
        throw(ArgumentError("Boundary overflow detected: offset $offset should be equal or less than $(typemax(T) - last(r))"))
    end
end

function overflow_check_with_func(r, offset::T) where T
    throw_overflow_error() = throw(ArgumentError("Boundary overflow detected: offset $offset should be equal or less than $(typemax(T) - last(r))"))
    if offset > 0 && last(r) > typemax(T) - offset
        throw_overflow_error()
    end
end

These two works equivalently, but their performance is slightly different:

julia> r, offset = -3:3, -4;

julia> @btime overflow_check($r, $offset)
  4.801 ns (0 allocations: 0 bytes) # 1.6.0-DEV.883
  4.078 ns (0 allocations: 0 bytes) # 1.4.0, 1.5.1

julia> @btime overflow_check_with_func($r, $offset)
  3.765 ns (0 allocations: 0 bytes) # 1.6.0-DEV.883
  1.696 ns (0 allocations: 0 bytes) # 1.5.1
  1.919 ns (0 allocations: 0 bytes) # 1.4.0

I'm really unsure what an appropriate title should be, please rename it if that's unclear.

cc: @jishnub @stevengj

Metadata

Metadata

Assignees

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