Skip to content

Commit 031f278

Browse files
committed
Deprecate fallback elsize for AbstractArray
Just like the deprecation of strides (#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.
1 parent c3ffa99 commit 031f278

9 files changed

+22
-5
lines changed

base/abstractarray.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ prevind(::AbstractArray, i::Integer) = Int(i)-1
128128
nextind(::AbstractArray, i::Integer) = Int(i)+1
129129

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

133133
"""
134134
ndims(A::AbstractArray) -> Integer

base/array.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function bitsunionsize(u::Union)
135135
end
136136

137137
length(a::Array) = arraylen(a)
138-
elsize(a::Array{T}) where {T} = isbits(T) ? sizeof(T) : (isbitsunion(T) ? bitsunionsize(T) : sizeof(Ptr))
138+
elsize(::Type{<:Array{T}}) where {T} = isbits(T) ? sizeof(T) : (isbitsunion(T) ? bitsunionsize(T) : sizeof(Ptr))
139139
sizeof(a::Array) = Core.sizeof(a)
140140

141141
function isassigned(a::Array, i::Int...)

base/deprecated.jl

+14-2
Original file line numberDiff line numberDiff line change
@@ -1146,8 +1146,8 @@ workspace() = error("`workspace()` is discontinued, consider Revise.jl for an al
11461146
# Issues #17812 Remove default stride implementation
11471147
function strides(a::AbstractArray)
11481148
depwarn("""
1149-
`strides(a::AbstractArray)` is deprecated for general arrays.
1150-
Specialize `strides` for custom array types that have the appropriate representation in memory.
1149+
The default `strides(a::AbstractArray)` implementation is deprecated for general arrays.
1150+
Specialize `strides(::$(typeof(a).name))` if `$(typeof(a).name)` indeed uses a strided representation in memory.
11511151
Warning: inappropriately implementing this method for an array type that does not use strided
11521152
storage may lead to incorrect results or segfaults.
11531153
""", :strides)
@@ -1156,6 +1156,18 @@ end
11561156

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

1159+
# Issue #26072 Also remove default Base.elsize implementation
1160+
function elsize(t::Type{<:AbstractArray{T}}) where T
1161+
depwarn("""
1162+
The default `Base.elsize(::Type{<:AbstractArray})` implementation is deprecated for general arrays.
1163+
Specialize `Base.elsize(::Type{<:$(t.name)})` if `$(t.name)` indeed has a known representation
1164+
in memory such that it represents the distance between two contiguous elements.
1165+
Warning: inappropriately implementing this method may lead to incorrect results or segfaults.
1166+
""", :elsize)
1167+
sizeof(T)
1168+
end
1169+
1170+
11591171
@deprecate lexcmp(x::AbstractArray, y::AbstractArray) cmp(x, y)
11601172
@deprecate lexcmp(x::Real, y::Real) cmp(isless, x, y)
11611173
@deprecate lexcmp(x::Complex, y::Complex) cmp((real(x),imag(x)), (real(y),imag(y)))

base/reinterpretarray.jl

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function size(a::ReinterpretArray{T,N,S} where {N}) where {T,S}
4444
tuple(size1, tail(psize)...)
4545
end
4646

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

4950
@inline @propagate_inbounds getindex(a::ReinterpretArray{T,0}) where {T} = reinterpret(T, a.parent[])

base/reshapedarray.jl

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ IndexStyle(::Type{<:ReshapedArrayLF}) = IndexLinear()
179179
parent(A::ReshapedArray) = A.parent
180180
parentindices(A::ReshapedArray) = map(s->1:s, size(parent(A)))
181181
reinterpret(::Type{T}, A::ReshapedArray, dims::Dims) where {T} = reinterpret(T, parent(A), dims)
182+
elsize(::Type{<:ReshapedArray{<:Any,<:Any,P}}) where {P} = elsize(P)
182183

183184
unaliascopy(A::ReshapedArray) = typeof(A)(unaliascopy(A.parent), A.dims, A.mi)
184185
dataids(A::ReshapedArray) = dataids(A.parent)

base/strings/basic.jl

+1
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ length(s::CodeUnits) = ncodeunits(s.s)
648648
sizeof(s::CodeUnits{T}) where {T} = ncodeunits(s.s) * sizeof(T)
649649
size(s::CodeUnits) = (length(s),)
650650
strides(s::CodeUnits) = (1,)
651+
elsize(s::CodeUnits{T}) where {T} = sizeof(T)
651652
@propagate_inbounds getindex(s::CodeUnits, i::Int) = codeunit(s.s, i)
652653
IndexStyle(::Type{<:CodeUnits}) = IndexLinear()
653654
start(s::CodeUnits) = 1

base/subarray.jl

+2
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ compute_stride1(s, inds, I::Tuple{AbstractRange, Vararg{Any}}) = s*step(I[1])
266266
compute_stride1(s, inds, I::Tuple{Slice, Vararg{Any}}) = s
267267
compute_stride1(s, inds, I::Tuple{Any, Vararg{Any}}) = throw(ArgumentError("invalid strided index type $(typeof(I[1]))"))
268268

269+
elsize(::Type{<:SubArray{<:Any,<:Any,P}}) where {P} = elsize(P)
270+
269271
iscontiguous(A::SubArray) = iscontiguous(typeof(A))
270272
iscontiguous(::Type{<:SubArray}) = false
271273
iscontiguous(::Type{<:FastContiguousSubArray}) = true

stdlib/Random/src/RNGs.jl

+1
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ Base.getindex(a::UnsafeView, i::Int) = unsafe_load(a.ptr, i)
371371
Base.setindex!(a::UnsafeView, x, i::Int) = unsafe_store!(a.ptr, x, i)
372372
Base.pointer(a::UnsafeView) = a.ptr
373373
Base.size(a::UnsafeView) = (a.len,)
374+
Base.elsize(::UnsafeView{T}) where {T} = sizeof(T)
374375

375376
# this is essentially equivalent to rand!(r, ::AbstractArray{Float64}, I) above, but due to
376377
# optimizations which can't be done currently when working with pointers, we have to re-order

test/abstractarray.jl

-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,6 @@ end
772772
@testset "ndims and friends" begin
773773
@test ndims(Diagonal(rand(1:5,5))) == 2
774774
@test ndims(Diagonal{Float64}) == 2
775-
@test Base.elsize(Diagonal(rand(1:5,5))) == sizeof(Int)
776775
end
777776

778777
@testset "Issue #17811" begin

0 commit comments

Comments
 (0)