[ty] Skip union comparisons between methods of distinct functions (#30721)
## Summary
Telethon looks up `clazz.from_reader` on a union of more than 2,000
classes. To build the union of bound methods, ty caches millions of
comparisons that fail as soon as we check the underlying functions.
We now check function identity in the union builder. Methods backed by
distinct function literals expose different `__func__` values even if
their signatures match. We still visit each pair and use the existing
checks for methods that share a function.
Cold checks of all 150 Python files in [Telethon
1.42.0](https://pypi.org/project/Telethon/1.42.0/) used 50.85–50.94%
fewer Cachegrind instructions. Median peak RSS fell from 1,776 to 487
MiB and the number of interned type pairs fell from 5,634,936 to
395,254.
On all 156 files in [Telethon
1.45.0](https://pypi.org/project/Telethon/1.45.0/), instructions fell by
51.93–52.00% and median peak RSS fell from 2,106 to 546 MiB.
<!-- ty-pinned-project-instructions:start -->
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/943f623f55c9aa625b0b1b21af731e451819accc)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/8d03b8535c23330cded93a3b4300cdb427ca8f95)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
(inconclusive) | 3,114,464,876.5 | 3,110,414,506 | -0.13% | -0.29% to
+0.03% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 7,987,516,040 | 7,993,539,938.5 | +0.08% | -0.19% to
+0.34% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 153,590,539,496 | 153,638,289,742 | +0.03% | +0.02% to +0.04% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
(inconclusive) | 1,816,340,726 | 1,816,898,305.5 | +0.03% | -0.04% to
+0.10% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,899,232,704.5 | 1,901,946,299 | +0.14% | +0.09% to
+0.20% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
(inconclusive) | 27,648,069,329.5 | 27,633,817,015.5 | -0.05% | -0.10%
to 0.00% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,875,891,658 | 2,874,361,007 | -0.05% | -0.18% to
+0.08% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
(inconclusive) | 4,866,071,962.5 | 4,868,306,269 | +0.05% | +0.02% to
+0.08% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
(inconclusive) | 93,380,078,394 | 93,453,373,652 | +0.08% | -0.05% to
+0.21% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| 1,651,220,747 | 1,654,539,098.5 | +0.20% | +0.20% to +0.20% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,216,717,078.5 | 8,229,302,158 | +0.15% | +0.09% to
+0.22% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
(inconclusive) | 2,945,954,611 | 2,947,488,094 | +0.05% | +0.03% to
+0.08% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 20,741,429,829.5 | 20,744,124,996.5 | +0.01% | +0.01%
to +0.02% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,679,182,569.5 | 2,677,272,692.5 | -0.07% | -0.48% to
+0.34% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 13,418,704,481.5 | 13,430,801,584 | +0.09% | +0.02% to
+0.17% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
(inconclusive) | 44,750,288,715.5 | 44,789,077,339.5 | +0.09% | +0.02%
to +0.15% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 15,434,514,718 | 15,457,492,616.5 | +0.15% | +0.15% to +0.15% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 8,591,821,941 | 8,604,767,453 | +0.15% | +0.10% to +0.20% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
(inconclusive) | 4,070,623,526.5 | 4,066,976,082.5 | -0.09% | -0.13% to
-0.04% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
(inconclusive) | 14,015,160,770.5 | 14,026,286,697.5 | +0.08% | 0.00% to
+0.15% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
(inconclusive) | 2,935,279,055 | 2,934,176,450.5 | -0.04% | -0.07% to
-0.01% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,651,480,837 | 4,653,743,507 | +0.05% | +0.04% to
+0.06% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
(inconclusive) | 32,512,163,503.5 | 32,514,936,889 | +0.01% | 0.00% to
+0.01% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
(inconclusive) | 15,213,287,215.5 | 15,219,151,727 | +0.04% | -0.03% to
+0.10% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| 12,884,688,971.5 | 12,909,835,715 | +0.20% | +0.16% to +0.23% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| 58,217,509,201 | 58,254,050,951 | +0.06% | +0.04% to +0.08% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
(inconclusive) | 8,737,403,792 | 8,728,786,127.5 | -0.10% | -0.16% to
-0.04% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
(inconclusive) | 96,483,800,171.5 | 96,522,588,263.5 | +0.04% | +0.01%
to +0.07% |
<!-- ty-pinned-project-instructions:end -->