Skip to content

Conversation

nsajko
Copy link
Member

@nsajko nsajko commented Sep 2, 2025

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).

@nsajko
Copy link
Member Author

nsajko commented Sep 2, 2025

Indeed, this PR is a prerequisite for PR #59442. Otherwise three of these effect inference tests fail:

let effects = Base.infer_effects(size, (Array,Int))
@test Compiler.is_consistent_if_inaccessiblememonly(effects)
@test Compiler.is_effect_free(effects)
@test !Compiler.is_nothrow(effects)
@test Compiler.is_terminates(effects)
end

@nsajko nsajko marked this pull request as ready for review September 2, 2025 17:47
@nsajko nsajko force-pushed the size_AbstractArray branch from 701c909 to 94abb7f Compare September 3, 2025 14:04
nsajko added a commit to nsajko/julia that referenced this pull request Sep 3, 2025
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)
nsajko added a commit to nsajko/julia that referenced this pull request Sep 3, 2025
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)
nsajko added a commit to nsajko/julia that referenced this pull request Sep 3, 2025
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)
@nsajko nsajko force-pushed the size_AbstractArray branch from 8302694 to 4f3b8c2 Compare September 3, 2025 15:22
nsajko added a commit to nsajko/julia that referenced this pull request Sep 3, 2025
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)
nsajko added a commit to nsajko/julia that referenced this pull request Sep 3, 2025
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)
@nsajko
Copy link
Member Author

nsajko commented Sep 5, 2025

bump

@nsajko
Copy link
Member Author

nsajko commented Sep 9, 2025

ping

Copy link
Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this. It changes clear code into a mess. Changing to simplified code like this would be okay though:

function size(t::AbstractArray, dim)
    d = Int(dim)::Int
    s = size(t)
    d <= length(s) ? s[d] : 1
end

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.
@nsajko nsajko force-pushed the size_AbstractArray branch from a4f8310 to 4ae27ee Compare September 9, 2025 16:19
Now this PR changes behavior: non-`Integer` values are allowed for the
second argument as long as they are convertible to `Int`.
Co-authored-by: Jeff Bezanson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants