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

Skip to content

[ty] Speed up large-union narrowing#26048

Merged
MichaReiser merged 3 commits into
mainfrom
micha/union-builder-rewrite
Jun 17, 2026
Merged

[ty] Speed up large-union narrowing#26048
MichaReiser merged 3 commits into
mainfrom
micha/union-builder-rewrite

Conversation

@MichaReiser

Copy link
Copy Markdown
Member

Summary

Supersedes #23555 with a semantics-preserving optimization for large-union narrowing. Apply De Morgan's law structurally when checking an intersection's negation, avoiding construction and simplification of a temporary union.

This reduces simulated instructions for large_union_narrowing by 22.85% (662,828,371 to 511,360,788).

Test Plan

Testing: Passed ty's semantic test suite, 10,000 property-test cases, Clippy, repository hooks, and CodSpeed.

@MichaReiser MichaReiser added ty Multi-file analysis & type inference performance Potential performance improvement labels Jun 16, 2026
@MichaReiser MichaReiser reopened this Jun 16, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 16, 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.37%. The percentage of expected errors that received a diagnostic held steady at 89.00%. The number of fully passing files held steady at 94/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

Memory usage report

Summary

Project Old New Diff Outcome
flake8 33.03MB 33.02MB -0.01% (3.76kB) ⬇️
trio 81.18MB 81.16MB -0.03% (22.22kB) ⬇️
sphinx 199.07MB 198.87MB -0.10% (201.40kB) ⬇️
prefect 526.65MB 526.39MB -0.05% (272.91kB) ⬇️

Significant changes

Click to expand detailed breakdown

flake8

Name Old New Diff Outcome
is_redundant_with_impl::interned_arguments 90.23kB 89.38kB -0.95% (880.00B) ⬇️
is_redundant_with_impl 76.07kB 75.41kB -0.86% (672.00B) ⬇️
infer_expression_types_impl 990.20kB 989.63kB -0.06% (588.00B) ⬇️
UnionType 69.02kB 68.47kB -0.79% (560.00B) ⬇️
infer_scope_types_impl 673.78kB 673.37kB -0.06% (420.00B) ⬇️
infer_definition_types 1.35MB 1.35MB -0.01% (204.00B) ⬇️
StaticClassLiteral<'db>::implicit_attribute_inner_ 50.85kB 50.65kB -0.39% (204.00B) ⬇️
UnionType<'db>::from_two_elements_ 30.06kB 29.98kB -0.27% (84.00B) ⬇️
infer_expression_type_impl 15.28kB 15.22kB -0.38% (60.00B) ⬇️
member_lookup_with_policy_inner 430.31kB 430.25kB -0.01% (60.00B) ⬇️
all_narrowing_constraints_for_expression 87.13kB 87.07kB -0.07% (60.00B) ⬇️
infer_statement_types_impl 54.68kB 54.62kB -0.11% (60.00B) ⬇️

trio

Name Old New Diff Outcome
infer_expression_types_impl 6.58MB 6.57MB -0.09% (5.88kB) ⬇️
is_redundant_with_impl::interned_arguments 223.95kB 219.91kB -1.80% (4.04kB) ⬇️
infer_definition_types 5.60MB 5.60MB -0.06% (3.27kB) ⬇️
is_redundant_with_impl 175.42kB 172.51kB -1.66% (2.91kB) ⬇️
UnionType 150.70kB 147.92kB -1.85% (2.78kB) ⬇️
all_narrowing_constraints_for_expression 614.18kB 613.16kB -0.17% (1.02kB) ⬇️
infer_scope_types_impl 3.19MB 3.19MB -0.03% (1.02kB) ⬇️
loop_header_reachability 124.94kB 124.47kB -0.38% (480.00B) ⬇️
IntersectionType<'db>::from_two_elements_ 17.59kB 17.18kB -2.33% (420.00B) ⬇️
member_lookup_with_policy_inner 1.50MB 1.50MB -0.01% (216.00B) ⬇️
infer_statement_types_impl 56.43kB 56.34kB -0.15% (84.00B) ⬇️
UnionType<'db>::from_two_elements_ 43.04kB 42.97kB -0.16% (72.00B) ⬇️
Type<'db>::class_member_with_policy_ 1.23MB 1.23MB -0.00% (60.00B) ⬇️

sphinx

