Skip to content

Commit 2565bed

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 #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 to `Int` before using it. This is correct it there can't be more than `typemax(Int)` dimensions anyway. (cherry picked from PR #59465)
1 parent dcdc3e9 commit 2565bed

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

base/abstractarray.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,17 @@ 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 integer_to_int(d::Integer) # throw for non-`Integer` without abstract `typeassert`
44+
Int(d)::Int
45+
end
46+
function f(s::Tuple, d::Int) # throw for non-`Tuple` without abstract `typeassert`
47+
d <= length(s) ? s[d] : 1
48+
end
49+
d = @inline integer_to_int(dim)
50+
s = size(t)
51+
@inline f(s, d)
52+
end
4353

4454
"""
4555
axes(A, d)

0 commit comments

Comments
 (0)