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

Speed up scalar BitArray indexing by ~25% #11403

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,8 @@ end
convert{T,N}(::Type{Array{T}}, B::BitArray{N}) = convert(Array{T,N},B)
function convert{T,N}(::Type{Array{T,N}}, B::BitArray{N})
A = Array(T, size(B))
Bc = B.chunks
@inbounds for i = 1:length(A)
A[i] = unsafe_bitgetindex(Bc, i)
A[i] = unsafe_bitgetindex(B, i)
end
return A
end
Expand Down Expand Up @@ -341,36 +340,34 @@ bitpack{T,N}(A::AbstractArray{T,N}) = convert(BitArray{N}, A)

## Indexing: getindex ##

@inline function unsafe_bitgetindex(Bc::Vector{UInt64}, i::Int)
@inline function unsafe_bitgetindex(B::BitArray, i::Int)
i1, i2 = get_chunks_id(i)
u = UInt64(1) << i2
@inbounds r = (Bc[i1] & u) != 0
@inbounds r = (B.chunks[i1] & u) != 0
return r
end

@inline function getindex(B::BitArray, i::Int)
1 <= i <= length(B) || throw(BoundsError(B, i))
return unsafe_bitgetindex(B.chunks, i)
return unsafe_bitgetindex(B, i)
end

getindex(B::BitArray, i::Real) = getindex(B, to_index(i))

getindex(B::BitArray) = getindex(B, 1)

# 0d bitarray
getindex(B::BitArray{0}) = unsafe_bitgetindex(B.chunks, 1)
getindex(B::BitArray{0}) = unsafe_bitgetindex(B, 1)

function getindex{T<:Real}(B::BitArray, I::AbstractVector{T})
X = BitArray(length(I))
lB = length(B)
Xc = X.chunks
Bc = B.chunks
ind = 1
for i in I
# faster X[ind] = B[i]
j = to_index(i)
1 <= j <= lB || throw(BoundsError(B, j))
unsafe_bitsetindex!(Xc, unsafe_bitgetindex(Bc, j), ind)
unsafe_bitsetindex!(X, unsafe_bitgetindex(B, j), ind)
ind += 1
end
return X
Expand All @@ -388,13 +385,11 @@ for IT in [AbstractVector{Bool}, AbstractArray{Bool}]
checkbounds(B, I)
n = sum(I)
X = BitArray(n)
Xc = X.chunks
Bc = B.chunks
ind = 1
for i = 1:length(I)
if $idxop(I, i)
# faster X[ind] = B[i]
unsafe_bitsetindex!(Xc, unsafe_bitgetindex(Bc, i), ind)
unsafe_bitsetindex!(X, unsafe_bitgetindex(B, i), ind)
ind += 1
end
end
Expand All @@ -405,14 +400,14 @@ end

## Indexing: setindex! ##

@inline function unsafe_bitsetindex!(Bc::Array{UInt64}, x::Bool, i::Int)
@inline function unsafe_bitsetindex!(B::BitArray, x::Bool, i::Int)
i1, i2 = get_chunks_id(i)
u = UInt64(1) << i2
@inbounds begin
if x
Bc[i1] |= u
B.chunks[i1] |= u
else
Bc[i1] &= ~u
B.chunks[i1] &= ~u
end
end
end
Expand All @@ -421,7 +416,7 @@ setindex!(B::BitArray, x) = setindex!(B, convert(Bool,x), 1)

function setindex!(B::BitArray, x::Bool, i::Int)
1 <= i <= length(B) || throw(BoundsError(B, i))
unsafe_bitsetindex!(B.chunks, x, i)
unsafe_bitsetindex!(B, x, i)
return B
end

