When I run findlast(y -> y≤x, xrange)
, the number of allocations changes depending on the sameness of the type of x
and the element type of xrange
. Here is an example:
julia> VERSION
v"1.6.5-pre.0"
julia> using BenchmarkTools
julia> xi = 1; # Int64
julia> xf = 0.5; # Float64
julia> xrange = 1:100; # Int64 range
julia> @btime findlast(y -> y≤$xi, $xrange);
165.173 ns (0 allocations: 0 bytes) # no allocations for typeof(xi) == eltype(xrange)
julia> @btime findlast(y -> y≤$xf, $xrange);
270.025 ns (2 allocations: 48 bytes) # allocations for typeof(xi) ≠ eltype(xrange)
What is the explanation of this difference? I compared @code_llvm
and @code_native
of the two executions findlast(y -> y≤xi, xrange)
and findlast(y -> y≤xf, xrange)
, but I was not able to identify the lines causing allocations in the latter.