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

Resolve aliasing problems in getindex #60

Merged
merged 3 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "PooledArrays"
uuid = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
version = "1.2.0"
version = "1.2.1"

[deps]
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand Down
45 changes: 15 additions & 30 deletions src/PooledArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -444,40 +444,25 @@ Base.convert(::Type{Array}, pa::PooledArray{T, R, N}) where {T, R, N} = convert(

# We need separate functions due to dispatch ambiguities

for T in (PooledArray, SubArray{<:Any, <:Any, <:PooledArray})
@eval Base.@propagate_inbounds function Base.getindex(A::$T, I::Integer...)
idx = DataAPI.refarray(A)[I...]
iszero(idx) && throw(UndefRefError())
return @inbounds DataAPI.refpool(A)[idx]
end

@eval Base.@propagate_inbounds function Base.getindex(A::$T, I::Union{Real, AbstractVector}...)
# make sure we do not increase A.refcount in case creation of newrefs fails
newrefs = DataAPI.refarray(A)[I...]
@assert newrefs isa AbstractArray
Threads.atomic_add!(refcount(A), 1)
return PooledArray(RefArray(newrefs), DataAPI.invrefpool(A), DataAPI.refpool(A), refcount(A))
end
end

if VERSION < v"1.1"
Base.@propagate_inbounds function Base.getindex(A::SubArray{T,D,P,I,true} ,
i::Int) where {I<:Tuple{Union{Base.Slice,
AbstractUnitRange},
Vararg{Any}}, P<:PooledArray, T, D}
idx = DataAPI.refarray(A)[i]
iszero(idx) && throw(UndefRefError())
return @inbounds DataAPI.refpool(A)[idx]
end
end

# Defined to avoid ambiguities with Base
Base.@propagate_inbounds function Base.getindex(A::SubArray{<:Any, N, <:PooledArray}, I::Vararg{Int,N}) where {T,N}
idx = DataAPI.refarray(A)[I...]
Base.@propagate_inbounds function Base.getindex(A::PooledArray, I::Int)
idx = DataAPI.refarray(A)[I]
iszero(idx) && throw(UndefRefError())
return @inbounds DataAPI.refpool(A)[idx]
end

# we handle fast only the case when the first index is an abstract vector
# this is to make sure other indexing synraxes use standard dispatch from Base
# the reason is that creation of DataAPI.refarray(A) is unfortunately slow
Base.@propagate_inbounds function Base.getindex(A::PooledArrOrSub,
I1::AbstractVector,
I2::Union{Real, AbstractVector}...)
# make sure we do not increase A.refcount in case creation of newrefs fails
newrefs = DataAPI.refarray(A)[I1, I2...]
@assert newrefs isa AbstractArray
Threads.atomic_add!(refcount(A), 1)
return PooledArray(RefArray(newrefs), DataAPI.invrefpool(A), DataAPI.refpool(A), refcount(A))
end

Base.@propagate_inbounds function Base.isassigned(pa::PooledArrOrSub, I::Int...)
!iszero(DataAPI.refarray(pa)[I...])
end
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,13 @@ end
@test pav[1:1, [1, 2]] == [1 2]
@test pav[[1], 1:2] == [1 2]
@test pav[[1], [1, 2]] == [1 2]

pav2 = view(PooledArray([1]), 1)
pa2 = similar(pav2)
pa2[] = 10

@test pav2[] == 1
@test pa2[] == 10
end

@testset "isassigned" begin
Expand Down