[ty] Skip narrowing alias scans for unrelated assignments (#30637)
## Summary
When ty indexes `flag = check(x)`, it records that `if flag` might
narrow `x`, since `check` could be a type guard. Each subsequent
assignment scans all such aliases in the scope to find the ones it
invalidates. This takes quadratic work in generated code like Pycrate’s
[`S1AP_IEs`
class](https://github.com/pycrate-org/pycrate/blob/7f5590f4f03a029c6699bb6dad28a7ada55d9929/pycrate_asn1dir/S1AP.py#L5024),
where 2,591 assignments pass a named constant to a constructor’s `mode`
parameter.
We now keep a set of the places that aliases in the scope could narrow.
We remove a reassigned alias by name and scan the others only if the
assigned place or one of its members appears in the set. Places can stay
in the set after their aliases expire, which can trigger an extra scan.
Cold checks under Cachegrind used 30.59–30.65% fewer instructions on
[Pycrate’s S1AP
test](https://github.com/pycrate-org/pycrate/blob/7f5590f4f03a029c6699bb6dad28a7ada55d9929/test/test_s1ap.py),
21.74–21.83% fewer on its [test
directory](https://github.com/pycrate-org/pycrate/tree/7f5590f4f03a029c6699bb6dad28a7ada55d9929/test),
and 23.43% fewer on [tinygrad’s AMD LLVM
test](https://github.com/tinygrad/tinygrad/blob/6f87158d77f66a36d5f8bbe915170b24e2acabe8/test/amd/test_llvm.py).
The ranges cover opposite run orders. Consecutive runs of the same
binary varied by less than 0.09%.
Across [all 824 Python files in
Pycrate](https://github.com/pycrate-org/pycrate/tree/7f5590f4f03a029c6699bb6dad28a7ada55d9929),
DynamoRIO counted 8.71–8.73% fewer application instructions across all
threads, with 0.012% variation between consecutive baseline runs. These
checks used an empty Python 3.12.16 environment. Native diagnostics and
exit codes matched on all four workloads.
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/a866aff503fd777156c1ae4eae318a3795fa09ec)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/a6ed0352ac22b57aa07810116d4372e8f0ec2c2e)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 3,172,514,062 | 3,169,449,416.5 | -0.10% | -0.12% to -0.08% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 8,243,857,102 | 8,251,685,538 | +0.10% | -0.12% to
+0.31% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
(inconclusive) | 159,262,345,008 | 159,238,769,327.5 | -0.01% | -0.03%
to 0.00% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
(inconclusive) | 1,860,855,750 | 1,859,816,634 | -0.06% | -0.13% to
+0.01% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| 1,945,562,312.5 | 1,942,842,617.5 | -0.14% | -0.18% to -0.10% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 28,816,874,186 | 28,834,699,505.5 | +0.06% | +0.01% to +0.11% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,973,537,912 | 2,965,507,141 | -0.27% | -0.66% to
+0.12% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
(inconclusive) | 4,961,099,552 | 4,956,571,899 | -0.09% | -0.11% to
-0.08% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
(inconclusive) | 96,659,865,188.5 | 96,632,753,940.5 | -0.03% | -0.08%
to +0.02% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,680,932,144.5 | 1,682,236,461 | +0.08% | +0.08% to
+0.08% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,483,841,428.5 | 8,481,733,436.5 | -0.02% | -0.18% to
+0.14% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| 3,012,146,177.5 | 3,007,171,518 | -0.17% | -0.26% to -0.07% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 21,532,966,384.5 | 21,497,492,098 | -0.16% | -0.30% to
-0.03% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,720,409,075.5 | 2,717,900,746 | -0.09% | -0.28% to
+0.10% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 13,907,020,196 | 13,905,107,844 | -0.01% | -0.17% to
+0.14% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| 46,159,144,129.5 | 46,172,655,846 | +0.03% | +0.02% to +0.04% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 16,025,875,168.5 | 16,012,416,670.5 | -0.08% | -0.12% to -0.05% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
(inconclusive) | 8,818,346,075 | 8,801,999,833 | -0.19% | -0.29% to
-0.08% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| 4,177,678,533 | 4,171,606,172 | -0.15% | -0.16% to -0.13% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
(inconclusive) | 14,457,339,364.5 | 14,453,377,916 | -0.03% | -0.09% to
+0.03% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
(inconclusive) | 3,024,271,752 | 3,023,640,917 | -0.02% | -0.04% to
0.00% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,818,652,173.5 | 4,820,367,552.5 | +0.04% | -0.03% to
+0.10% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
(inconclusive) | 34,984,499,788 | 34,968,252,921.5 | -0.05% | -0.09% to
0.00% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
(inconclusive) | 15,753,703,965 | 15,753,541,562 | 0.00% | -0.01% to
+0.01% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
(inconclusive) | 13,069,930,420.5 | 13,071,507,284 | +0.01% | 0.00% to
+0.02% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
(inconclusive) | 66,895,354,675 | 66,877,054,277.5 | -0.03% | -0.05% to
-0.01% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
(inconclusive) | 8,982,976,001 | 8,981,618,190 | -0.02% | -0.02% to
-0.01% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
(inconclusive) | 102,113,433,441 | 102,130,884,935 | +0.02% | -0.05% to
+0.08% |