-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compile-time optimization for any() and all() #21256
Comments
I think the trouble is either that iteration of arrays can't be proven to be effect free, or that this information isn't being passed to LLVM to allow optimization. Right now even an empty loop can't be optimized away.
|
Good catch! Actually, this is just because of the absence of the |
Fixes JuliaData#1358 until JuliaLang/julia#21256 is fixed. The issue with `enumerate` somehow allocating during the loop seems fixed on Julia master (0.7/1.0). Since this is a performance change, I don't quite know if and how to add a test. Suggestions?
There's a regression on master which makes the proposed At any rate, for # using the branch of #27386
julia> f(v) = any(x -> false, v)
julia> g(v) = all(x -> true, v)
julia> @btime f(v) setup = (v = rand(Int, 1_000))
1.887 ns (0 allocations: 0 bytes)
false
julia> @btime g(v) setup = (v = rand(Int, 1_000))
3.370 ns (0 allocations: 0 bytes) # this is "slower" due to function call overhead; the call to `_all` is not inlined
true |
I confirm that #27386 fixes it: julia> f(v) = any(x -> false, v)
f (generic function with 1 method)
julia> @code_llvm f([1])
; Function f
; Location: REPL[7]:1
define i8 @julia_f_33311(%jl_value_t addrspace(10)* nonnull dereferenceable(40)) {
top:
ret i8 0
}
julia> g(v) = all(x -> true, v)
g (generic function with 1 method)
julia> @code_llvm g([1])
; Function g
; Location: REPL[9]:1
define i8 @julia_g_33323(%jl_value_t addrspace(10)* nonnull dereferenceable(40)) {
top:
ret i8 1
} |
Currently
all(x -> true, X)
andany(x -> false, X)
loop over all entries ofX
. Could the compiler optimize the loop away when the return value is known?This would be useful in cases like
any(isnull, X)
, sinceisnull
returnsfalse
for non-Nullable
entries. That would make this call very efficient whenX
is a non-nullable array. Cf. JuliaStats/NullableArrays.jl#189.The text was updated successfully, but these errors were encountered: