-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Remove potential leak in CachingSemanticModelProvider._providerCache #77192
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,30 +23,31 @@ namespace Microsoft.CodeAnalysis.Diagnostics | |
| /// </summary> | ||
| internal sealed class CachingSemanticModelProvider : SemanticModelProvider | ||
| { | ||
| public static CachingSemanticModelProvider Instance { get; } = new CachingSemanticModelProvider(); | ||
|
|
||
| private static readonly ConditionalWeakTable<Compilation, PerCompilationProvider>.CreateValueCallback s_createProviderCallback | ||
| = new ConditionalWeakTable<Compilation, PerCompilationProvider>.CreateValueCallback(compilation => new PerCompilationProvider(compilation)); | ||
|
|
||
| private readonly ConditionalWeakTable<Compilation, PerCompilationProvider> _providerCache; | ||
| private static readonly ConditionalWeakTable<Compilation, PerCompilationProvider> s_providerCache = new ConditionalWeakTable<Compilation, PerCompilationProvider>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this made
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The general guidance per @DustinCampbell is that CWT declarations should be static if possible to remove the cyclic leak possibility. But yes, it doesn't need to be static now that the owner is a singleton, but I figured it was best to do so. |
||
|
|
||
| public CachingSemanticModelProvider() | ||
| private CachingSemanticModelProvider() | ||
| { | ||
| _providerCache = new ConditionalWeakTable<Compilation, PerCompilationProvider>(); | ||
| } | ||
|
|
||
| public override SemanticModel GetSemanticModel(SyntaxTree tree, Compilation compilation, SemanticModelOptions options = default) | ||
| => _providerCache.GetValue(compilation, s_createProviderCallback).GetSemanticModel(tree, options); | ||
| => s_providerCache.GetValue(compilation, s_createProviderCallback).GetSemanticModel(tree, options); | ||
|
|
||
| internal void ClearCache(SyntaxTree tree, Compilation compilation) | ||
| { | ||
| if (_providerCache.TryGetValue(compilation, out var provider)) | ||
| if (s_providerCache.TryGetValue(compilation, out var provider)) | ||
| { | ||
| provider.ClearCachedSemanticModel(tree); | ||
| } | ||
| } | ||
|
|
||
| internal void ClearCache(Compilation compilation) | ||
| { | ||
| _providerCache.Remove(compilation); | ||
| s_providerCache.Remove(compilation); | ||
| } | ||
|
|
||
| private sealed class PerCompilationProvider | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a comment linking to the CWT issue so someone doesn't unintentionally undo this change in the future.