-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Closed
Description
julia> function foo(xs)
count = 0
@inbounds for i in 1:length(xs)
if isnan(xs[i])
count += 1
end
end
return count
end
foo (generic function with 1 method)
julia> vals = rand(10_000);
julia> @time foo(vals)
0.009762 seconds (17.66 k allocations: 960.671 KiB)
0
julia> @time foo(vals)
0.000019 seconds
0
julia> vals[:] .= NaN;
julia> @time foo(vals)
0.000016 seconds (1 allocation: 16 bytes)
10000
julia> vals[:] .= 0;
julia> @time foo(vals)
0.000018 seconds
0
julia> vals = rand(10);
julia> @time foo(vals)
0.000003 seconds
0
julia> vals[:] .= NaN;
julia> @time foo(vals)
0.000003 seconds
10
julia> versioninfo()
Julia Version 1.4.2
Commit 44fa15b150* (2020-05-23 18:35 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Xeon(R) Gold 6254 CPU @ 3.10GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-8.0.1 (ORCJIT, skylake)
Environment:
JULIA_NUM_THREADS = 12
I came across this odd behaviour today when trying to work out where this 16 byte allocation was coming from. Whether or not the function allocates seems to be something to do with the length, which is perhaps reasonable (in the generated LLVM there is a scalar/vector switch based on the length of the vector). It also seems to be affected by the contents of the vector - the latter is more surprising to me.
The behaviour doesn't change when the @inbounds is removed, despite that preventing vector instruction generation.
I also see this behaviour on Julia Version 1.5.0-rc1.0.
It would be great to hear a sensible explanation!
Metadata
Metadata
Assignees
Labels
No labels