Make expression cache robust for nested generic calls - #22059
Open
ilevkivskyi wants to merge 1 commit into
Open
ilevkivskyi wants to merge 1 commit into
ilevkivskyi wants to merge 1 commit into
Conversation
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: core (https://github.com/home-assistant/core)
- Warning: disabling incremental mode may severely reduce performance
- If this is intentional, delete '.mypy_cache' to suppress this warning
rotki (https://github.com/rotki/rotki)
+ rotkehlchen/api/server.py:487: error: Value of type variable "ErrorHandlerT" of "error_handler" of "Parser" cannot be "Callable[[ValidationError, LocalProxy[Any], Schema, int | None, dict[Any, Any] | None], None]" [type-var]
+ rotkehlchen/api/server.py:488: error: Value of type variable "ErrorHandlerT" of "error_handler" of "Parser" cannot be "Callable[[ValidationError, LocalProxy[Any], Schema, int | None, dict[Any, Any] | None], None]" [type-var]
|
Member
Author
|
The diff in |
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.
Example in the added test takes almost a minute to check on current master, while with this PR it only takes milliseconds. This is unlikely to affect any real code (but should reduce the number of rare extreme cases of poor performance).
The problem is that we call
freshen_function_type_vars()each time we type-check a generic function call. This means that arguments will get new type context each time, and our expression cache (which is our main guard against poor performance for deeply nested expressions) stops working, because type context is a part of the cache key.The fix seems simple, only call
freshen_function_type_vars()once per call expression (I also include original callable type as part of the new cache key for overloads). An alternative would be to erase type context before caching, butTypeVarEraservisitor takes some time, while expression cache is an ultra-hot code path.Note the test I added relies on the fact that we call
freshen_function_type_vars()incheckmember.py, without this PR it fails with:Btw, I didn't find this while profiling slow libraries, I actually need this for something radical. I think I know how to fix mypy overusing outer context during inference once and for all (and likely remove all existing ad-hoc heuristics that compensate for this).