master
Name already in use
Commits on Dec 29, 2022
-
Speed up make_simplified_union (#14370)
If there is only one non-union item, there's nothing interesting to do. This is pretty common, and it avoids a fairly expensive `_remove_redundant_union_items` call. (Various small optimizations, including this, together netted a 6% performance improvement in self check.)
-
Micro-optimize get_proper_type(s) (#14369)
These are used a lot, so it makes sense to tune them a bit. We now avoid allocations in a common case, when compiled. (Various small optimizations, including these, together netted a 6% performance improvement in self check.)
-
Micro-optimize flatten_nested_unions (#14368)
Avoid constructing a new list if there is nothing to flatten (and the input is a list, which is often the case).
-
Some semantic analyzer micro-optimizations (#14367)
The biggest change is replacing some calls to bound methods with trait method calls, which are faster when compiled. Also remove an unused argument to TypeVarLikeQuery and make a few misc tweaks. (Various small optimizations, including these, together netted a 6% performance improvement in self check.)
-
[mypyc] Fixes to union simplification (#14364)
Flatten nested unions before simplifying unions. Simplify item type unions in loops. This fixes a crash introduced in #14363.
-
A few miscellaneous micro-optimizations (#14366)
These are part of the changes that collectively bring a 6% performance improvement. These are all pretty minor.
-
stubtest: Improve error message for
__all__
-related errors (#14362)This error is *only* emitted if `__all__` is included in the stub, so the 'if present' clause is unnecessary Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
-
Stubtest: clean up the
_belongs_to_runtime
function (#14361)There's a small semantic change in this PR (instead of returning `False` if trying to access the `__module__` attribute raises an exception, we now just move on to the next heuristic). But the main purpose of this PR is to make the code more readable, as this function was getting quite hard to understand. Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
Commits on Dec 28, 2022
-
[mypyc] Simplify union types (#14363)
We can sometimes simplify a mypyc RType union, even if the mypy union couldn't be simplified. A typical example is `list[x] | list[y]` which can be simplified to just `list`. Previously this would generate a redundant union `union[list, list]`.
-
Optimization: Avoid a few uses of contextmanagers in semantic analyzer (
#14360) This helps mypyc. (Various small optimizations, including this, together netted a 6% performance improvement in self check.)
-
Require setuptools>=65.5.1 (#14355)
Address dependabot alert about security vulnerability (https://nvd.nist.gov/vuln/detail/CVE-2022-40897).
-
Optimization: Enable always defined attributes in Type subclasses (#1…
…4356) Use lazy initialization to avoid method calls in `__init__`. This allows mypyc to infer more always defined attributes. (Various small optimizations, including this, together netted a 6% performance improvement in self check.)
-
Optimization: Remove expensive context manager in type analyzer (#14357)
This makes mypy a bit faster and the implementation seems a little cleaner as well. (Various small optimizations, including this, together netted a 6% performance improvement in self check.)
-
[undefined vars] do not double report errors in class defs (#14350)
These errors are already reported by the (new) semantic analyzer. I've discovered this while updating unit tests for new semanal in #14166. Tests are included.
-
[undefined vars] fix per-module error code override bug (#14351)
This one would occur because we set the errors module with global options, instead of per-module override ones. It only mattered for checks that happened after the partially undefined checks, which (I believe) is only the unused `type: ignore` checks. This was discovered when updating tests for #14166. I've also cleaned up the function signature a little.
-
subtypes: fast path for Union/Union subtype check (#14277)
Enums are exploded into Union of Literal when narrowed. Conditional branches on enum values can result in multiple distinct narrowing of the same enum which are later subject to subtype checks (most notably via `is_same_type`, when exiting frame context in the binder). Such checks would have quadratic complexity: `O(N*M)` where `N` and `M` are the number of entries in each narrowed enum variable, and led to drastic slowdown if any of the enums involved has a large number of values. Implement a linear-time fast path where literals are quickly filtered, with a fallback to the slow path for more complex values. In our codebase there is one method with a chain of a dozen `if` statements operating on instances of an enum with a hundreds of values. Prior to the regression it was typechecked in less than 1s. After the regression it takes over 13min to typecheck. This patch fully fixes the regression for us. Fixes #13821.
-
Micro-optimization: avoid Bogus[int] types that cause needless boxing (…
…#14354) I want to get rid of all the bogus types eventually.
Commits on Dec 26, 2022
-
Avoid slow error message logic if errors not shown to user (#14336)
This helps with await-related errors introduced in #12958, in particular, which are expensive to generate. If errors are ignored (e.g. in third-party libraries) or we don't care about the error message, use simpler error message logic. We also often filter out error messages temporarily, so any effort in constructing a nice error message is wasted. We could skip even more logic, but this should cover many of the important code paths. This speeds up self check by about 2%.
-
Speed up the implementation of hasattr() checks (#14333)
This makes the implementation of hasattr() checks faster (introduced in #13544). In particular, since the `extra_attrs` attribute used for hasattr() checks is usually None, I micro-optimized the codepaths to avoid expensive operations whenever there are no hasattr() checks. Also avoid expensive operations on simple unions and order `isinstance` checks so that common types are checked first. I measured a 2% performance uplift in self-check.
Commits on Dec 25, 2022
Commits on Dec 22, 2022
-
stubtest: Improve heuristics for determining whether global-namespace…
… names are imported (#14270) Stubtest currently has both false-positives and false-negatives when it comes to verifying constants in the global namespace of a module. This PR fixes the false positive by using `inspect.getsourcelines()` to dynamically retrieve the module source code. It then uses `symtable` to analyse that source code to gather a list of names which are known to be imported. The PR fixes the false negative by only using the `__module__` heuristic on objects which are callable. The vast majority of callable objects will be types or functions. For these objects, the `__module__` attribute will give a good indication of whether the object originates from another module or not; for other objects, it's less useful.
Commits on Dec 21, 2022
-
The docs build is currently failing on `master`: see e.g. https://github.com/python/mypy/actions/runs/3748461486/jobs/6365827914. It looks like it was broken by the release of `attrs` 22.2.0 earlier today. Specifically, it looks like python-attrs/attrs@1bb2864 broke mypy's docs build. I think this fixes things.
-
Tool to compare performance of any number of mypy commits/branches (#…
…14332) The script compiles some mypy commits in parallel and then measures how long each version takes to self-check a specific mypy commit. It measures the performance 15 times for each commit and takes the average. Based on some experiments, the noise floor on my Linux desktop is about 0.5% to 1.0%. Any difference above 1.0% is likely significant, I believe. For differences between 0.5% and 1.0% it makes sense to repeat the measurement a few times. The interesting part of the output looks something like this: ``` ... === Results === 145d8a4 8.207s (0.0%) 145d8a4~1 8.105s (-1.2%) ``` Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-
Avoid the use of a context manager in hot code path (#14331)
Mypyc can't optimize context managers yet, so it's best to avoid them in hot code paths. This sacrifices some code quality for a considerable perf gain. This improved self-check performance by 4%.
-
Refactor TypeState into a singleton class (#14327)
This helps mypyc, since accessing mutable attributes of singleton instances is faster than accessing class variables. The implementation is also arguably a bit cleaner. This seems performance-neutral or a very minor optimization, but if we continue to add attributes to TypeState, this can help.
-
Change various type queries into faster bool type queries (#14330)
I measured a 1% performance improvement in self check.
Commits on Dec 20, 2022
-
Fix RST markup in type_narrowing.rst (#14253)
Switch :py:func:type to py:class:type in type_narrowing.rst. The former does not render properly in the docs This is a tiny follow up from #14246 (comment)
-
Revert "[mypyc] Use tabs instead of spaces in emitted C code (#14016)" (
#14152) This reverts commit dbcbb3f. The indentation in generated code is now inconsistent if using 4-space tabs. We should either use tabs or spaces consistently everywhere, since we can't expect everybody to have the same tab width. The broken indentation can be seen by compiling a hello world program and opening it in an editor configured to use 4-space tabs. Since there is a lot of code in mypyc that assumes a 4-space indentation, fixing it all is probably only feasible by normalizing the indentation during the emit phase. However, the normalization step might actually slow down compilation slightly, whereas the intent of the original change to improve efficiency, so this change may ultimately be impractical. In the future we should make it possible to normalize tabs without any significant cost, but I'm not sure if that's possible right now.
-
Allow 'in' to narrow TypedDict unions (#13838)
`in` could narrow unions of TypeDicts, e.g. ```python class A(TypedDict) foo: int @Final class B(TypedDict): bar: int union: Union[A, B] = ... value: int if 'foo' in union: # Cannot be a B as it is final and has no "foo" field, so must be an A value = union['foo'] else: # Cannot be an A as those went to the if branch value = union['bar'] ```
-
Speed up recursive type check (#14326)
Use a faster type query visitor and reuse visitor across calls. This should speed up type checking slightly. My measurements show a ~0.5% improvement, but it may be below the noise floor.
-
Optimize subtype checking by avoiding a nested function (#14325)
Mypyc isn't good at compiling nested functions, and this one was in one of the hottest code paths in all of mypy. The nested function wasn't even used that often, but mypyc would still construct a closure object every time. This adds some code duplication, but it's well worth it. Amazingly, this speeds up self-check by about 10%, if my measurements are to be trusted! This addresses some of the slowdown introduced in #13303. #14324 addresses another related slowdown.
-
Optimize type parameter checks in subtype checking (#14324)
Avoid the use of a nested function, which are a bit slow when compiled with mypyc. Also avoid a callable value and instead call a function directly, which allows using faster native calls. Based on a quick experiment, this speeds up self check by about 3%. This addresses some of the slowdown introduced in #13303.
-
Speed up freshening type variables (#14323)
Only perform type variable freshening if it's needed, i.e. there is a nested generic callable, since it's fairly expensive. Make the check for generic callables fast by creating a specialized type query visitor base class for queries with bool results. The visitor tries hard to avoid memory allocation in typical cases, since allocation is slow. This addresses at least some of the performance regression in #14095. This improved self-check performance by about 3% when compiled with mypyc (-O2). The new visitor class can potentially help with other type queries as well. I'll explore it in follow-up PRs.