Skip to content

Commit

Permalink
Deprecate fallback elsize for AbstractArray (JuliaLang#26379)
Browse files Browse the repository at this point in the history
Just like the deprecation of strides (JuliaLang#25321), the default implementation for `elsize` is generally not true. This deprecates the fallback, restructures it to be defined on the type (and not the value), and implements it where possible for the builtin array types and wrappers.
  • Loading branch information
mbauman committed Mar 12, 2018
1 parent 00ac33a commit 0a7bb64
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ prevind(::AbstractArray, i::Integer) = Int(i)-1
nextind(::AbstractArray, i::Integer) = Int(i)+1

eltype(::Type{<:AbstractArray{E}}) where {E} = @isdefined(E) ? E : Any
elsize(::AbstractArray{T}) where {T} = sizeof(T)
elsize(A::AbstractArray) = elsize(typeof(A))

"""
ndims(A::AbstractArray) -> Integer
Expand Down
2 changes: 1 addition & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function bitsunionsize(u::Union)
end

length(a::Array) = arraylen(a)
elsize(a::Array{T}) where {T} = isbits(T) ? sizeof(T) : (isbitsunion(T) ? bitsunionsize(T) : sizeof(Ptr))
elsize(::Type{<:Array{T}}) where {T} = isbits(T) ? sizeof(T) : (isbitsunion(T) ? bitsunionsize(T) : sizeof(Ptr))
sizeof(a::Array) = Core.sizeof(a)

function isassigned(a::Array, i::Int...)
Expand Down
16 changes: 14 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1147,8 +1147,8 @@ workspace() = error("`workspace()` is discontinued, consider Revise.jl for an al
# Issues #17812 Remove default stride implementation
function strides(a::AbstractArray)
depwarn("""
`strides(a::AbstractArray)` is deprecated for general arrays.
Specialize `strides` for custom array types that have the appropriate representation in memory.
The default `strides(a::AbstractArray)` implementation is deprecated for general arrays.
Specialize `strides(::$(typeof(a).name))` if `$(typeof(a).name)` indeed uses a strided representation in memory.
Warning: inappropriately implementing this method for an array type that does not use strided
storage may lead to incorrect results or segfaults.
""", :strides)
Expand All @@ -1157,6 +1157,18 @@ end

@deprecate substrides(s, parent, dim, I::Tuple) substrides(parent, strides(parent), I)

# Issue #26072 Also remove default Base.elsize implementation
function elsize(t::Type{<:AbstractArray{T}}) where T
depwarn("""
The default `Base.elsize(::Type{<:AbstractArray})` implementation is deprecated for general arrays.
Specialize `Base.elsize(::Type{<:$(t.name)})` if `$(t.name)` indeed has a known representation
in memory such that it represents the distance between two contiguous elements.
Warning: inappropriately implementing this method may lead to incorrect results or segfaults.
""", :elsize)
sizeof(T)
end


@deprecate lexcmp(x::AbstractArray, y::AbstractArray) cmp(x, y)
@deprecate lexcmp(x::Real, y::Real) cmp(isless, x, y)
@deprecate lexcmp(x::Complex, y::Complex) cmp((real(x),imag(x)), (real(y),imag(y)))
Expand Down
1 change: 1 addition & 0 deletions base/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function size(a::ReinterpretArray{T,N,S} where {N}) where {T,S}
tuple(size1, tail(psize)...)
end

elsize(::Type{<:ReinterpretArray{T}}) where {T} = sizeof(T)
unsafe_convert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} = Ptr{T}(unsafe_convert(Ptr{S},a.parent))

@inline @propagate_inbounds getindex(a::ReinterpretArray{T,0}) where {T} = reinterpret(T, a.parent[])
Expand Down
1 change: 1 addition & 0 deletions base/reshapedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ IndexStyle(::Type{<:ReshapedArrayLF}) = IndexLinear()
parent(A::ReshapedArray) = A.parent
parentindices(A::ReshapedArray) = map(s->1:s, size(parent(A)))
reinterpret(::Type{T}, A::ReshapedArray, dims::Dims) where {T} = reinterpret(T, parent(A), dims)
elsize(::Type{<:ReshapedArray{<:Any,<:Any,P}}) where {P} = elsize(P)

unaliascopy(A::ReshapedArray) = typeof(A)(unaliascopy(A.parent), A.dims, A.mi)
dataids(A::ReshapedArray) = dataids(A.parent)
Expand Down
1 change: 1 addition & 0 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ length(s::CodeUnits) = ncodeunits(s.s)
sizeof(s::CodeUnits{T}) where {T} = ncodeunits(s.s) * sizeof(T)
size(s::CodeUnits) = (length(s),)
strides(s::CodeUnits) = (1,)
elsize(s::CodeUnits{T}) where {T} = sizeof(T)
@propagate_inbounds getindex(s::CodeUnits, i::Int) = codeunit(s.s, i)
IndexStyle(::Type{<:CodeUnits}) = IndexLinear()
start(s::CodeUnits) = 1
Expand Down
2 changes: 2 additions & 0 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ compute_stride1(s, inds, I::Tuple{AbstractRange, Vararg{Any}}) = s*step(I[1])
compute_stride1(s, inds, I::Tuple{Slice, Vararg{Any}}) = s
compute_stride1(s, inds, I::Tuple{Any, Vararg{Any}}) = throw(ArgumentError("invalid strided index type $(typeof(I[1]))"))

elsize(::Type{<:SubArray{<:Any,<:Any,P}}) where {P} = elsize(P)

iscontiguous(A::SubArray) = iscontiguous(typeof(A))
iscontiguous(::Type{<:SubArray}) = false
iscontiguous(::Type{<:FastContiguousSubArray}) = true
Expand Down
1 change: 1 addition & 0 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ Base.getindex(a::UnsafeView, i::Int) = unsafe_load(a.ptr, i)
Base.setindex!(a::UnsafeView, x, i::Int) = unsafe_store!(a.ptr, x, i)
Base.pointer(a::UnsafeView) = a.ptr
Base.size(a::UnsafeView) = (a.len,)
Base.elsize(::UnsafeView{T}) where {T} = sizeof(T)

# this is essentially equivalent to rand!(r, ::AbstractArray{Float64}, I) above, but due to
# optimizations which can't be done currently when working with pointers, we have to re-order
Expand Down
1 change: 0 additions & 1 deletion test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,6 @@ end
@testset "ndims and friends" begin
@test ndims(Diagonal(rand(1:5,5))) == 2
@test ndims(Diagonal{Float64}) == 2
@test Base.elsize(Diagonal(rand(1:5,5))) == sizeof(Int)
end

@testset "Issue #17811" begin
Expand Down

0 comments on commit 0a7bb64

Please sign in to comment.