[ty] Omit offset tables for singleton binding and declaration states (#30971)
## Summary
On discord.py, we built 2,289 nonempty offset tables instead of 21,652.
The offsets occupied 391 KiB in total, down from 795 KiB.
Single-threaded checks used 0.25% fewer instructions on discord.py,
0.33% fewer on Black, and 0.40% fewer on FastAPI.
We store the live bindings and declarations for a scope in contiguous
arrays. We also store an end offset for each distinct state, even when
each nonempty state has just one entry.
We now reserve ID zero for the empty state. As long as the other states
have one entry each, a state’s ID is also its end offset. When we first
see a state with multiple entries, we fill in the offsets for the
earlier states and append offsets as usual. The IDs already in the
use-def map remain valid.
<!-- ty-pinned-project-instructions:start -->
Across 28 projects, the mean change in instructions was −0.18% and the
median was −0.15%. Weighting each project by its median instruction
count on main gives a change of −0.13%.
<details>
<summary>Instruction counts by project</summary>
Both builds use the linked main commit, with this PR applied to the
second. Counts are medians of four single-worker Cachegrind runs on
Linux (x86-64). Change is the median of four paired percentages.
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/8f0189e0e975334474efb938eafc48f22855aadb)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/d01c0ab236edb3e589734934cf7076625375fc8a)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 2,522,640,457 | 2,517,284,488 | -0.21% | -0.23% to -0.20% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| 6,099,033,500 | 6,090,874,408.5 | -0.13% | -0.14% to -0.13% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 122,848,014,563 | 122,574,889,053.5 | -0.22% | -0.22% to -0.22% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 1,373,982,985.5 | 1,371,344,109.5 | -0.19% | -0.21% to -0.18% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| 1,439,135,055.5 | 1,435,558,370 | -0.25% | -0.26% to -0.24% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 21,666,673,477 | 21,641,637,180.5 | -0.12% | -0.12% to -0.11% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
| 2,490,232,305.5 | 2,482,800,289.5 | -0.30% | -0.34% to -0.22% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 4,106,343,771 | 4,090,217,554 | -0.39% | -0.40% to -0.38% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 72,483,870,921.5 | 72,429,053,572 | -0.08% | -0.08% to -0.07% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| 1,297,752,100 | 1,292,982,182.5 | -0.37% | -0.37% to -0.35% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
| 7,632,692,797.5 | 7,622,796,100 | -0.13% | -0.13% to -0.12% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| 2,408,778,430 | 2,401,147,020.5 | -0.32% | -0.32% to -0.30% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| 15,849,775,866.5 | 15,834,835,734 | -0.09% | -0.10% to -0.09% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
| 2,128,156,477.5 | 2,122,641,387 | -0.26% | -0.27% to -0.24% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| 10,444,056,781 | 10,428,966,888 | -0.14% | -0.15% to -0.14% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| 33,049,161,630.5 | 33,024,459,304 | -0.07% | -0.08% to -0.07% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 11,748,870,228.5 | 11,734,753,145.5 | -0.12% | -0.13% to -0.12% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 6,567,966,724.5 | 6,554,987,300.5 | -0.20% | -0.21% to -0.19% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| 3,244,146,768 | 3,239,108,157.5 | -0.15% | -0.16% to -0.15% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| 10,911,374,763.5 | 10,901,923,407 | -0.09% | -0.09% to -0.09% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| 2,338,072,009 | 2,331,549,802.5 | -0.28% | -0.28% to -0.28% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| 3,710,834,614 | 3,701,947,567.5 | -0.24% | -0.24% to -0.23% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 25,588,072,461.5 | 25,555,239,421.5 | -0.13% | -0.14% to -0.12% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| 11,651,042,293.5 | 11,638,993,105.5 | -0.10% | -0.11% to -0.10% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| 6,473,692,102 | 6,470,882,331.5 | -0.05% | -0.05% to -0.03% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| 39,264,219,105 | 39,249,163,436 | -0.04% | -0.04% to -0.04% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| 7,078,845,234 | 7,065,086,754.5 | -0.19% | -0.20% to -0.19% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
| 75,664,614,352 | 75,626,036,612.5 | -0.05% | -0.06% to -0.05% |
</details>
<details>
<summary>Memory by project</summary>
Peak RSS is the median paired change in MiB. We ran 16 pairs with
default parallelism and four with one worker. The last column estimates
retained query storage from two separate single-worker pairs. It
excludes some allocations.
| Project | Peak RSS (MiB) | Pairs with higher RSS | Peak RSS, one
worker (MiB) | Estimated query storage (MiB) |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| +0.16 (+0.15%) | 9/16 | +0.52 | −0.15 (−0.23%) |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| −0.49 (−0.26%) | 4/16 | −0.38 | −0.24 (−0.18%) |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| −10.68 (−0.33%) | 0/16 | −12.04 | −10.66 (−0.35%) |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| +0.97 (+1.32%) | 11/16 | +1.05 | −0.04 (−0.12%) |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| +0.29 (+0.36%) | 10/16 | +0.49 | −0.07 (−0.18%) |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| −0.91 (−0.17%) | 8/16 | −0.82 | −0.86 (−0.19%) |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
| +0.18 (+0.13%) | 9/16 | +0.11 | −0.18 (−0.21%) |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| −0.66 (−0.34%) | 1/16 | −0.48 | −0.47 (−0.33%) |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| −0.03 (0.00%) | 8/16 | −3.27 | −2.79 (−0.21%) |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| −0.05 (−0.05%) | 8/16 | −0.31 | −0.10 (−0.24%) |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
| −0.18 (−0.08%) | 7/16 | +0.11 | −0.08 (−0.04%) |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| +0.18 (+0.15%) | 9/16 | +0.25 | −0.19 (−0.27%) |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| +0.11 (+0.03%) | 9/16 | −0.13 | −0.53 (−0.18%) |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
| −0.46 (−0.45%) | 6/16 | −0.16 | −0.12 (−0.22%) |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| −0.80 (−0.29%) | 5/16 | +0.01 | −0.49 (−0.23%) |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| +0.71 (+0.11%) | 9/16 | −0.96 | −0.88 (−0.16%) |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| +0.59 (+0.20%) | 10/16 | −0.16 | −0.51 (−0.22%) |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| −0.53 (−0.25%) | 6/16 | −0.47 | −0.47 (−0.30%) |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| +0.25 (+0.21%) | 12/16 | −0.26 | −0.12 (−0.16%) |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| +0.42 (+0.16%) | 9/16 | −0.09 | −0.38 (−0.18%) |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| −0.44 (−0.40%) | 2/16 | −0.48 | −0.15 (−0.25%) |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| −0.02 (−0.01%) | 7/16 | −0.28 | −0.24 (−0.26%) |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| −0.14 (−0.02%) | 7/16 | −0.98 | −0.96 (−0.20%) |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| −0.97 (−0.37%) | 4/16 | −0.37 | −0.39 (−0.19%) |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| +0.10 (+0.08%) | 9/16 | −0.13 | −0.09 (−0.11%) |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| −1.82 (−0.32%) | 4/16 | −0.71 | −0.68 (−0.13%) |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| −0.17 (−0.08%) | 6/16 | −0.71 | −0.54 (−0.34%) |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
| −5.86 (−0.40%) | 3/16 | −3.82 | −2.97 (−0.22%) |
</details>
<!-- ty-pinned-project-instructions:end -->