Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reimplement _setindex using recursion #54479

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

nsajko
Copy link
Contributor

@nsajko nsajko commented May 15, 2024

FTR, this PR is part of a series of changes which (re)implement many of the operations on tuples using a new recursive technique. The ultimate goal is to hopefully increase the value of the loop-vs-recurse cutoff (Any32, sometimes hardcoded 32) for tuple operations.

Updates #54462

Closes #54463

Example type inference improvement:

julia> Core.Compiler.return_type(Base.setindex, Tuple{Tuple{Vararg{Int}},Int,Int})
Tuple{Vararg{Int64}}

FTR, this PR is part of a series of changes which (re)implement many of
the operations on tuples using a new recursive technique. The ultimate
goal is to hopefully increase the value of the loop-vs-recurse cutoff
(`Any32`, sometimes hardcoded `32`) for tuple operations.

Updates JuliaLang#54462

Closes JuliaLang#54463

Example type inference improvement:

```julia-repl
julia> Core.Compiler.return_type(Base.setindex, Tuple{Tuple{Vararg{Int}},Int,Int})
Tuple{Vararg{Int64}}
```
@nsajko nsajko added domain:collections Data structures holding multiple items, e.g. sets compiler:inference Type inference labels May 15, 2024
@kimikage
Copy link
Contributor

kimikage commented May 15, 2024

It is difficult to understand the relationship to the issue and prior PR. (typo?)

from #54463 (comment)

(I think it is also misleading that the function _setindex exists in Base. Can't we use _setindex_tuple or something like that?)

I think there are two problems with the current (master) _setindex that have nothing to do with the inference.

  • Although the Base.setindex is also implemented by NamedTuple, etc., the signature does not indicate that it is for Tuple (due to the splatting).
  • The order of arguments is different from setindex due to the Vararg technique.

If we don't use Vararg, I don't see the need to change the order of the arguments from setindex.

@kimikage
Copy link
Contributor

I think the true NTuple cases (x::NTuple{N, T}, v::T, i::Integer) where {N, T} are worth specializing. It is easier to understand for humans as well as the compiler.

@nsajko
Copy link
Contributor Author

nsajko commented May 15, 2024

Regarding the naming of the functions, that's way off-topic IMO (to this PR).

I think the true NTuple cases ... are worth specializing

Why? As shown above, the generic implementation seems to handle this just fine.

@kimikage
Copy link
Contributor

kimikage commented May 15, 2024

Regarding the naming of the functions, that's way off-topic IMO (to this PR).

Agreed. However, we would probably lose the opportunity to change it.

I think the true NTuple cases ... are worth specializing

Why?

As noted above, it is because it is easy for humans to understand. TBH, I still don't understand this PR.
Also, I believe that not only inference but also runtime performance is important. Some benchmarks would be helpful.

If we were to try to support Base.setindex((1, 2, 3), 0, 2:3), would this PR technique work well? (Edit: cf. #54508)
Of course that has nothing to do with this PR, but I don't think having a simple implementation is a bad thing.

@aviatesk aviatesk removed the compiler:inference Type inference label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:collections Data structures holding multiple items, e.g. sets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants