-
Notifications
You must be signed in to change notification settings - Fork 830
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
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: |
|
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? |
|
So, basically, we care only about caching I wonder if the constraints are really a problem wrt to type subsumption cache. Because the current situation where we emit only a stamp as typar identity worked so far ( the bug this PR tries to fix is elsewhere). A given typar can very well have its constraints mutated while the stamp stays constant, so it is already not collision free in general. It just does not collide in practice for this usage. I think it was like this since the original Vlad's implementation. Copilot when asked says: Q: which typar constraints can influence the result of TypeFeasiblySubsumesType? I wonder because I need to know what should take part in keying the TypeSubsumptionCache.
hmm. |
|
I quickly asked copilot: Q: Does TypeFeasiblySubsumesType take nullness into account ? A: Short answer: No. The predicate is intentionally nullness-agnostic. |
|
As usual I'm testing this in the IDE. I see way less cache churn now and good hit ratio. For example after some minutes of copilot work in this solution:
|
Would it make sense to publish a built release .vsix for |
|
Not sure if it goes through |
|
Another potential problem to check, given that we operate on stripped types, can we really trust that a type that looks like it has no type vars at all wouldn't reverse into an unsolved one in the course of Trace.Undo? It looks to me more and more like the whole idea of memoizing TType -> TypeStructure is not great. We really cannot pretend this is a pure function. |
Actually, the fact that the function strips first, and then looks into the weak cache, is of help here. When that solution is undone, stripping will not kick in (or stop at the last ttype_var) and you would get the TType_var as the key. Stripping helps to ensure that when we have a solved Ttype_var, it will not go as cache key, but rather its solution will go. The hypothetical problem is with speculative additions of constraints, since stripping does not change the reference there. |
|
@majocha : I would want to merge this in and start dogfooding, WDYT ? |
Yes I think this is fine now, I'm using it and not seeing any anomalies. |
Description
Fixes #19037
Added repro test case.
The problem was in memoization of
TType->TypeStructuregeneration.TTypeis inherently mutable. OneTTypeinstance can result in differentTypeFeasiblySubsumesTypeoutcomes and differentTypeStructurekey fragments, as type arguments get solved. For the purpose of type subsumption caching, we can treat as stable only fully solved types, with solved or rigid type vars. We still want the cache to use the unstable ones, but we must produce newTypeStructuresfrom theTTypesas they evolve towards a solution.Additionally, caching is now limited only to
TType_appcases.Note:
Cache keys do not take into account constraints. This seems to work in practice, because
TypeFeasiblySubsumesType, where the cache is applied, does not use them either.If this turns out to be a problem, or we ever need to use
TypeStructuresalso somewhere else, we should emit a structural fingerprint ofTypar.Constraints, too.