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

add count(itr) and throw and error in count if non-boolean values are encountered #20421

Merged
merged 2 commits into from
Feb 3, 2017

Conversation

stevengj
Copy link
Member

@stevengj stevengj commented Feb 3, 2017

Fixes #20403 and #20404. Since changing countnz was controversial, I left that function alone.

function count(pred, a::AbstractArray)
n = 0
for i in eachindex(a)
@inbounds n += pred(a[i])::Bool
Copy link
Member

Choose a reason for hiding this comment

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

Is this safe for arbitrary preds and AbstractArrays? Could, for example, pred resize a::Vector as a side effect, causing issues downstream in the iteration? Best!

Copy link
Member Author

@stevengj stevengj Feb 3, 2017

Choose a reason for hiding this comment

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

We seem to use @inbounds for all of the other mapreduce functions, so they are all assuming that the mapped function does not resize the array. Maybe we should reconsider that elsewhere, but I don't think that count should be the exception here.

Copy link
Member

Choose a reason for hiding this comment

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

Hm, interesting. On the one hand, from base/reduce.jl _mapreduce, mapreduce_impl, mapfoldl_impl, and sum_kbn use @inbounds. On the other hand, mapfoldr_impl, any, all, contains, extrema, and the existing implementation of count do not.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds like a reasonable assumption to me. Better turn on @inbounds everywhere possible.

Copy link
Member

Choose a reason for hiding this comment

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

Ref. related discussion #19925 (review). Perhaps consistency of @inbounds decoration in reductions and similar functions deserves a dedicated issue? Best!

Copy link
Member

Choose a reason for hiding this comment

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

@JeffBezanson seemed to imply that @inbounds was only OK for Array, for which we know the index is valid. Indeed, for a custom AbstractArray, a buggy implementation could lead to crashes with @inbounds. We should definitely have a policy about this.

Copy link
Member

Choose a reason for hiding this comment

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

We have a policy: only use @inbounds when you can be certain, from local information, that all accesses are in bounds.

Copy link
Member

Choose a reason for hiding this comment

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

So that means the signature needs to be changed to Array? We cannot be certain that eachindex(a) is correct for any custom type.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe another PR should go through and fix occurrences of @inbounds in Base for AbstractArray?

It would be nice to have a way to turn this on for Array without requiring two methods. See also #15291

Copy link
Member

Choose a reason for hiding this comment

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

I've opened #20469 so that we don't forget about this.

Copy link
Member

@ararslan ararslan left a comment

Choose a reason for hiding this comment

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

💯

@stevengj
Copy link
Member Author

stevengj commented Feb 3, 2017

Should be good to merge.

@ararslan ararslan merged commit ec832e5 into JuliaLang:master Feb 3, 2017
@stevengj stevengj deleted the count branch February 4, 2017 00:09
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.

5 participants