[ty] Defer constructing instance types during generated member lookup (#30680)
## Summary
When ty checks whether two bases define the same member, it can ask a
Pydantic model to synthesize a name it doesn’t define. Today, even a
failed lookup constructs the model’s instance type and checks whether
its constructor parameters are keyword-only. Dify’s settings classes
inherit hundreds of fields from many bases, so most of these lookups
fail.
We now construct the instance type only if the generated member needs
it, at most once per lookup. We also wait until we build a signature
from fields to check whether the parameters are keyword-only or
optional. This doesn’t change which names ty looks up or add a
persistent cache.
A cold check of [Dify’s
`api/configs`](https://github.com/langgenius/dify/tree/63069f5f47e4a268bc7c015ae1a17c364c1ad3af/api/configs)
used 11.97–12.04% fewer instructions in two opposite-order Linux
Cachegrind pairs (2.84 billion → 2.50 billion), without #30670. The
unchanged binary varied by 0.07%. Diagnostics, exit status, and the
inferred constructor signature matched. Median native peak RSS was 110.6
MiB before and 109.4 MiB after.
## Pinned projects
The effects on these projects are much smaller than on Dify: every
paired mean was within 0.3% of the base.
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/cd2d973241d7f5bcba588e94cc686d9e356a4621)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/4871f1d3029c7c22d7a552ab49a4b2065cf11625)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
(inconclusive) | 3,162,707,394.5 | 3,161,882,664 | -0.03% | -0.06% to
+0.01% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 8,151,685,149 | 8,149,321,707 | -0.03% | -0.06% to
0.00% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 158,284,286,968 | 158,222,659,456.5 | -0.04% | -0.04% to -0.03% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
(inconclusive) | 1,853,658,011 | 1,851,473,079 | -0.12% | -0.16% to
-0.07% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,939,631,374.5 | 1,934,787,832.5 | -0.25% | -0.57% to
+0.07% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
(inconclusive) | 28,403,284,136 | 28,403,981,606.5 | 0.00% | -0.10% to
+0.10% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,951,825,878.5 | 2,958,517,322 | +0.23% | +0.12% to
+0.33% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 4,937,463,518 | 4,930,688,343 | -0.14% | -0.16% to -0.12% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
(inconclusive) | 95,740,725,103 | 95,713,077,991 | -0.03% | -0.08% to
+0.02% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,675,724,780.5 | 1,673,777,619 | -0.12% | -0.16% to
-0.08% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,450,176,454.5 | 8,446,719,849 | -0.04% | -0.16% to
+0.08% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
(inconclusive) | 2,980,980,592.5 | 2,980,883,516.5 | 0.00% | -0.01% to
0.00% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 21,251,919,640 | 21,253,134,098.5 | +0.01% | -0.04% to
+0.05% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,713,894,868.5 | 2,710,753,553.5 | -0.12% | -0.21% to
-0.02% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 13,804,672,189.5 | 13,776,418,804 | -0.20% | -0.34% to
-0.07% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
(inconclusive) | 45,793,709,206 | 45,848,344,313.5 | +0.12% | +0.11% to
+0.13% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 15,877,984,455 | 15,832,607,795 | -0.29% | -0.32% to -0.25% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
(inconclusive) | 8,770,299,509 | 8,774,790,034 | +0.05% | -0.03% to
+0.13% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
(inconclusive) | 4,152,232,782 | 4,152,105,992.5 | 0.00% | 0.00% to
0.00% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
(inconclusive) | 14,335,497,724.5 | 14,329,548,330 | -0.04% | -0.05% to
-0.03% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
(inconclusive) | 3,003,426,508.5 | 3,005,378,187 | +0.07% | -0.03% to
+0.16% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,770,949,075.5 | 4,775,600,672.5 | +0.10% | +0.05% to
+0.14% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
(inconclusive) | 33,753,164,206.5 | 33,776,883,681.5 | +0.07% | +0.04%
to +0.10% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
(inconclusive) | 15,578,709,580 | 15,579,611,045.5 | +0.01% | -0.04% to
+0.05% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
(inconclusive) | 12,994,469,050 | 12,993,721,842 | -0.01% | -0.05% to
+0.04% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| 61,273,444,829.5 | 61,309,796,338.5 | +0.06% | +0.03% to +0.08% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| 8,941,200,624 | 8,925,929,001 | -0.17% | -0.22% to -0.12% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
(inconclusive) | 99,772,962,544 | 99,779,316,536.5 | +0.01% | -0.03% to
+0.05% |