Skip to content

[SourceKit] Add a request kind to notify dependencies are updated #35768

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
merged 1 commit into from
Feb 8, 2021

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Feb 4, 2021

At this point, SourceKit clears the cached compiler instance for code completion if exist.

@rintaro rintaro requested a review from akyrtzi February 4, 2021 23:58
@rintaro
Copy link
Member Author

rintaro commented Feb 5, 2021

@swift-ci Please smoke test

@rintaro rintaro force-pushed the sourcekit-dependencyupdated branch from 6762213 to a4eee0e Compare February 5, 2021 19:04
@rintaro
Copy link
Member Author

rintaro commented Feb 5, 2021

@swift-ci Please smoke test

@rintaro rintaro requested a review from akyrtzi February 5, 2021 19:04
@@ -586,6 +598,11 @@ bool CompletionInstance::shouldCheckDependencies() const {
return threshold <= now;
}

void CompletionInstance::invalidateCachedCompilerInstance() {
std::lock_guard<std::mutex> lock(mtx);
CachedCIInvalidated = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks to me that you have shared access of CachedCIInvalidated but without always using the mutex.
I think you can just make it a std::atomic<bool>.

Copy link
Member Author

Choose a reason for hiding this comment

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

My bad, I moved

  // Clear the cached CI if it's invalidated.
   if (CachedCIInvalidated) {
     clearCachedCompilerInstance();
   }

inside the mutex. Do you still think it should be std::atomic<bool>?

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 don't feel safe CachedCIInvalidated is mutated during the performOperation() anyways. Using mutex to guard it is still needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is the modification in CompletionInstance::cacheCompilerInstance() protected?

I would suggest it's better to use std::atomic<bool> because:

  • One less thing to worry about accessing out of a mutex. With later changes you may end up either not protecting access to this field or blocking the modification of the new request on the mutex longer than you'd expect.
  • It doesn't matter when this is getting modified or not, what matters is when you choose to check for it. It is like a "hint", e.g. say you had a "cancel" boolean that can be set while operation is running and then you check it at certain points, what matters is just to make it's use thread-safe.

How about renaming it to CachedCIShouldBeInvalidated, then it is more clear that modifying this doesn't have an effect until we check it at certain points.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes cacheCompilerInstance() is private method and only called inside performOperation() after std::lock_guard<std::mutex> lock(mtx).

Anyways, I agreed. Updated to use std::atomic<bool>

@rintaro rintaro force-pushed the sourcekit-dependencyupdated branch from a4eee0e to 946f9c6 Compare February 5, 2021 19:37
@rintaro
Copy link
Member Author

rintaro commented Feb 5, 2021

@swift-ci Please smoke test

@rintaro rintaro force-pushed the sourcekit-dependencyupdated branch 2 times, most recently from e02d8ed to d048342 Compare February 5, 2021 23:46
@@ -49,9 +49,11 @@ class CompletionInstance {
llvm::sys::TimePoint<> DependencyCheckedTimestamp;
llvm::StringMap<llvm::hash_code> InMemoryDependencyHash;
unsigned CachedReuseCount = 0;
std::atomic<bool> CachedCIShouldInvalidated;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggesting CachedCIShouldInvalidated -> CachedCIShouldBeInvalidated


// Mark the cached compiler instance "invalidated". It will be cleared in
// next completion. (Thread safe.)
void invalidateCachedCompilerInstance();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggesting invalidateCachedCompilerInstance -> markCachedCompilerInstanceShouldBeInvalidated

@@ -586,6 +597,11 @@ bool CompletionInstance::shouldCheckDependencies() const {
return threshold <= now;
}

void CompletionInstance::invalidateCachedCompilerInstance() {
std::lock_guard<std::mutex> lock(mtx);
Copy link
Contributor

Choose a reason for hiding this comment

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

The lock guard is not necessary right?

@@ -618,6 +634,9 @@ bool swift::ide::CompletionInstance::performOperation(
// the cached completion instance.
std::lock_guard<std::mutex> lock(mtx);

// Clear the cached CI if it's invalidated.
clearCachedCompilerInstanceIfNeeded();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was it necessary to introduce this extra function and not just add an additional check inside performCachedOperationIfPossible()?

@rintaro rintaro force-pushed the sourcekit-dependencyupdated branch from d048342 to 366965c Compare February 6, 2021 01:14
@rintaro
Copy link
Member Author

rintaro commented Feb 6, 2021

How does this look @akyrtzi ?

Copy link
Contributor

@akyrtzi akyrtzi left a comment

Choose a reason for hiding this comment

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

Great!

@rintaro rintaro force-pushed the sourcekit-dependencyupdated branch from 366965c to 27dc2cf Compare February 6, 2021 02:41
@rintaro
Copy link
Member Author

rintaro commented Feb 6, 2021

@swift-ci Please smoke test

@rintaro rintaro merged commit cbbeb91 into swiftlang:main Feb 8, 2021
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