Skip to content

Member lookup macro expansion performance #66257

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

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented May 31, 2023

When performing name lookup into a type (and its extensions), we may need to expand macros. While we are careful to expand macros that we don't need to expand, we were still looping over all of the extensions of a type (and all of the members of those extensions!) looking for places where macros could have been used. In an example with 6,000 extensions of the same type, generated like this:

let upperLimit = 6000

print("""
  struct MyType {
    func f0() { }
  }
  """)

for i in 1..<upperLimit {
  print("""
    extension MyType {
      func f\(i)() {
        f\(i-1)()
      }
    }  
    """)
}

The checking for potential macro expansions regressed type checker performance by more than 25%, and adding on more extensions compounded the problem nonlinearly.

Introduce mechanisms to ensure that we only check what kinds of macro expansions each extension introduces once, capturing all of the potential macro-introduced names and caching it via the evaluator. Then, keep a list of only the extensions that can produce macro expansions and loop over that---not the whole list of extensions---when we need to check whether a given name might be produced by macros. This returns the example's type-checking time back to what it was

Fixes rdar://109543968, eliminating the regression.

…d to

Keep track of which types/extensions have members that could be produced by
a macro expansion, including the names of those members. Use this to
avoid walking into extensions or type definitions to expand macros
when they don't have any related macros.
…hat matter

Keep track of those types and extensions that have any macro expansions,
so that when we need to check whether to expand macros to satisfy name
lookup, we only look through that list---rather than looking at every
extension again.

This improves compile times significantly for a source file with a
large number of extensions on a single type. However, there is still
much to do here.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test compiler performance

@DougGregor DougGregor merged commit 7e5ecfd into swiftlang:main Jun 1, 2023
@DougGregor DougGregor deleted the member-lookup-macro-expansion-performance branch June 1, 2023 18:55
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