-
Notifications
You must be signed in to change notification settings - Fork 231
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
UtilityAnalyzer: Reduce lock contention in ShouldGenerateMetrics
#7411
Comments
The UnchangedFilesCache uses the ConditionalWeakTable<TKey,TValue>.GetValue Method which calls
This might result in if (UnchangedFilesCache.TryGetValue(Compilation, out var unchangedFiles))
{
return unchangedFiles;
}
else
{
lock (UnchangedFilesLock)
{
// Might be populated by now
if (UnchangedFilesCache.TryGetValue(Compilation, out var unchangedFiles))
{
return unchangedFiles;
}
else
{
var unchangedFiles = CreateUnchangedFilesHashSet();
UnchangedFilesCache.AddOrUpdate(Compilation, unchangedFiles);
}
}
} This still locks, but the file access only occurs once. I would do this, if the precalculation of the cache does not work for some reason. |
Unrelated minor perf improvement: private static bool HasGeneratedFileName(SyntaxTree tree)
{
if (string.IsNullOrEmpty(tree.FilePath))
{
return false;
}
var fileName = Path.GetFileName(tree.FilePath);
return Array.Exists(GeneratedFileParts, part => fileName.IndexOf(part, StringComparison.OrdinalIgnoreCase) >= 0);
} Same can be done for AutoGeneratedCommentParts in HasAutoGeneratedComment. |
Other things to look at:
We may need to split the issue before we work on it. |
The contention on
|
#6576 introduced a performance regression that was reverted in #7262. Our investigation showed the cause of the regression was that using the
RegisterSemanticModelAction
changed the behaviour of the analyzer to a multithreaded approach.ShouldGenerateMetrics
queries theUnchangedFilesCache
and callsGeneratedCodeRecognizer.IsGenerated
. As both are based on synchronized collections it creates a thread contention which caused the performance regression.The idea is to precalculate the generated and unchanged files and in the SemanticModelAction only use the values. In theory, this should remove the thread contention, enabling us to introduce one again the changes made in #6576.
The text was updated successfully, but these errors were encountered: