inference: (slightly) improve type stability of capturing closures#56532
Open
inference: (slightly) improve type stability of capturing closures#56532
Conversation
Collaborator
|
Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. |
72be4a1 to
56de49c
Compare
Member
Author
|
@nanosoldier |
Collaborator
|
Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. |
This was referenced Nov 14, 2024
aviatesk
added a commit
that referenced
this pull request
Nov 15, 2024
- removed unnecessary `Core.Box` allocation - made the type of the closure that is passed to `Future` concrete That said, it doesn’t seem ideal to require this sort of manual optimizations.. The value of using closures cannot be denied in this code base, and I feel that it would be better to work towards optimizing closures more (as we do with #56532)?
aviatesk
added a commit
that referenced
this pull request
Nov 20, 2024
This is an alternative to #56532 and can resolve #31909. Currently `apply_type_tfunc` is unable to handle `Union`-argtypes with any precision. With this change, `apply_type_tfunc` now performs union-splitting on `Union`-argtypes and returns the merged result of the splits. While this can improve inference precision, we might need to be cautious about potential inference time bloat.
aviatesk
added a commit
that referenced
this pull request
Nov 20, 2024
This is an alternative to #56532 and can resolve #31909. Currently `apply_type_tfunc` is unable to handle `Union`-argtypes with any precision. With this change, `apply_type_tfunc` now performs union-splitting on `Union`-argtypes and returns the merged result of the splits. While this can improve inference precision, we might need to be cautious about potential inference time bloat.
aviatesk
added a commit
that referenced
this pull request
Nov 21, 2024
This is an alternative to #56532 and can resolve #31909. Currently `apply_type_tfunc` is unable to handle `Union`-argtypes with any precision. With this change, `apply_type_tfunc` now performs union-splitting on `Union`-argtypes and returns the merged result of the splits. While this can improve inference precision, we might need to be cautious about potential inference time bloat.
aviatesk
added a commit
that referenced
this pull request
Nov 21, 2024
This is an alternative to #56532 and can resolve #31909. Currently `apply_type_tfunc` is unable to handle `Union`-argtypes with any precision. With this change, `apply_type_tfunc` now performs union-splitting on `Union`-argtypes and returns the merged result of the splits. While this can improve inference precision, we might need to be cautious about potential inference time bloat.
aviatesk
added a commit
that referenced
this pull request
Nov 21, 2024
This is an alternative to #56532 and can resolve #31909. Currently `apply_type_tfunc` is unable to handle `Union`-argtypes with any precision. With this change, `apply_type_tfunc` now performs union-splitting on `Union`-argtypes and returns the merged result of the splits. While this can improve inference precision, we might need to be cautious about potential inference time bloat.
aviatesk
added a commit
that referenced
this pull request
Nov 22, 2024
This is an alternative to #56532 and can resolve #31909. Currently `apply_type_tfunc` is unable to handle `Union`-argtypes with any precision. With this change, `apply_type_tfunc` now performs union-splitting on `Union`-argtypes and returns the merged result of the splits. While this can improve inference precision, we might need to be cautious about potential inference time bloat.
aviatesk
added a commit
that referenced
this pull request
Nov 22, 2024
This is an alternative to #56532 and can resolve #31909. Currently `apply_type_tfunc` is unable to handle `Union`-argtypes with any precision. With this change, `apply_type_tfunc` now performs union-splitting on `Union`-argtypes and returns the merged result of the splits. While this can improve inference precision, we might need to be cautious about potential inference time bloat. --------- Co-authored-by: Jameson Nash <vtjnash@gmail.com>
As an idea to improve type stability for capturing closures, such as in #31909, I tried this idea of propagating the closure object as a `PartialStruct` whose `fields` include captured variables of which types are (partially) known. By performing const-prop on this `closure::PartialStruct`, we can achieve a certain level of type stability. Specifically, I made some modifications to `abstract_eval_new` to allow creating `PartialStruct` even for `:new` objects that are `!isconcretedispatch` (since `PartialStruct` can now represent abstract elements). I also adjusted `const_prop_argument_heuristic` to perform aggressive constant propagation using such `closure::PartialStruct`. As a result, the following code now achieves type stability: ```julia julia> Base.infer_return_type((Bool,Int,)) do b, y x = b ? 1 : missing inner = y -> x + y return inner(y) end Any # master Union{Missing, Int64} # this commit ``` However, this alone was not enough to fully resolve #31909. The call graph of `map` is extremely complex, and simply applying constant propagation everywhere does not achieve the type safety requested in the issue. Nevertheless this commit alone would still improve type stability for some cases, so I will go ahead and submit it as a PR.
56de49c to
fb76bfe
Compare
serenity4
pushed a commit
to serenity4/julia
that referenced
this pull request
May 1, 2025
- removed unnecessary `Core.Box` allocation - made the type of the closure that is passed to `Future` concrete That said, it doesn’t seem ideal to require this sort of manual optimizations.. The value of using closures cannot be denied in this code base, and I feel that it would be better to work towards optimizing closures more (as we do with JuliaLang#56532)?
serenity4
pushed a commit
to serenity4/julia
that referenced
this pull request
May 1, 2025
…6617) This is an alternative to JuliaLang#56532 and can resolve JuliaLang#31909. Currently `apply_type_tfunc` is unable to handle `Union`-argtypes with any precision. With this change, `apply_type_tfunc` now performs union-splitting on `Union`-argtypes and returns the merged result of the splits. While this can improve inference precision, we might need to be cautious about potential inference time bloat. --------- Co-authored-by: Jameson Nash <vtjnash@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As an idea to improve type stability for capturing closures, such as in #31909, I tried this idea of propagating the closure object as a
PartialStructwhosefieldsinclude captured variables of which types are (partially) known. By performing const-prop on thisclosure::PartialStruct, we can achieve a certain level of type stability.Specifically, I made some modifications to
abstract_eval_newto allow creatingPartialStructeven for:newobjects that are!isconcretedispatch(sincePartialStructcan now represent abstract elements). I also adjustedconst_prop_argument_heuristicto perform aggressive constant propagation using suchclosure::PartialStruct.As a result, the following code now achieves type stability:
However, this alone was not enough to fully resolve #31909. The call graph of
mapis extremely complex, and simply applying constant propagation everywhere does not achieve the type safety requested in the issue.Nevertheless this commit alone would still improve type stability for some cases, so I will go ahead and submit it as a PR.
@nanosoldier
runbenchmarks("inference", vs=":master")