Skip to content

[Code Completion] Split code completion results into context free part that can be cached and contextual part displayed to the user #40471

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

ahoppen
Copy link
Member

@ahoppen ahoppen commented Dec 8, 2021

This is the first (of two) refactoring part of #40399, which has gotten a little big.

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.

@ahoppen
Copy link
Member Author

ahoppen commented Dec 8, 2021

@swift-ci Please smoke test

Comment on lines 718 to 724
/// Maps USRs to \c CodeCompletionResultTypes that have already been
/// constructed.
static llvm::StringMap<CodeCompletionResultType *> USRCache;

/// Maps Types to \c CodeCompletionResultTypes that have already been
/// constructed.
static llvm::DenseMap<Type, CodeCompletionResultType *> TypeCache;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global cache without clearing. This is a little scary, and they leaks. I believe these are tied to an ASTContext. Doesn't it make sense to move this to ASTContext instance member?

Also, can CodeCompletionResultType be allocated on the ASTContext?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I thought that cache would only be introduced by the next PR, sorry.

I will store them in a dedicate arena with ahoppen@e9ef698 but because that change is >200 LOC as well, I didn’t want to inflate this PR even further. Do you think we should pull that commit into this PR or should we just fix it in a follow-up PR?

We can’t store CodeCompletionResultType in the ASTContext because they are used for cached results, which are used for multiple code completion sessions and thus outlive the ASTContext.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can’t store CodeCompletionResultType in the ASTContext because they are used for cached results, which are used for multiple code completion sessions and thus outlive the ASTContext.

I'm not sure the cache should outlive the ASTContext. An ASTContext is used for multiple code completion sessions as long as fast-completion is working. If fast-completion is not working and the ASTContext is destroyed, I think the cache should be cleared, or what cache can we reuse?

@ahoppen
Copy link
Member Author

ahoppen commented Dec 10, 2021

@swift-ci Please smoke test Windows

@ahoppen
Copy link
Member Author

ahoppen commented Dec 13, 2021

@swift-ci Please test Windows

@ahoppen ahoppen force-pushed the pr/split-context-free-codecompletion-result branch from a704547 to 23f0d6e Compare December 14, 2021 19:56
@ahoppen
Copy link
Member Author

ahoppen commented Dec 14, 2021

I refactored the PR to make a cleaner distinction between USR-based and ASTContext-based types. For now ContextFreeCodeCompletionResult stores a CodeCompletionResultType that is always backed by a ASTContext-bound swift::Type. That design doesn’t allow caching the types yet.

In a follow-up PR I will add the option to back CodeCompletionResultType by a swift::ide::USRBasedType which can be cached and supports computation of convertible type relations by storing the USRs of its supertype. The current WIP of that PR lives at https://github.com/ahoppen/swift/tree/pr/compute-typerelation-for-global-cached-results.

@ahoppen
Copy link
Member Author

ahoppen commented Dec 14, 2021

@swift-ci Please smoke test

/// A single code completion result enriched with information that depend on
/// the completion's usage context.
class CodeCompletionResult {
ContextFreeCodeCompletionResult ContextFree;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ContextFreeCodeCompletionResult is fairly big object. Instead of copying it, could CodeCompletionResult just hold a reference to it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about the same but decided against it, because

  • this way, we are sure that ContextFreeCodeCompletionResult is allocated by the same allocator as CodeCompletionResult and we don’t run into issues that ContextFreeCodeCompletionResult might get deallocated while CodeCompletionResult is still referencing it
  • we only need to copy it when constructing CodeCompletionResult and I don’t think it makes much of a performance difference
  • We don’t have to do an indirection to ContextFreeCodeCompletionResult when accessing context-free properties of a CodeCompletionResult

Do you think this makes sense or would you think it’s worth to measure the performance impact of the copy?

@ahoppen ahoppen force-pushed the pr/split-context-free-codecompletion-result branch from 23f0d6e to e0253ca Compare January 14, 2022 16:24
@ahoppen
Copy link
Member Author

ahoppen commented Jan 14, 2022

I have rebased this PR now that #40724 is merged and tried to make it as easy to review as possible:

  • 707e6e855bfc3a6cc65c0d71b408bf63ee9b56ab is a super simple refactoring that just pull out a few nested types from CodeCompletionResult to the top-level
  • e0253ca624e2709c3e4c5a2f88344251c0b72888 performs the actual split into contextual and context-free result bits.

@ahoppen
Copy link
Member Author

ahoppen commented Jan 14, 2022

@swift-ci Please smoke test

@ahoppen ahoppen requested a review from rintaro January 14, 2022 16:29
@ahoppen
Copy link
Member Author

ahoppen commented Jan 19, 2022

@swift-ci Please smoke test Windows

1 similar comment
@ahoppen
Copy link
Member Author

ahoppen commented Jan 20, 2022

@swift-ci Please smoke test Windows

…ultKind to top-level types

Preliminary work to split `CodeCompletionResult` into a context-free and contextual part.
@ahoppen ahoppen force-pushed the pr/split-context-free-codecompletion-result branch from e0253ca to 6f65af5 Compare January 20, 2022 17:00
@ahoppen
Copy link
Member Author

ahoppen commented Jan 20, 2022

@swift-ci Please smoke test

Copy link
Member

@rintaro rintaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

CodeCompletionResultKind Kind : 3;
static_assert(int(CodeCompletionResultKind::MAX_VALUE) < 1 << 3, "");

/// Opaque kind that is interpreted differently depending on
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Incomplete comment? Probably:

Suggested change
/// Opaque kind that is interpreted differently depending on
/// Opaque kind that is interpreted differently depending on 'Kind'.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. That comment pre-dated the use of a union here. I just removed it.

…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.
@ahoppen ahoppen force-pushed the pr/split-context-free-codecompletion-result branch from 6f65af5 to fd72674 Compare January 27, 2022 09:41
@ahoppen
Copy link
Member Author

ahoppen commented Jan 27, 2022

@swift-ci Please smoke test

@ahoppen ahoppen merged commit e8c76a1 into swiftlang:main Feb 8, 2022
@ahoppen ahoppen deleted the pr/split-context-free-codecompletion-result branch April 5, 2022 14:48
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