Skip to content

Commit 6ea17fb

Browse files
committed
generic size: avoid method static parameters and abstract type assert
The generic method `size(::AbstractArray, ::Any)` currently has: * method static parameters * an abstract type assert (`::Integer`) in the method body PR JuliaLang#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 JuliaLang#59442. Another thing that is necessary for preserving good abstract inference is to convert the index to `Int` before using it. This is correct it there can't be more than `typemax(Int)` dimensions anyway. (cherry picked from PR JuliaLang#59465)
1 parent a0dca05 commit 6ea17fb

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

base/abstractarray.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ 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+
function f(t::Tuple, d::Int)
44+
d <= length(t) ? t[d] : 1
45+
end
46+
sz = size(t)
47+
d = Int(dim)
48+
@inline f(sz, d)
49+
end
4350

4451
"""
4552
axes(A, d)

0 commit comments

Comments
 (0)