Skip to content
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

isassigned is slow for out-of-range indices #1152

Closed
gaurav-arya opened this issue Apr 6, 2023 · 4 comments
Closed

isassigned is slow for out-of-range indices #1152

gaurav-arya opened this issue Apr 6, 2023 · 4 comments

Comments

@gaurav-arya
Copy link

gaurav-arya commented Apr 6, 2023

MWE:

using StaticArrays, BenchmarkTools

arr = SA[1, 2, 3]

@btime isassigned(arr, 4) # 17 μs

This is because Base's fallback implementation uses a try/catch, which is slow in the catch case:
https://github.com/JuliaLang/julia/blob/def2ddacc9a9b064d06c24d12885427fb0502465/base/abstractarray.jl#L605-L616

This really hurts performance when isassigned is being used in a case where the index is really expected to be out-of-range sometimes. I even wonder if Base's fallback implementation is just a bad idea, because it will always be slow in this case: something like i in eachindex(arr) would at least be faster for static arrays.

Using Julia 1.9 and StaticArrays v.1.5.12

@thchr
Copy link
Collaborator

thchr commented Apr 11, 2023

There used to be a (faulty) isassigned specialization for StaticArrays but it was removed recently (#1096), partly due to incorrectness, invalidations, and because it was not clear why performance would matter here.

What do you need a performant isassigned for?

@gaurav-arya
Copy link
Author

gaurav-arya commented Apr 11, 2023

The logic was something like "given an array and an index, see if index + 1 is a valid index and access array there if so". I was using it in my package here:

https://github.com/gaurav-arya/StochasticAD.jl/blob/892486bec7bb0757a2fef6e6e8afd12374d4ebe4/src/general_rules.jl#L259

Although I've since replaced it with i in eachindex(arr) because of this issue

@thchr
Copy link
Collaborator

thchr commented Apr 11, 2023

Yeah, that seems better: isassigned is intended for more than just bounds checking (i.e., it also returns false if the index is #undef).

See also checkbounds and the Base docs.

Closing cf. earlier discussion in #1096.

@thchr thchr closed this as completed Apr 11, 2023
@gaurav-arya
Copy link
Author

Thanks, that makes sense, checkbounds seems like the right thing to use indeed.

(Although note that Base's Array does have its own specialization: https://github.com/JuliaLang/julia/blob/1aa65c33f24e7a6c7a341ce4a13202bfb6ac4811/base/array.jl#L258)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants