From e0df471954da6fe08f6f4f090f8196c0e548bef2 Mon Sep 17 00:00:00 2001 From: Shantanu Date: Mon, 26 Oct 2026 05:55:19 +0000 Subject: [PATCH] Skip the symbol table scan for missing methods TypeInfo.get_method scanned every name in each MRO entry when a method was absent. On colour, native sampling attributed 21% of the baseline profile to this lookup. The semantic analyzer inserts the unnumbered "name-redefinition" before any numbered redefinitions. Check for that key first. When it exists, select the same lexicographically last definition as before. This keeps the fallback that dataclass InitVars need. Both binaries checked the same source trees cold. The baseline was b286c11c49ef. input median before -> after paired wall reduction self 6.227 s -> 6.169 s 0.84% torch 3.251 s -> 3.278 s -0.11% pandas 24.372 s -> 23.622 s 2.82% colour 38.643 s -> 29.050 s 24.69% jax 29.032 s -> 27.883 s 3.39% zulip 14.505 s -> 14.154 s 2.19% The torch interval spans zero (-0.75% to +0.47%). Each pair ran both Clang 18 release binaries on the same CPU set, with an empty mypy cache. The order alternated over 12 pairs for self, 14 for torch 2.1.0, and eight per project from mypy_primer 9889850. The last column averages within-pair wall time ratios. All pairs produced byte-identical diagnostics. The checker, fine-grained incremental, and dependency suites passed (9,134 tests). An independent multifile mypyc build reduced colour wall time by 26.01% across six pairs (95% interval 25.66% to 26.67%). Its self result was inconclusive (0.20%, interval -0.64% to 1.15%). A common import path and common typeshed files gave a 0.60% self improvement across 14 pairs with the single-unit build. --- mypy/nodes.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mypy/nodes.py b/mypy/nodes.py index 20f7a0df804c4..edf8a5c5c7f76 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -4057,14 +4057,14 @@ def has_readable_member(self, name: str) -> bool: def get_method(self, name: str) -> FuncBase | Decorator | None: for cls in self.mro: - if name in cls.names: - node = cls.names[name].node - elif possible_redefinitions := sorted( - [n for n in cls.names.keys() if n.startswith(f"{name}-redefinition")] - ): - node = cls.names[possible_redefinitions[-1]].node - else: - continue + sym = cls.names.get(name) + if sym is None: + # add_redefinition installs the unnumbered name before any numbered ones. + prefix = f"{name}-redefinition" + if prefix not in cls.names: + continue + sym = cls.names[max(n for n in cls.names if n.startswith(prefix))] + node = sym.node if isinstance(node, SYMBOL_FUNCBASE_TYPES): return node elif isinstance(node, Decorator): # Two `if`s make `mypyc` happy