Improve performance of SimplifyTypeNames by 5x.#40746
Closed
CyrusNajmabadi wants to merge 171 commits intodotnet:masterfrom
Closed
Improve performance of SimplifyTypeNames by 5x.#40746CyrusNajmabadi wants to merge 171 commits intodotnet:masterfrom
CyrusNajmabadi wants to merge 171 commits intodotnet:masterfrom
Conversation
CyrusNajmabadi
commented
Jan 15, 2020
| // A qualified cref could be many different types of things. It could be referencing a | ||
| // namespace, type or member (including static and instance members). Because of this | ||
| // we basically have no avenues for bailing early and we have to try out all possible | ||
| // simplifications paths. |
Contributor
Author
There was a problem hiding this comment.
note: we could bail out earlier if we store instance members in our indices as well as static members.
Contributor
|
Baseline: With this pull request: I see an improvement in analyzer callback times, but "only" by about 33%. |
Contributor
Author
|
I'm wondering if there's an issue where with your core count and mine. Will rnu some more test and try to break down teh individual costs. |
Use UnionCollection to avoid large set allocations
333fred
reviewed
Jan 16, 2020
333fred
reviewed
Jan 16, 2020
This was referenced Jan 18, 2020
alrz
reviewed
Mar 7, 2020
| _preferPredefinedTypeInDecl = optionSet.GetOption(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInDeclaration, semanticModel.Language)!.Value; | ||
| _preferPredefinedTypeInMemberAccess = optionSet.GetOption(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess, semanticModel.Language)!.Value; | ||
|
|
||
| _visitBaseCompilationUnit = n => base.VisitCompilationUnit(n); |
Member
There was a problem hiding this comment.
you're caching it anyways, maybe not wrap in lambda?
333fred
reviewed
Mar 10, 2020
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis.Text; | ||
| using System.Collections.Immutable; |
Member
There was a problem hiding this comment.
These are the only changes in the file now. Consider reverting.
Contributor
Author
|
Closing out . The majority of these changes went in. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This should be reviewed after #40544 goes in.
This PR introduces a change in the approach the simplify-type-names feature works. Currently the feature is a standard syntax-node analyzer that effectively analyzes pretty much all identifiers in every file to see if they're simplifiable. This is unsurprisingly quite expensive.
The approach taken here is to do the following:
For example, today every identifier is checked to see if it could possibly be replaced with an alias in scope. This can trivially be avoided if, as we walk down the tree, we keep track of the names of types that have an alias to them. i.e. if we see
using MyFoo = X.Foo, then we only need to check identifiers calledFooto see if they could be aliased.We only need to update these tracked names as we encounter them down the tree. That happens vastly less times than the number of times we need to analyze some identifier.
Importantly, we still go through the core simplification system to determine if something is actually simplifiable. THe new system does not make this determination. It just attempts to call into the main system much less as it is very costly.
--
Using hte AnalyzerRunner on Roslyn.sln itself for CSharpSimplifyTypeNamesDiagnosticAnalyzer yields a change from:
An improvement of 5x. There are likely other avenues for improvement. However, my primary concern was safety and correctness. in other words, the simplifier feature is still very conservative and will call into the simplifier engine every time it think there is a potential simplification it could perform.
This is likely unnecessary in many cases as we could probably tell immediately in the feature taht things are safe. However, this would likely involve duplicating some amount of simplification logic (which would then need to be kept in sync).
This approach avoids taht (for now). But does not limit further improvements that could be made here by refactoring the simplification engine and making is smarter and more reusable for situations like this.