Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ty] Avoid redundant constraint saturation work#25786

Merged
charliermarsh merged 1 commit into
mainfrom
charlie/optimize-constraint-saturation
Jun 10, 2026
Merged

[ty] Avoid redundant constraint saturation work#25786
charliermarsh merged 1 commit into
mainfrom
charlie/optimize-constraint-saturation

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jun 9, 2026

Copy link
Copy Markdown
Member

Summary

The constraint-set subtype checks introduced in #25778 can be repeated many times while saturating pairs of constraints. On pytest-robotframework, that increased median check time from 105.8 ms on main to 159.9 ms with #25778.

This caches those checks for the lifetime of a constraint-set builder.

As a separate, semantics-preserving optimization, this also skips calling add_nested_typevar_sequents when neither constraint has a type variable in its bounds. In that case, the method cannot derive any sequents: all of its variance checks return Bivariant. The precheck visits lazy types to match the traversal performed by variance_of.

With this change, pytest-robotframework takes 108.5 ms, 2.6% above main, with byte-for-byte identical diagnostics. The full ty_python_semantic test suite, focused Clippy, and repository hooks pass.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Jun 9, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 94.36%. The percentage of expected errors that received a diagnostic held steady at 88.82%. The number of fully passing files held steady at 93/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

Memory usage report

Summary

Project Old New Diff Outcome
flake8 35.27MB 35.25MB -0.06% (21.00kB) ⬇️
trio 87.18MB 87.14MB -0.05% (41.05kB) ⬇️
sphinx 206.04MB 205.65MB -0.19% (403.25kB) ⬇️
prefect 558.61MB 557.09MB -0.27% (1.51MB) ⬇️

Significant changes

Click to expand detailed breakdown

flake8

Name Old New Diff Outcome
StaticClassLiteral<'db>::variance_of_::interned_arguments 11.67kB 5.70kB -51.20% (5.98kB) ⬇️
Type<'db>::is_constraint_set_subtype_of_::interned_arguments 0.00B 5.16kB +5.16kB (new) ⬇️
when_constraint_set_assignable_to_owned_impl 157.59kB 152.55kB -3.20% (5.04kB) ⬇️
StaticClassLiteral<'db>::variance_of_ 9.19kB 4.52kB -50.77% (4.66kB) ⬇️
Type<'db>::is_constraint_set_subtype_of_ 0.00B 4.45kB +4.45kB (new) ⬇️
is_possibly_constraint_set_assignable 28.17kB 24.80kB -11.98% (3.38kB) ⬇️
infer_definition_types 1.53MB 1.52MB -0.20% (3.12kB) ⬇️
infer_expression_types_impl 969.27kB 967.22kB -0.21% (2.05kB) ⬇️
GenericAlias<'db>::variance_of_ 3.49kB 1.62kB -53.69% (1.88kB) ⬇️
GenericAlias<'db>::variance_of_::interned_arguments 3.23kB 1.48kB -54.35% (1.76kB) ⬇️
infer_scope_types_impl 781.87kB 780.45kB -0.18% (1.42kB) ⬇️
assignable_solutions_impl 18.12kB 16.77kB -7.50% (1.36kB) ⬇️
infer_statement_types_impl 39.82kB 40.12kB +0.77% (312.00B) ⬇️
function_known_decorators 143.05kB 142.89kB -0.11% (168.00B) ⬇️
try_call_bin_op_return_type_impl 6.21kB 6.09kB -1.89% (120.00B) ⬇️

trio

Name Old New Diff Outcome
Type<'db>::is_constraint_set_subtype_of_::interned_arguments 0.00B 14.95kB +14.95kB (new) ⬇️
Type<'db>::is_constraint_set_subtype_of_ 0.00B 12.98kB +12.98kB (new) ⬇️
StaticClassLiteral<'db>::variance_of_::interned_arguments 26.37kB 14.62kB -44.53% (11.74kB) ⬇️
when_constraint_set_assignable_to_owned_impl 399.62kB 388.85kB -2.69% (10.77kB) ⬇️
is_possibly_constraint_set_assignable 88.48kB 77.87kB -11.99% (10.61kB) ⬇️
GenericAlias<'db>::variance_of_ 16.27kB 6.14kB -62.25% (10.12kB) ⬇️
StaticClassLiteral<'db>::variance_of_ 20.31kB 11.19kB -44.89% (9.12kB) ⬇️
infer_definition_types 6.41MB 6.40MB -0.14% (8.95kB) ⬇️
GenericAlias<'db>::variance_of_::interned_arguments 15.54kB 6.96kB -55.20% (8.58kB) ⬇️
infer_expression_types_impl 6.50MB 6.50MB -0.06% (4.24kB) ⬇️
all_narrowing_constraints_for_expression 1007.28kB 1010.35kB +0.30% (3.07kB) ⬇️
infer_expression_type_impl 59.13kB 61.05kB +3.24% (1.91kB) ⬇️
assignable_solutions_impl 44.16kB 42.27kB -4.27% (1.89kB) ⬇️
try_call_bin_op_return_type_impl 45.94kB 45.12kB -1.79% (840.00B) ⬇️
infer_scope_types_impl 3.74MB 3.74MB -0.02% (828.00B) ⬇️
... 18 more

sphinx

Name Old New Diff Outcome
infer_expression_types_impl 20.34MB 20.25MB -0.46% (95.21kB) ⬇️
infer_definition_types 20.15MB 20.07MB -0.39% (79.95kB) ⬇️
when_constraint_set_assignable_to_owned_impl 1.48MB 1.43MB -3.81% (57.94kB) ⬇️
Type<'db>::is_constraint_set_subtype_of_::interned_arguments 0.00B 51.30kB +51.30kB (new) ⬇️
Type<'db>::is_constraint_set_subtype_of_ 0.00B 48.76kB +48.76kB (new) ⬇️
is_possibly_constraint_set_assignable 262.71kB 223.35kB -14.98% (39.36kB) ⬇️
GenericAlias<'db>::variance_of_ 49.03kB 12.96kB -73.57% (36.07kB) ⬇️
function_known_decorators 963.80kB 930.21kB -3.49% (33.60kB) ⬇️
GenericAlias<'db>::variance_of_::interned_arguments 44.65kB 13.85kB -68.98% (30.80kB) ⬇️
StaticClassLiteral<'db>::variance_of_::interned_arguments 46.27kB 16.52kB -64.29% (29.74kB) ⬇️
infer_deferred_types 4.00MB 3.98MB -0.64% (26.05kB) ⬇️
StaticClassLiteral<'db>::variance_of_ 39.30kB 15.02kB -61.78% (24.28kB) ⬇️
assignable_solutions_impl 187.64kB 167.62kB -10.67% (20.03kB) ⬇️
infer_scope_types_impl 12.47MB 12.45MB -0.14% (17.73kB) ⬇️
try_call_bin_op_return_type_impl 201.02kB 195.75kB -2.62% (5.27kB) ⬇️
... 24 more

prefect