Name Old New Diff Outcome
infer_expression_types_impl 23.38MB 23.32MB -0.27% (64.04kB) ⬇️
infer_definition_types 19.60MB 19.56MB -0.18% (35.30kB) ⬇️
is_redundant_with_impl::interned_arguments 1.17MB 1.14MB -2.36% (28.19kB) ⬇️
is_redundant_with_impl 927.54kB 908.06kB -2.10% (19.48kB) ⬇️
UnionType 678.22kB 659.31kB -2.79% (18.91kB) ⬇️
all_narrowing_constraints_for_expression 2.71MB 2.69MB -0.55% (15.23kB) ⬇️
infer_scope_types_impl 10.54MB 10.53MB -0.05% (5.44kB) ⬇️
infer_statement_types_impl 968.66kB 964.50kB -0.43% (4.16kB) ⬇️
loop_header_reachability 386.09kB 382.78kB -0.86% (3.32kB) ⬇️
UnionType<'db>::from_two_elements_ 316.34kB 314.75kB -0.50% (1.59kB) ⬇️
infer_unpack_types 502.95kB 501.52kB -0.28% (1.43kB) ⬇️
member_lookup_with_policy_inner 6.00MB 6.00MB -0.02% (1.16kB) ⬇️
TupleType<'db>::to_class_type_ 162.53kB 161.43kB -0.68% (1.10kB) ⬇️
IntersectionType 552.40kB 551.41kB -0.18% (1008.00B) ⬇️
StaticClassLiteral<'db>::implicit_attribute_inner_ 781.03kB 780.64kB -0.05% (392.00B) ⬇️
... 3 more

prefect

Name Old New Diff Outcome
infer_expression_types_impl 56.84MB 56.76MB -0.15% (86.06kB) ⬇️
infer_definition_types 69.38MB 69.34MB -0.06% (44.68kB) ⬇️
is_redundant_with_impl::interned_arguments 2.39MB 2.35MB -1.49% (36.35kB) ⬇️
UnionType 1.39MB 1.37MB -1.84% (26.31kB) ⬇️
is_redundant_with_impl 1.96MB 1.94MB -1.24% (25.02kB) ⬇️
all_narrowing_constraints_for_expression 6.58MB 6.56MB -0.28% (18.68kB) ⬇️
infer_scope_types_impl 38.98MB 38.97MB -0.03% (10.44kB) ⬇️
UnionType<'db>::from_two_elements_ 770.73kB 766.69kB -0.52% (4.04kB) ⬇️
member_lookup_with_policy_inner 14.26MB 14.25MB -0.03% (3.77kB) ⬇️
IntersectionType 982.75kB 979.63kB -0.32% (3.12kB) ⬇️
loop_header_reachability 427.14kB 424.43kB -0.64% (2.72kB) ⬇️
StaticClassLiteral<'db>::implicit_attribute_inner_ 1.10MB 1.09MB -0.21% (2.36kB) ⬇️
infer_expression_type_impl 384.46kB 382.17kB -0.59% (2.29kB) ⬇️
infer_statement_types_impl 1.54MB 1.54MB -0.12% (1.86kB) ⬇️
infer_unpack_types 993.27kB 991.52kB -0.18% (1.75kB) ⬇️
... 6 more

@astral-sh-bot

astral-sh-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@codspeed-hq

codspeed-hq Bot commented Jun 16, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 14.5%

⚡ 4 improved benchmarks
✅ 67 untouched benchmarks
⏩ 64 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation ty_micro[large_union_narrowing] 302.6 ms 234 ms +29.3%
Simulation ty_micro[literal_match_fallthrough_guarded_any] 137.8 ms 122.1 ms +12.89%
Simulation ty_micro[literal_equality_fallthrough_guarded_any] 161.1 ms 144.1 ms +11.82%
WallTime colour_science 54.5 s 51.7 s +5.29%

Tip

Curious why this is faster? Use the CodSpeed MCP and ask your agent.


Comparing micha/union-builder-rewrite (deca3f8) with main (77194b1)

Open in CodSpeed

Footnotes

  1. 64 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.

@MichaReiser MichaReiser force-pushed the micha/union-builder-rewrite branch from bddd8f2 to 0e38eac Compare June 17, 2026 08:15
@MichaReiser MichaReiser marked this pull request as ready for review June 17, 2026 08:21
@MichaReiser MichaReiser requested a review from a team as a code owner June 17, 2026 08:21
@astral-sh-bot astral-sh-bot Bot requested a review from carljm June 17, 2026 08:21
@MichaReiser

Copy link
Copy Markdown
Member Author

I made it faster :)

@MichaReiser MichaReiser merged commit bcd0853 into main Jun 17, 2026
60 checks passed
@MichaReiser MichaReiser deleted the micha/union-builder-rewrite branch June 17, 2026 11:47
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