[ty] Fix non-determinism in ConstraintSet.specialize_constrained by dcreager · Pull Request #21744 · astral-sh/ruff (original) (raw)

@dcreager dcreager added internal

An internal refactor or improvement

ty

Multi-file analysis & type inference

labels

Dec 1, 2025

Base automatically changed from dcreager/fire-display to main

December 2, 2025 08:17

dcreager

@dcreager

@dcreager

@dcreager dcreager deleted the dcreager/nondeterminism branch

December 3, 2025 15:19

dcreager added a commit that referenced this pull request

Dec 3, 2025

@dcreager

dcreager added a commit that referenced this pull request

Dec 9, 2025

@dcreager

#21744 fixed some non-determinism in our constraint set implementation by switching our BDD representation from being "fully reduced" to being "quasi-reduced". We still deduplicate identical nodes (via salsa interning), but we removed the logic to prune redundant nodes (one with identical outgoing true and false edges). This ensures that the BDD "remembers" all of the individual constraints that it was created with.

However, that comes at the cost of creating larger BDDs, and on #21551 that was causing performance issues. scikit-learn was producing a function signature with dozens of overloads, and we were trying to create a constraint set that would map a return type typevar to any of those overload's return types. This created a combinatorial explosion in the BDD, with by far most of the BDD paths leading to the never terminal.

This change updates the quasi-reduction logic to prune nodes that are redundant because both edges lead to the never terminal. In this case, we don't need to "remember" that constraint, since no assignment to it can lead to a valid specialization. So we keep the "memory" of our quasi-reduced structure, while still pruning large unneeded portions of the BDD structure.

Pulling this out of #21551 for separate review.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})