[ty] Reuse overload prefix checks across calls (#30640)
## Summary
When an argument of type `Any` matches several overloads, ty checks
whether the first overload, then the first two, and so on, accept every
materialization of the arguments. Each step builds a union of parameter
types. Calls to the same overloaded function can repeat the entire
search with identical types.
We now reuse the number of overloads to keep when the inferable type
variables and the argument and parameter types at the distinguishing
positions agree. We still compare the remaining return types at each
call site and evaluate the search directly during cycle recovery.
## Performance
After a full check of scikit-learn, the cache holds 4.01 MiB of keys,
results and Salsa metadata. With one worker, total live Salsa storage
grows 0.97% with the frozen inputs used by the CLI and falls 2.67% with
mutable inputs.
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/a866aff503fd777156c1ae4eae318a3795fa09ec)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/ce4906c7203f761ff6b2b2748b3cbf1af653388e)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 3,172,849,958.5 | 3,171,504,084 | -0.04% | -0.06% to -0.03% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| 8,244,551,270 | 8,225,108,318.5 | -0.24% | -0.26% to -0.21% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 159,255,076,114 | 159,160,797,237 | -0.06% | -0.07% to -0.05% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 1,860,603,867.5 | 1,858,126,810 | -0.13% | -0.21% to -0.06% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,946,524,045.5 | 1,943,888,989.5 | -0.14% | -0.14% to
-0.13% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 28,831,258,280.5 | 28,495,153,924 | -1.17% | -1.26% to -1.08% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,971,313,111 | 2,972,442,653 | +0.04% | -0.26% to
+0.33% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 4,957,712,369.5 | 4,952,626,259.5 | -0.10% | -0.11% to -0.09% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 96,639,048,118.5 | 96,414,268,431.5 | -0.23% | -0.26% to -0.21% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,683,810,371.5 | 1,682,014,111 | -0.11% | -0.13% to
-0.08% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,497,337,652.5 | 8,480,204,308.5 | -0.20% | -0.32% to
-0.09% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
(inconclusive) | 3,008,218,552 | 3,004,472,580.5 | -0.12% | -0.22% to
-0.03% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| 21,521,362,036.5 | 21,427,512,841 | -0.44% | -0.48% to -0.39% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,723,800,499.5 | 2,717,501,353.5 | -0.23% | -0.37% to
-0.09% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| 13,912,015,847 | 13,883,110,045 | -0.21% | -0.28% to -0.13% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| 46,177,370,870 | 45,967,496,305.5 | -0.45% | -0.45% to -0.45% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
(inconclusive) | 16,023,139,660 | 16,002,107,945.5 | -0.13% | -0.24% to
-0.03% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 8,814,471,520.5 | 8,798,972,122.5 | -0.18% | -0.27% to -0.08% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
(inconclusive) | 4,175,149,063.5 | 4,172,710,283.5 | -0.06% | -0.15% to
+0.04% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| 14,442,791,334.5 | 14,401,352,824 | -0.29% | -0.32% to -0.25% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| 3,026,052,345 | 3,023,319,766 | -0.09% | -0.10% to -0.08% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| 4,823,425,865.5 | 4,812,542,246 | -0.23% | -0.29% to -0.16% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 34,967,356,287.5 | 33,935,527,972 | -2.95% | -2.98% to -2.93% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| 15,760,776,050.5 | 15,624,434,259.5 | -0.87% | -0.87% to -0.86% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| 13,064,494,398 | 13,039,730,470.5 | -0.19% | -0.19% to -0.19% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| 66,902,103,018 | 62,012,792,620 | -7.31% | -7.39% to -7.22% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| 8,974,801,335.5 | 8,962,767,833.5 | -0.13% | -0.17% to -0.10% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
| 102,173,739,178.5 | 101,044,134,037.5 | -1.11% | -1.21% to -1.01% |