Skip to content

[5.9🍒] Fix a performance issue when answering "is this tuple Copyable"? #65709

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

Merged

Conversation

kavon
Copy link
Member

@kavon kavon commented May 5, 2023

• Description: This PR fixes a performance regression in a RegexBuilder test that triggered this workaround. The regression was caused by the addition of the Copyable constraint on generic type parameters, manifesting as a "expression too complex" error message.
• Risk: Low. It's very unlikely for this change to cause regressions in typechecking other programs, based on how the solver operates.
• Original PR: #65673
• Reviewed By: @xedin
• Testing: compiler performance testing showed no regression in performance. Also manually tested different builds of the compiler to see that this patch makes the error go away. See original PR for rationale about why this is sufficient.
• Resolves: rdar://107536402

If we don't split up the tuple into individual constraints,
we end up spending more time querying whether a tuple is
Copyable in `lookupConformance`, because it will naively
check the types of all elements of the tuple recursively
with `lookupConformance`.

This is inefficient because if we know some of the elements
of the tuple are fixed types, we don't need to keep checking
those again. For example, if we have `($T, Int, $U)`, and
then try a binding for `$T`, we might ask again if the whole
tuple conforms. Leading to `lookupConformance` to check
whether `Int` (and all other elements of the tuple) conforms
to Copyable, when we either already know that, or can't
answer it yet because it's still a type variable.

By splitting up a Copyable constraint on a tuple into
invidivual  constraints on each of its type elements,
we can avoid this redundant work by `lookupConformance`.

While today we could short-cut this even further to say
that _all_ tuples are Copyable, since we emit an error if
a noncopyable type appears in one, that won't be true in
the near future. This is the nicer solution we'll want to
keep around long-term.

After discussing this with Pavel, we don't think there's a
good way to add a regression test for this, because the
performance issue primarily comes up in specific example
programs that aren't amenable to scale tests.

resolves rdar://107536402

(cherry picked from commit ce04b84)
@kavon kavon requested a review from a team as a code owner May 5, 2023 18:25
@kavon
Copy link
Member Author

kavon commented May 5, 2023

@swift-ci please test

@kavon kavon merged commit 872a9d5 into swiftlang:release/5.9 May 5, 2023
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.

2 participants