Name Old New Diff Outcome
infer_definition_types 73.75MB 73.10MB -0.88% (666.02kB) ⬇️
infer_expression_types_impl 54.84MB 54.39MB -0.82% (462.91kB) ⬇️
infer_scope_types_impl 47.30MB 47.14MB -0.34% (163.90kB) ⬇️
Type<'db>::is_constraint_set_subtype_of_::interned_arguments 0.00B 116.10kB +116.10kB (new) ⬇️
GenericAlias<'db>::variance_of_ 136.20kB 24.45kB -82.05% (111.75kB) ⬇️
Type<'db>::is_constraint_set_subtype_of_ 0.00B 102.30kB +102.30kB (new) ⬇️
GenericAlias<'db>::variance_of_::interned_arguments 116.79kB 28.05kB -75.98% (88.73kB) ⬇️
when_constraint_set_assignable_to_owned_impl 3.00MB 2.93MB -2.31% (70.72kB) ⬇️
is_possibly_constraint_set_assignable 593.10kB 535.98kB -9.63% (57.12kB) ⬇️
StaticClassLiteral<'db>::variance_of_::interned_arguments 90.00kB 38.11kB -57.66% (51.89kB) ⬇️
StaticClassLiteral<'db>::variance_of_ 79.14kB 37.24kB -52.95% (41.91kB) ⬇️
function_known_decorators 3.90MB 3.88MB -0.69% (27.79kB) ⬇️
assignable_solutions_impl 319.45kB 296.09kB -7.31% (23.36kB) ⬇️
infer_statement_types_impl 875.64kB 898.22kB +2.58% (22.58kB) ⬇️
infer_deferred_types 8.98MB 8.97MB -0.14% (12.73kB) ⬇️
... 26 more

@astral-sh-bot

astral-sh-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

@charliermarsh charliermarsh added the performance Potential performance improvement label Jun 9, 2026
@charliermarsh charliermarsh force-pushed the charlie/fix-dynamic-constraint-explosion branch from 28d7fdb to be0c908 Compare June 9, 2026 16:54
@charliermarsh charliermarsh force-pushed the charlie/optimize-constraint-saturation branch from c433edc to 294e3f1 Compare June 9, 2026 16:56
@charliermarsh charliermarsh marked this pull request as ready for review June 9, 2026 16:56
Comment thread crates/ty_python_semantic/src/types/constraints.rs Outdated
Comment thread crates/ty_python_semantic/src/types/constraints.rs Outdated
@charliermarsh charliermarsh force-pushed the charlie/fix-dynamic-constraint-explosion branch from be0c908 to ab9db7e Compare June 9, 2026 20:01
@charliermarsh charliermarsh force-pushed the charlie/optimize-constraint-saturation branch from 294e3f1 to d249349 Compare June 9, 2026 20:16

@dcreager dcreager left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💥

@charliermarsh charliermarsh force-pushed the charlie/fix-dynamic-constraint-explosion branch from ab9db7e to 6fd42b6 Compare June 10, 2026 20:13
Base automatically changed from charlie/fix-dynamic-constraint-explosion to main June 10, 2026 20:23
@charliermarsh charliermarsh force-pushed the charlie/optimize-constraint-saturation branch from d249349 to a9a2a50 Compare June 10, 2026 20:23
@AlexWaygood AlexWaygood removed their request for review June 10, 2026 20:24
@charliermarsh charliermarsh merged commit 8f2caa6 into main Jun 10, 2026
58 checks passed
@charliermarsh charliermarsh deleted the charlie/optimize-constraint-saturation branch June 10, 2026 20:37
charliermarsh added a commit that referenced this pull request Jun 10, 2026
## Summary

PR #25786 originally cached repeated constraint-set subtype checks on
`ConstraintSetBuilder`. [During
review](#25786 (comment)),
that cache was replaced with a Salsa-tracked query to allow reuse across
builders. On pytest-robotframework, the tracked query does not recover
the same performance: current main remains roughly 25% slower than the
commit immediately before #25778.

This restores the builder-local `FxHashMap` cache and routes the two
transitive-pivot subtype checks through it. The separate
type-variable-bound precheck from #25786 remains unchanged.

In 80 balanced, interleaved runs of pytest-robotframework using clean
profiling builds, the results were:

| Revision | Median | Paired change vs. pre-#25778 |
| --- | ---: | ---: |
| Pre-#25778 (`1676ec8013`) | 103.2 ms | — |
| Current main (`8f2caa6a04`) | 128.3 ms | +24.9% |
| This PR | 106.9 ms | +3.7% |

This PR is 16.9% faster than current main. All three revisions produced
byte-for-byte identical diagnostics. The full `ty_python_semantic` suite
(558 tests), focused Clippy, and repository hooks pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Potential performance improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants