Skip to content

Avoid unnecessary setattr lookups - #22270

Merged
hauntsaninja merged 2 commits into
python:masterfrom
hauntsaninja:protcolsetattr
Oct 29, 2026
Merged

hauntsaninja merged 2 commits into
python:masterfrom
hauntsaninja:protcolsetattr

Conversation

@hauntsaninja

@hauntsaninja hauntsaninja commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

This makes mypy 13% faster on the colour-science/colour codebase. Codex claims it's worth 0.8% on self check too.

Co-authored by Codex

This makes mypy 10% faster on the colour-science/colour codebase
@github-actions

This comment has been minimized.

@hauntsaninja

hauntsaninja commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator Author

Hmm that's a little annoying. I guess I could just land the optimisation and punt on the "correctness" issue

@ilevkivskyi

Copy link
Copy Markdown
Member

Btw I can confirm this also affects some other protocol-heavy libraries, let's just focus on the optimization part.

@ilevkivskyi

Copy link
Copy Markdown
Member

Btw a (much) better performance fix is to delete all the bizarre logic from get_method() added in #17219 That PR added a lot of slow ops to a very hot code path to fix a niche edge case (also I am not sure that PR is actually the correct solution).

@hauntsaninja

Copy link
Copy Markdown
Collaborator Author

Yeah I was looking at that redefinition one too. Will definitely at least put up an optimisation PR for it

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@hauntsaninja hauntsaninja changed the title Avoid unnecessary setattr lookups, ignore object.__setattr__ Avoid unnecessary setattr lookups Oct 29, 2026
@hauntsaninja
hauntsaninja merged commit b3ab06a into python:master Oct 29, 2026
25 checks passed
@hauntsaninja
hauntsaninja deleted the protcolsetattr branch October 29, 2026 19:47
@JukkaL

JukkaL commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

An excellent perf win with a tiny PR!

JukkaL pushed a commit that referenced this pull request Nov 2, 2026
Authored by Codex, here's its description below.

Note that its baseline doesn't include
#22270 so it probably overstates the
win a little

```
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 b286c11.

    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.
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants