[ty] Avoid linear scans for parent places during semantic indexing (#30633)
## Summary
When ty records a binding for `data["group"]["key"]`, it looks up
`data["group"]` and `data` so that reassigning either one invalidates
the nested binding. The scope builder maintains hash tables for these
lookups. Passing the builders to the parent iterator implicitly
dereferenced them to the unfinished symbol and member tables, which only
receive their indexes when the scope is complete. Looking up each parent
by linear search can make indexing a large nested dictionary quadratic.
We now pass the symbol and member lookup functions into the iterator so
it can use the builder’s existing indexes as we add members.
Cold checks of pydicom 3.0.2’s entire `src/pydicom` package with its
default dependencies used 43.57–43.58% fewer instructions across two
Cachegrind pairs run in opposite orders. Adjacent runs of the base
binary differed by 0.02%. Checking its generated dictionary of private
DICOM tags alone used 70.43–70.45% fewer instructions. [pydicom 3.0.2
source](https://github.com/pydicom/pydicom/tree/20aa4b73aef76bffb6fbc46a8827588dfa9bb89d/src/pydicom)
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/a866aff503fd777156c1ae4eae318a3795fa09ec)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/1eb60753d8b0baf755665f00c09c35af5a5ccb8e)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
(inconclusive) | 3,179,241,810 | 3,173,145,286.5 | -0.19% | -0.27% to
-0.11% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| 8,251,674,998 | 8,235,873,731.5 | -0.19% | -0.24% to -0.14% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 159,255,171,567 | 158,927,725,167 | -0.21% | -0.22% to -0.20% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
(inconclusive) | 1,857,783,981.5 | 1,858,739,641 | +0.05% | -0.07% to
+0.18% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,944,207,945.5 | 1,945,917,959.5 | +0.09% | +0.03% to
+0.15% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
(inconclusive) | 28,857,584,239.5 | 28,827,980,775.5 | -0.10% | -0.18%
to -0.02% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,969,289,911.5 | 2,968,889,126.5 | -0.01% | -0.11% to
+0.08% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
(inconclusive) | 4,959,651,554.5 | 4,954,941,319 | -0.09% | -0.14% to
-0.05% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
(inconclusive) | 96,600,225,801 | 96,628,473,122.5 | +0.03% | -0.07% to
+0.13% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,682,185,663.5 | 1,681,995,005.5 | -0.01% | -0.18% to
+0.16% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,489,598,735.5 | 8,518,833,821 | +0.32% | -0.10% to
+0.75% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| 3,009,140,640 | 3,007,119,308.5 | -0.07% | -0.07% to -0.06% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 21,516,496,612.5 | 21,510,319,495 | -0.03% | -0.09% to
+0.04% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,720,493,717 | 2,721,963,315 | +0.05% | +0.02% to
+0.09% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 13,930,389,243.5 | 13,906,624,027.5 | -0.17% | -0.29%
to -0.05% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
(inconclusive) | 46,182,900,822.5 | 46,186,094,845 | +0.01% | -0.08% to
+0.10% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
(inconclusive) | 16,019,618,289.5 | 16,017,512,389.5 | -0.01% | -0.02%
to -0.01% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
(inconclusive) | 8,818,963,066.5 | 8,817,088,979 | -0.02% | -0.05% to
0.00% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| 4,175,924,456 | 4,174,358,303 | -0.04% | -0.05% to -0.03% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
(inconclusive) | 14,446,075,572 | 14,446,955,112.5 | +0.01% | -0.06% to
+0.07% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| 3,028,126,050.5 | 3,024,033,583.5 | -0.14% | -0.20% to -0.07% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,823,824,406 | 4,820,538,949.5 | -0.07% | -0.08% to
-0.05% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 34,969,653,748 | 34,945,818,318 | -0.07% | -0.13% to -0.01% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
(inconclusive) | 15,757,763,943 | 15,757,986,190.5 | 0.00% | -0.01% to
+0.02% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
(inconclusive) | 13,071,895,302 | 13,071,015,551.5 | -0.01% | -0.08% to
+0.07% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
(inconclusive) | 66,858,569,632 | 66,851,461,986 | -0.01% | -0.04% to
+0.01% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
(inconclusive) | 8,979,834,139.5 | 8,970,832,045.5 | -0.10% | -0.28% to
+0.08% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
(inconclusive) | 102,123,619,518.5 | 102,202,314,098 | +0.08% | +0.04%
to +0.12% |