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

Improve isassigned implementation #49827

Merged
merged 5 commits into from
May 20, 2023
Merged

Conversation

Tokazama
Copy link
Contributor

Unless isassigned is called on Array with Ints, it uses a try catch, which makes isassigned pretty slow (#44720). This PR provides changes the default implementation of isassigned to coerce the indices provided to Ints and converts to linear or cartesian indices, depending on the arrays IndexStyle. This also overloads isassigned for many of the array types defined in Base.

Unless `isassigned` is called on `Array` with `Int`s, it uses a try
catch, which is notoriously slow. This PR provides changes the default
implementation of `isassigned` to coerce the indices provided to `Int`s
and converts to linear or cartesian indices, depending on the arrays
`IndexStyle`. This also overloads `isassigned` for many of the array
types defined in Base.
Copy link
Sponsor Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

@Tokazama Tokazama marked this pull request as ready for review May 17, 2023 14:50
@Tokazama
Copy link
Contributor Author

Tokazama commented May 17, 2023

I'm not sure if the errors I'm seeing are related to this PR (passes locally on my computer), but I'd be happy to make changes as needed

Looks like updating the branch fixed the test

@@ -901,6 +901,8 @@ end

## indexing

isassigned(r::AbstractRange, i::Int) = firstindex(r) <= i <= lastindex(r)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true for StepRange? I think it might only be true for Unit ranges

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of an instance where it could produce an undefined variable

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, right, I was thinking of values, not index

@vtjnash vtjnash merged commit 8e03be1 into JuliaLang:master May 20, 2023
@@ -604,6 +617,19 @@ function diag(M::Tridiagonal{T}, n::Integer=0) where T
end
end

@inline function Base.isassigned(A::Tridiagonal, i::Int, j::Int)
@boundscheck checkbounds(A, i, j)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this ok? in all other isassigned implementations, this returns false if the bound check fails, but this throws an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. That should be fixed

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

Successfully merging this pull request may close these issues.

None yet

3 participants