python / python/mypy

Speed up union simplification

Open
#12,526 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance refactoring
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Union simplification (make_simplified_union) has been causing multiple performance issues (at least #9169, #12408, #12225). It can make proper subtype checks of all union items against all other items, which is O(n**2) -- with certain O(n) fast paths that cover some (but not all) problematic scenarios. Union simplification is fairly performance-critical even when we don't hit worst-case scenarios.

Here are some ideas about what we might do to improve the situation:

  1. Somehow implement union simplification of multiple Instance types (at least simple ones) in close to linear time. I suspect that this is possible under some reasonable assumptions.
  2. Cache negative results of proper subtype checks. I think that currently we only cache positive results (in mypy.typestate). This might have some drawbacks, such as a possible explosion of cache sizes. I assume there's a reason why we aren't currently doing this. Union simplification tends to perform many proper subtype checks with negative results.
  3. Avoid doing full union simplification in some cases, perhaps based on some heuristics. Union simplification should never be semantically necessary.
  4. Add fast paths for the most common union simplification operations (e.g. single item, X | None).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with make_simplified_union and the subtype-check caching in mypy.typestate, then review the related issues #9169, #12408, and #12225. The work is complete when union simplification is measurably faster without changing its semantics, but the issue does not specify a single implementation or benchmark.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, performance
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.