Skip to content

[ty] Avoid quadratic enum comparison narrowing#25789

Draft
charliermarsh wants to merge 8 commits into
charlie/revive-equality-narrowingfrom
charlie/revive-equality-narrowing-enum-performance
Draft

[ty] Avoid quadratic enum comparison narrowing#25789
charliermarsh wants to merge 8 commits into
charlie/revive-equality-narrowingfrom
charlie/revive-equality-narrowing-enum-performance

Conversation

@charliermarsh

Copy link
Copy Markdown
Member

Summary

Stacks on #25788.

The equality evaluator introduced in #25788 expands enum types into member literals before narrowing. When both operands contain enum types, recursively expanding one side causes the other side to be expanded and compared again for every member, making == and != quadratic. Mixed enum and open-ended unions can trigger the same repeated work without producing useful narrowing.

This expands each finite operand once, then compares the resulting finite sets directly. It has fast paths for identical and disjoint sets, and it avoids expansion when the other operand includes an open-ended union arm. Narrowing with an identity singleton such as None still works.

The regression coverage compares same-enum, optional-enum, and different-enum operands with 250-member enums and enforces a 10-second upper bound.

@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 92.23%. The percentage of expected errors that received a diagnostic held steady at 87.42%. The number of fully passing files held steady at 92/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
trio 87.68MB 87.69MB +0.01% (8.83kB)
flake8 35.38MB 35.38MB -
sphinx 207.42MB 207.41MB -0.00% (4.45kB) ⬇️
prefect 563.24MB 563.22MB -0.00% (13.89kB) ⬇️

Significant changes

Click to expand detailed breakdown

trio

Name Old New Diff Outcome
infer_definition_types 6.61MB 6.61MB +0.03% (2.00kB)
is_redundant_with_impl::interned_arguments 225.76kB 227.13kB +0.61% (1.38kB)
infer_expression_types_impl 6.58MB 6.58MB +0.02% (1.36kB)
loop_header_reachability 126.57kB 127.79kB +0.96% (1.22kB)
IntersectionType 141.10kB 142.31kB +0.86% (1.21kB)
all_narrowing_constraints_for_expression 963.37kB 964.21kB +0.09% (864.00B)
is_redundant_with_impl 182.45kB 183.20kB +0.41% (768.00B)
UnionType 150.56kB 150.83kB +0.18% (272.00B)
IntersectionType<'db>::from_two_elements_::interned_arguments 13.15kB 13.06kB -0.65% (88.00B)
place_table 79.10kB 79.04kB -0.07% (60.00B)
IntersectionType<'db>::from_two_elements_ 16.83kB 16.77kB -0.32% (56.00B)

sphinx

Name Old New Diff Outcome
all_narrowing_constraints_for_expression 3.92MB 3.92MB -0.08% (3.09kB) ⬇️
IntersectionType<'db>::from_two_elements_ 54.06kB 53.74kB -0.59% (324.00B) ⬇️
member_lookup_with_policy_inner 5.64MB 5.64MB -0.00% (268.00B) ⬇️
IntersectionType<'db>::from_two_elements_::interned_arguments 58.78kB 58.52kB -0.44% (264.00B) ⬇️
Type<'db>::class_member_with_policy_ 4.63MB 4.63MB -0.01% (256.00B) ⬇️
member_lookup_with_policy_inner::interned_arguments 2.66MB 2.66MB -0.00% (120.00B) ⬇️
Type<'db>::class_member_with_policy_::interned_arguments 2.29MB 2.29MB -0.00% (104.00B) ⬇️
place_table 233.14kB 233.09kB -0.03% (60.00B) ⬇️

prefect

Name Old New Diff Outcome
all_narrowing_constraints_for_expression 12.27MB 12.26MB -0.07% (8.75kB) ⬇️
is_redundant_with_impl::interned_arguments 2.38MB 2.38MB -0.07% (1.72kB) ⬇️
is_redundant_with_impl 1.99MB 1.99MB -0.07% (1.46kB) ⬇️
IntersectionType<'db>::from_two_elements_ 89.90kB 88.63kB -1.41% (1.27kB) ⬇️
IntersectionType 1012.30kB 1011.10kB -0.12% (1.20kB) ⬇️
place_table 910.14kB 909.55kB -0.06% (600.00B) ⬇️
infer_expression_types_impl 55.22MB 55.22MB +0.00% (420.00B) ⬇️
infer_definition_types 75.88MB 75.88MB +0.00% (372.00B) ⬇️
infer_scope_types_impl 47.65MB 47.65MB +0.00% (360.00B) ⬇️
IntersectionType<'db>::from_two_elements_::interned_arguments 85.51kB 85.25kB -0.30% (264.00B) ⬇️
infer_statement_types_impl 878.18kB 878.36kB +0.02% (180.00B) ⬇️
loop_header_reachability 428.98kB 429.05kB +0.02% (72.00B) ⬇️
UnionType 1.38MB 1.38MB -0.00% (16.00B) ⬇️

@charliermarsh charliermarsh force-pushed the charlie/revive-equality-narrowing-enum-performance branch from 9cc5317 to 8efc6b1 Compare June 9, 2026 21:08
@charliermarsh charliermarsh force-pushed the charlie/revive-equality-narrowing-enum-performance branch from 8efc6b1 to 81cb2f2 Compare June 9, 2026 22:12
@codspeed-hq

codspeed-hq Bot commented Jun 9, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 67 untouched benchmarks
⏩ 60 skipped benchmarks1


Comparing charlie/revive-equality-narrowing-enum-performance (f5d9dae) with charlie/revive-equality-narrowing (35f160f)

Open in CodSpeed

Footnotes

  1. 60 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@charliermarsh charliermarsh force-pushed the charlie/revive-equality-narrowing-enum-performance branch from 81cb2f2 to 801415f Compare June 9, 2026 23:43
@astral-sh-bot

astral-sh-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@charliermarsh charliermarsh force-pushed the charlie/revive-equality-narrowing-enum-performance branch from 801415f to 5803734 Compare June 10, 2026 03:30
@charliermarsh charliermarsh force-pushed the charlie/revive-equality-narrowing-enum-performance branch from 5803734 to f5d9dae Compare June 10, 2026 04:09
@charliermarsh charliermarsh force-pushed the charlie/revive-equality-narrowing branch from 35f160f to b0d6ec8 Compare June 10, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant