[ty] Cache protocol receiver binding
Merged
CA
3mo ago+59+59+01234567890123456789-4-4-0123456789
Summary
Protocol-member compatibility repeatedly specializes the same callable for the same runtime receiver and typing Self, rebuilding signatures and constraint sets on each structural comparison.
- Add a private Salsa-tracked protocol receiver-binding cache keyed by the callable, receiver type, and
Selftype. - Route all four existing protocol compatibility binding sites through the cache, including protocol-to-protocol member comparisons.
- Preserve distinct receiver and
Selfbindings and reuseapply_selfwhen they are identical.
This is independently useful on main and becomes even more relevant with #27267.
Performance
DateType on clean main and this PR, with 40 samples per version:
| Version | Estimate | 95% confidence interval |
|---|---|---|
| Baseline | 38.701 ms | 38.336–39.125 ms |
| Protocol receiver cache with fixed-point recovery | 33.887 ms | 33.742–34.033 ms |
The change is 12.4% faster, with non-overlapping confidence intervals.
Test plan
- Add a minimized mdtest for a generic protocol whose receiver-specific method and classmethod produce a recursive receiver-binding query; verify that the expected return-type diagnostic is reported instead of a Salsa panic.
- Reproduce the exact ecosystem-analyzer configuration for DateType, scipy-stubs, and trio, and verify that all three projects produce byte-for-byte identical diagnostics to
main.