Skip to content

Sema: Don't attempt deep equality if we're also attempting optional-to-optional #78958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Jan 27, 2025

When solving a conversion between two optional types, for example Optional<$T1> and Optional<$T2> , we would introduce a disjunction with three choices:

  • "Deep equality": both sides are identical, so we're equating $T1 and $T2
  • "Optional to optional": we're converting $T1 to $T2
  • "Value to optional": we're binding $T2 to Optional<$T1>.

However, the first one is actually redundant, because any situation where it leads to a solution, the second choice will also lead to a solution. This is inefficient because it results in repeated work in the solver. The first choice was "favored", meaning if it succeeds we would skip the others, which partially mitigated the overhead. However, if we're exploring a dead end in the solution space, we would attempt all three choices, when it would suffice to attempt two.

A new -disable-optimized-restrictions flag turns off the new behavior. I plan on removing this flag as soon as possible. I will also make a similar change to array conversions shortly.

  • Fixes rdar://problem/143338891.
  • Speeds up rdar://problem/140212559.

@slavapestov slavapestov force-pushed the optimize-optional-conversion branch 4 times, most recently from cdbbe17 to b51a68b Compare February 1, 2025 18:43
@slavapestov slavapestov force-pushed the optimize-optional-conversion branch 3 times, most recently from 5b4da72 to b4c5bba Compare February 25, 2025 16:38
@slavapestov slavapestov marked this pull request as ready for review February 25, 2025 16:53
@slavapestov slavapestov force-pushed the optimize-optional-conversion branch from b4c5bba to eea62b1 Compare February 25, 2025 16:53
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

I had to revert swiftlang#78301 to fix
rdar://143338891.

This now brings back the change, but without deleting code; instead,
it's guarded by the -disable-optimized-restrictions flag. Once the
flag is removed, I will once again completely delete OneWayEqual
constraints.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant