-
Notifications
You must be signed in to change notification settings - Fork 833
Type subsumption cache: handle unsolved type vars #19040
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
base: main
Are you sure you want to change the base?
Conversation
❗ Release notes required
|
|
Yet another bug. This needs a careful look. I need to run some benchmarks again, too and compare standalone builds with the original Vlad's implementation. |
|
@majocha: It would equally help if two typars are both unsolved, but strip down to the same form. Wasn't the mistake more the fact that solved/unsolved form of the same TType_var had always the same key (based on Stamp) ? This is definitely a safe fix, but I wonder if it doesn't too much limit the potential of the cache for typars. |
Yes, that makes sense. It makes me wonder if we could also handle unsolved nullness better. |
|
Nullness is not stamped, so in the case of unresolved/not yet resolved/ nullness, there is no identifier to hold onto. |
|
This fix is accidental, the real problem is here: fsharp/src/Compiler/Utilities/TypeHashing.fs Line 480 in 9670f60
This was intended to speed up things by weakly attaching the computed TypeStructures to their respective TTypes. We cannot do it for not fully solved types, they still mutate and things get outdated.
|
Yes, in fact in case of typars we don't want the stamp as identity at all. We should use the structure of the solution (TType) if available or a common token for unsolved. |
Co-authored-by: Brian Rourke Boll <[email protected]>
|
I'm testing this in the IDE with the notorious OpenTK 5.0. It improved things significantly, there are way less cache entries now but much more hits, resulting in 99% ratio with little memory use and no constant churn during edits: |
| toNullnessToken n | ||
|
|
||
| | TType_var(r, n) -> | ||
| TypeToken.Stamp r.Stamp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that the stamp is gone, I feel we are missing something to differentiate constraints.
Most likely types where constraints matter will be unsolved, so this will bypass the cache (via shouldCache=false) anyway. Right now cannot come up with a specific example of solved types where also constraints would matter for subsumption.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldCache here only affects the memoization of TType -> TypeStructure, it will not prevent the caching of checkSubsumes result for unsolved.
Yeah this is missing constraints. I feel encoding them could cause this to explode in size.
What I completely missed also is the notion of rigidity. As I understand not all variables end up solved.
| let rec private accumulateTypar (typar: Typar) = | ||
| seq { | ||
| match typar.Solution with | ||
| | Some ty -> yield! accumulateTType ty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, the input going into the cache is already stripped, right?
i.e. we should not be getting long chains of solution pointers for something which is solved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah since this is operating solely on stripped types, if we encounter a type var, it should never be a solved one. In theory.
|
The huge mistake that causes a lot of inefficiency was emitting typar stamps as part of the cache key. This causes a lot of equivalent but really unusable keys polluting the cache. The increased churn is especially heavy on the IDE. I cannot see a solution to this, that would guarantee soundness. |
What about skipping cache for them? Of course it somewhat limits the genericity of the cache, and is special-tailored to the place of application. The typesubsumption was really needed for concrete types, with super types and interface hierarchies. I was thinking of content-based hashing, but you would need to hash all type constraints content-wise as they are mutable as well. Maybe the inherent mutation for solutions and constraints of a type var are good enough reasons to treat them differently? |
Description
Fixes #19037
Added repro test case.