[ty] Cache protocol receiver binding

Merged
CA
3mo ago
+59-4

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 Self type.
  • Route all four existing protocol compatibility binding sites through the cache, including protocol-to-protocol member comparisons.
  • Preserve distinct receiver and Self bindings and reuse apply_self when 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:

VersionEstimate95% confidence interval
Baseline38.701 ms38.336–39.125 ms
Protocol receiver cache with fixed-point recovery33.887 ms33.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.