-
-
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
speed up logical indexing by bitarray #29746
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -523,19 +523,23 @@ end | |||
L.mask[idx] && return (idx, s) | ||||
end | ||||
end | ||||
# When wrapping a BitArray, lean heavily upon its internals -- this is a common | ||||
# case. Just use the Int index and count as its state. | ||||
@inline function iterate(L::LogicalIndex{Int,<:BitArray}, s=(0,1)) | ||||
s[2] > length(L) && return nothing | ||||
i, n = s | ||||
# When wrapping a BitArray, lean heavily upon its internals. | ||||
@inline function iterate(L::Base.LogicalIndex{Int,<:BitArray}) | ||||
L.sum == 0 && return nothing | ||||
Bc = L.mask.chunks | ||||
while true | ||||
if Bc[_div64(i)+1] & (UInt64(1)<<_mod64(i)) != 0 | ||||
i += 1 | ||||
return (i, (i, n+1)) | ||||
end | ||||
i += 1 | ||||
return iterate(L::Base.LogicalIndex{Int,<:BitArray}, (1, @inbounds Bc[1])) | ||||
chethega marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
end | ||||
@propagate_inbounds function iterate(L::Base.LogicalIndex{Int,<:BitArray}, s) | ||||
Bc = L.mask.chunks | ||||
i1, c = s | ||||
while c==0 | ||||
i1 == length(Bc) && return nothing | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not entirely sure about the desired safety here. If the iteration interface is used correctly, this will always be be inbounds. If we get passed an OOB-state, do we need to raise a correct error, are we allowed to return garbage (as long as no OOB read occurs) or are we allowed to perform an OOB read (possibly with segfault) and then return garbage? The TEST instead of CMP was actually cribbed from the iteration code for vectors. Benchmarks and Agner Fog agree that there is no price difference between these instructions. I could change to For some reason the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
totally fine to return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I got a fast UInt version now. Only question is about what is our preferred error behavior:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "When in doubt, do as the So, couldn't just something like Line 707 in 0e023d0
be used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arrays currently give nothing for OOB states, and ranges silently return garbage:
In view of that, I guess it would be consistent enough to return |
||||
i1 += 1 | ||||
c = Bc[i1] | ||||
end | ||||
tz = trailing_zeros(c) + 1 | ||||
c = _BLSR(c) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a really clever idea. Nice work. |
||||
return ((i1-1)<<6 + tz, (i1, c)) | ||||
end | ||||
|
||||
@inline checkbounds(::Type{Bool}, A::AbstractArray, I::LogicalIndex{<:Any,<:AbstractArray{Bool,1}}) = | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pickiest possible nit: maybe
_blsr
to match the surrounding capitalization?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amended.