- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4.2k
Switch Project data structures from ImmutableDictionary => Dictionary and lock #78287
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 all commits
084437c
              014d868
              9381844
              5677bc5
              2b26fe6
              64c72c2
              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 | 
|---|---|---|
|  | @@ -27,9 +27,8 @@ namespace Microsoft.CodeAnalysis; | |
| /// </summary> | ||
| public partial class Solution | ||
| { | ||
|  | ||
| // Values for all these are created on demand. | ||
| private ImmutableDictionary<ProjectId, Project> _projectIdToProjectMap; | ||
| // Values for all these are created on demand. Only access when holding the dictionary as a lock. | ||
| private readonly Dictionary<ProjectId, Project> _projectIdToProjectMap = []; | ||
| 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. since you do initialize this, consider having hte dict be its own lock. 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. are you auditing all usages of _projectIdToProjectMap to make sure it is locked? 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. Yes, I did check all five dictionaries converted, just didn't notice the TryGetValue calls on _idToSourceGeneratedDocumentMap | ||
|  | ||
| /// <summary> | ||
| /// Result of calling <see cref="WithFrozenPartialCompilationsAsync"/>. | ||
|  | @@ -46,7 +45,6 @@ private Solution( | |
| SolutionCompilationState compilationState, | ||
| AsyncLazy<Solution>? cachedFrozenSolution = null) | ||
| { | ||
| _projectIdToProjectMap = ImmutableDictionary<ProjectId, Project>.Empty; | ||
| CompilationState = compilationState; | ||
|  | ||
| _cachedFrozenSolution = cachedFrozenSolution ?? | ||
|  | @@ -152,7 +150,10 @@ public bool ContainsProject([NotNullWhen(returnValue: true)] ProjectId? projectI | |
| { | ||
| if (this.ContainsProject(projectId)) | ||
| { | ||
| return ImmutableInterlocked.GetOrAdd(ref _projectIdToProjectMap, projectId, s_createProjectFunction, this); | ||
| lock (_projectIdToProjectMap) | ||
| { | ||
| return _projectIdToProjectMap.GetOrAdd(projectId, s_createProjectFunction, this); | ||
| } | ||
| } | ||
|  | ||
| return null; | ||
|  | ||
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.
small nit. consider extracting helper for this pattern. we often have triplicate methods for docs/analyzerdocs/additionaldocs. i lke it when they all call into a single helper.
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.
Extracted both GetOrAdd and TryGetValue calls into their own methods that can be called for any of the dictionaries.