Expand Down Expand Up @@ -453,10 +448,9 @@ end
quote
checkbounds(B, I)
y = convert(Bool, x)
Bc = B.chunks
for i = 1:length(I)
# faster I[i] && B[i] = y
$idxop(I, i) && unsafe_bitsetindex!(Bc, y, i)
$idxop(I, i) && unsafe_bitsetindex!(B, y, i)
end
return B
end
Expand Down Expand Up @@ -499,12 +493,11 @@ end
idxop = I <: Array{Bool} ? :unsafe_getindex : :getindex
quote
checkbounds(B, I)
Bc = B.chunks
c = 1
for i = 1:length(I)
if $idxop(I, i)
# faster B[i] = X[c]
unsafe_bitsetindex!(Bc, convert(Bool, X[c]), i)
unsafe_bitsetindex!(B, convert(Bool, X[c]), i)
c += 1
end
end
Expand Down
7 changes: 3 additions & 4 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,11 @@ end
(.^)(A::BitArray, B::AbstractArray{Bool}) = (B .<= A)
(.^)(A::AbstractArray{Bool}, B::AbstractArray{Bool}) = (B .<= A)

function bitcache_pow{T}(Ac::Vector{UInt64}, B::Array{T}, l::Int, ind::Int, C::Vector{Bool})
function bitcache_pow{T}(A::BitArray, B::Array{T}, l::Int, ind::Int, C::Vector{Bool})
left = l - ind + 1
@inbounds begin
for j = 1:min(bitcache_size, left)
C[j] = unsafe_bitgetindex(Ac, ind) ^ B[ind]
C[j] = unsafe_bitgetindex(A, ind) ^ B[ind]
ind += 1
end
C[left+1:bitcache_size] = false
Expand All @@ -438,13 +438,12 @@ function (.^){T<:Integer}(A::BitArray, B::Array{T})
F = BitArray(shape)
l = length(F)
l == 0 && return F
Ac = A.chunks
Fc = F.chunks
C = Array(Bool, bitcache_size)
ind = 1
cind = 1
for i = 1:div(l + bitcache_size - 1, bitcache_size)
ind = bitcache_pow(Ac, B, l, ind, C)
ind = bitcache_pow(A, B, l, ind, C)
dumpbitcache(Fc, cind, C)
cind += bitcache_chunks
end
Expand Down
7 changes: 3 additions & 4 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ using .IteratorsMD
end
end

@inline unsafe_getindex(v::BitArray, ind::Int) = Base.unsafe_bitgetindex(v.chunks, ind)
@inline unsafe_getindex(v::BitArray, ind::Int) = Base.unsafe_bitgetindex(v, ind)

@inline unsafe_setindex!{T}(v::Array{T}, x::T, ind::Int) = (@inbounds v[ind] = x; v)
@inline unsafe_setindex!{T}(v::AbstractArray{T}, x::T, ind::Int) = (v[ind] = x; v)
@inline unsafe_setindex!(v::BitArray, x::Bool, ind::Int) = (Base.unsafe_bitsetindex!(v.chunks, x, ind); v)
@inline unsafe_setindex!(v::BitArray, x::Bool, ind::Int) = (Base.unsafe_bitsetindex!(v, x, ind); v)
@inline unsafe_setindex!{T}(v::AbstractArray{T}, x::T, ind::Real) = unsafe_setindex!(v, x, to_index(ind))

# Version that uses cartesian indexing for src
Expand Down Expand Up @@ -640,7 +640,6 @@ end
quote
@nexprs $N d->(I_d = I[d])
X = BitArray(index_shape($(Isplat...)))
Xc = X.chunks

stride_1 = 1
@nexprs $N d->(stride_{d+1} = stride_d * size(B, d))
Expand All @@ -649,7 +648,7 @@ end
@nloops($N, i, d->I_d,
d->(offset_{d-1} = offset_d + (i_d-1)*stride_d), # PRE
begin
unsafe_bitsetindex!(Xc, B[offset_0], ind)
unsafe_bitsetindex!(X, B[offset_0], ind)
ind += 1
end)
return X
Expand Down