Skip to content

Commit 0551b2f

Browse files
authored
generic size: avoid method static parameters and abstract type assert (#59465)
The generic method `size(::AbstractArray, ::Any)` currently has: * method static parameters * an abstract type assert (`::Integer`) in the method body PR #59442 aims to make this generic method be used for `Array` and `Memory`, by deleting the more specific methods. It is my understanding that method static parameters and abstract type asserts can cause performance issues, when they're not optimized out, which happens when the argument types are not concretely inferred. So I'm putting up this PR, which eliminates the method static parameters and the `typeassert`, to prevent any possible regressions from PR #59442. Another thing that is necessary for preserving good abstract inference is to convert the index argument to `Int` before using it. This is correct as there can't be more than `typemax(Int)` dimensions anyway (the `N` type parameter to `AbstractArray` is an `Int` value).
1 parent 72b0fe2 commit 0551b2f

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

base/abstractarray.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ julia> size(A, 2)
3939
3
4040
```
4141
"""
42-
size(t::AbstractArray{T,N}, d) where {T,N} = d::Integer <= N ? size(t)[d] : 1
42+
function size(t::AbstractArray, dim)
43+
d = Int(dim)::Int
44+
s = size(t)
45+
d <= length(s) ? s[d] : 1
46+
end
4347

4448
"""
4549
axes(A, d)

0 commit comments

Comments
 (0)