Skip to content

Commit 59bc99d

Browse files
authored
eliminate some nongeneric methods of length and size (#59442)
Changes: * Eliminate some nongeneric `length` methods: * `GenericMemory` * `Slice` * `IdentityUnitRange` * `CartesianIndices` * `LogicalIndex` * `CodeUnits` * `UnsafeView` (from the `Random` stdlib) * Eliminate some nongeneric two-argument `size` methods: * `GenericMemory` * `Array` * `BitVector` Depends on PR #59465 to prevent abstract inference regressions.
1 parent eb2d838 commit 59bc99d

File tree

8 files changed

+2
-28
lines changed

8 files changed

+2
-28
lines changed

base/array.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,6 @@ function vect(X...)
185185
return T[X...]
186186
end
187187

188-
size(a::Array, d::Integer) = size(a, Int(d)::Int)
189-
function size(a::Array, d::Int)
190-
d < 1 && error("arraysize: dimension out of range")
191-
sz = getfield(a, :size)
192-
return d > length(sz) ? 1 : getfield(sz, d, false) # @inbounds
193-
end
194-
195188
asize_from(a::Array, n) = n > ndims(a) ? () : (size(a,n), asize_from(a, n+1)...)
196189

197190
allocatedinline(@nospecialize T::Type) = (@_total_meta; ccall(:jl_stored_inline, Cint, (Any,), T) != Cint(0))

base/bitarray.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ length(B::BitArray) = B.len
104104
size(B::BitVector) = (B.len,)
105105
size(B::BitArray) = B.dims
106106

107-
@inline function size(B::BitVector, d::Integer)
108-
d < 1 && throw_boundserror(size(B), d)
109-
ifelse(d == 1, B.len, 1)
110-
end
111-
112107
isassigned(B::BitArray, i::Int) = 1 <= i <= length(B)
113108

114109
IndexStyle(::Type{<:BitArray}) = IndexLinear()

base/essentials.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Bottom = Union{}
99
# Define minimal array interface here to help code used in macros:
1010
size(a::Array) = getfield(a, :size)
1111
length(t::AbstractArray) = (@inline; prod(size(t)))
12-
length(a::GenericMemory) = getfield(a, :length)
12+
size(a::GenericMemory) = (getfield(a, :length),)
1313
throw_boundserror(A, I) = (@noinline; throw(BoundsError(A, I)))
1414

1515
# multidimensional getindex will be defined later on

base/genericmemory.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ AtomicMemory
6161

6262
using Core: memoryrefoffset, memoryref_isassigned # import more functions which were not essential
6363

64-
size(a::GenericMemory, d::Int) =
65-
d < 1 ? error("dimension out of range") :
66-
d == 1 ? length(a) :
67-
1
68-
size(a::GenericMemory, d::Integer) = size(a, convert(Int, d))
69-
size(a::GenericMemory) = (length(a),)
70-
7164
IndexStyle(::Type{<:GenericMemory}) = IndexLinear()
7265

7366
parent(ref::GenericMemoryRef) = ref.mem

base/indices.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,6 @@ axes1(S::Slice{<:AbstractOneTo{<:Integer}}) = S.indices
392392
first(S::Slice) = first(S.indices)
393393
last(S::Slice) = last(S.indices)
394394
size(S::Slice) = (length(S.indices),)
395-
length(S::Slice) = length(S.indices)
396395
getindex(S::Slice, i::Int) = (@inline; @boundscheck checkbounds(S, i); i)
397396
getindex(S::Slice, i::AbstractUnitRange{<:Integer}) = (@inline; @boundscheck checkbounds(S, i); i)
398397
getindex(S::Slice, i::StepRange{<:Integer}) = (@inline; @boundscheck checkbounds(S, i); i)
@@ -420,7 +419,6 @@ axes1(S::IdentityUnitRange{<:AbstractOneTo{<:Integer}}) = S.indices
420419
first(S::IdentityUnitRange) = first(S.indices)
421420
last(S::IdentityUnitRange) = last(S.indices)
422421
size(S::IdentityUnitRange) = (length(S.indices),)
423-
length(S::IdentityUnitRange) = length(S.indices)
424422
unsafe_length(S::IdentityUnitRange) = unsafe_length(S.indices)
425423
getindex(S::IdentityUnitRange, i::Integer) = (@inline; @boundscheck checkbounds(S, i); convert(eltype(S), i))
426424
getindex(S::IdentityUnitRange, i::Bool) = throw(ArgumentError("invalid index: $i of type Bool"))

base/multidimensional.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,6 @@ module IteratorsMD
485485

486486
size(iter::CartesianIndices) = map(length, iter.indices)
487487

488-
length(iter::CartesianIndices) = prod(size(iter))
489-
490488
# make CartesianIndices a multidimensional range
491489
Base.step(iter::CartesianIndices) = CartesianIndex(map(step, iter.indices))
492490

@@ -836,7 +834,6 @@ LogicalIndex(mask::AbstractVector{Bool}) = LogicalIndex{Int, typeof(mask)}(mask)
836834
LogicalIndex(mask::AbstractArray{Bool, N}) where {N} = LogicalIndex{CartesianIndex{N}, typeof(mask)}(mask)
837835
LogicalIndex{Int}(mask::AbstractArray) = LogicalIndex{Int, typeof(mask)}(mask)
838836
size(L::LogicalIndex) = (L.sum,)
839-
length(L::LogicalIndex) = L.sum
840837
collect(L::LogicalIndex) = [i for i in L]
841838
show(io::IO, r::LogicalIndex) = print(io,collect(r))
842839
print_array(io::IO, X::LogicalIndex) = print_array(io, collect(X))

base/strings/basic.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,8 @@ struct CodeUnits{T,S<:AbstractString} <: DenseVector{T}
791791
CodeUnits(s::S) where {S<:AbstractString} = new{codeunit(s),S}(s)
792792
end
793793

794-
length(s::CodeUnits) = ncodeunits(s.s)
795794
sizeof(s::CodeUnits{T}) where {T} = ncodeunits(s.s) * sizeof(T)
796-
size(s::CodeUnits) = (length(s),)
795+
size(s::CodeUnits) = (ncodeunits(s.s),)
797796
elsize(s::Type{<:CodeUnits{T}}) where {T} = sizeof(T)
798797
@propagate_inbounds getindex(s::CodeUnits, i::Int) = codeunit(s.s, i)
799798
IndexStyle(::Type{<:CodeUnits}) = IndexLinear()

stdlib/Random/src/Random.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ struct UnsafeView{T} <: DenseArray{T,1}
309309
len::Int
310310
end
311311

312-
Base.length(a::UnsafeView) = a.len
313312
Base.getindex(a::UnsafeView, i::Int) = unsafe_load(a.ptr, i)
314313
Base.setindex!(a::UnsafeView, x, i::Int) = unsafe_store!(a.ptr, x, i)
315314
Base.pointer(a::UnsafeView) = a.ptr

0 commit comments

Comments
 (0)