Skip to content

[SourceKit] Compute type relations for global code completion items from the cache #40399

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

ahoppen
Copy link
Member

@ahoppen ahoppen commented Dec 3, 2021

Depends on #40163.


First, split code completion results into context free part that can be cached and contextual part displayed to the user.
This allows makes the distinction between cachable and non-cachable properties cleaner and allows us to more easily compute contextual information (like type relations) for cached items.

Then, compute type relations for cached items. Computing the type relation for every item in the code completion cache is way too expensive (~4x slowdown for global completion that imports SwiftUI). Instead, compute a type’s supertypes (protocol conformances and superclasses) once and write their USRs to the cache. To compute a type relation we can then check if the contextual type is in the completion item’s supertypes.

This reduces the overhead of computing the type relations (again global completion that imports SwiftUI) to ~6% – measured by instructions executed.

Technically, we might miss some conversions like

  • retroactive conformances inside another module (because we can’t cache them if that other module isn’t imported)
  • complex generic conversions (just too complicated to model using USRs)

Because of this, we never report an unkown type relation for global items but always default to unknown.

But I believe this PR covers the most common cases and is a good tradeoff between accuracy and performance.

rdar://83846531

@ahoppen ahoppen requested a review from rintaro December 3, 2021 21:44
@ahoppen
Copy link
Member Author

ahoppen commented Dec 3, 2021

@swift-ci Please smoke test

@ahoppen
Copy link
Member Author

ahoppen commented Dec 3, 2021

@swift-ci Please build toolchain macOS

rintaro and others added 5 commits December 7, 2021 17:23
We don't need to convert them from/to integer
…t that can be cached and contextual part displayed to the user

This allows makes the distinction between cachable and non-cachable properties cleaner and allows us to more easily compute contextual information (like type relations) for cached items later.
…textFreeCodeCompletionResult in a CodeCompletionResult
@ahoppen ahoppen force-pushed the pr/typerelations-for-cached-global-results branch from 34b61ad to 021f316 Compare December 8, 2021 13:35
…ypes

Computing the type relation for every item in the code completion cache is way to expensive (~4x slowdown for global completion that imports `SwiftUI`). Instead, compute a type’s supertypes (protocol conformances and superclasses) once and write their USRs to the cache. To compute a type relation we can then check if the contextual type is in the completion item’s supertypes.

This reduces the overhead of computing the type relations (again global completion that imports `SwiftUI`) to ~6% – measured by instructions executed.

Technically, we might miss some conversions like
- retroactive conformances inside another module (because we can’t cache them if that other module isn’t imported)
- complex generic conversions (just too complicated to model using USRs)

Because of this, we never report an `unkown` type relation for global items but always default to `unknown`.

But I believe this PR covers the most common cases and is a good tradeoff between accuracy and performance.

rdar://83846531
Allow a code completion item to have multiple result type to model the fact that we consider `Int` as both producing either a metatype and an instance type in code completion.
Previously, CodeCompletionResultTypes were stored on the heap and leaked. Instead, store them in a dedicated arena. While that arena is currently never released either, it cleans up the design and lays the foundations to release the arena on memory pressure.
@ahoppen
Copy link
Member Author

ahoppen commented Dec 8, 2021

This PR has gotten a little big. I’m splitting it up into multiple. The refactoring part is #40471.

@ahoppen ahoppen closed this Dec 8, 2021
@ahoppen ahoppen deleted the pr/typerelations-for-cached-global-results branch January 25, 2022 10:43
@ahoppen ahoppen restored the pr/typerelations-for-cached-global-results branch January 25, 2022 10:47
@ahoppen ahoppen deleted the pr/typerelations-for-cached-global-results branch April 5, 2022 14:53
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