Avoid copying entire mappings during type inference

Merged
AL
1d ago
+57-3

Mapping type inference samples only 100 keys and values, but currently materializes every key and value before taking that sample. Use bounded iteration so inference keeps the same sample and resulting types without copying or retrieving the rest of a large mapping.

On a local CPython 3.14.5 / Apple M5 Pro benchmark, inference for a one-million-entry dict fell from 5.62 ms to 0.026 ms, with peak traced temporary allocation falling from 8 MB to 360 B. This measures the inference helper, not whole-app compilation. Added a CodSpeed benchmark across mapping sizes and regressions covering empty mappings, the exact sample boundary, and custom Mapping iteration/value access. The bounded-iteration regression fails on the unchanged base.

Validation:

  • Full unit suite: 8,303 passed, 18 skipped; 75.95% coverage.
  • Focused Var tests and new benchmark: 260 passed.
  • Repository-wide Ruff check and format check passed.
  • pyright reflex tests and changed-file type checks passed.

Independent of the other performance changes; based directly on main.