-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Source Generator Cancellation #13433
Conversation
… source generator.
FYI, I separated out the cancellation changes from the rest of the optimizations I'm working on upon request (see linked bug). |
Thank you for your pull request. We are auto-formating your source code to follow our code guidelines. |
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.
I'm really interested by the results of this changes (numbers). Please share if you can collect them
@StephaneDelcroix I would be interested too. During my debugging I only ran into one cancellation. Most of the time it was running without being cancelled. So while this should fix the thread starvation issue the overall perf problem still remains. I was already working on that part too but I was asked to split out the cancellation issue to reduce the risk so it can be backported to servicing. @Redth what's the process for servicing? |
@mgoertz-msft I added the label which will cause this PR to show up in our next weekly MAUI call to review backport suggestions. |
@@ -293,7 +300,7 @@ static bool GetXamlCompilationProcessingInstruction(XmlDocument xmlDoc) | |||
return false; | |||
} | |||
|
|||
static IEnumerable<(string name, string type, string accessModifier)> GetNamedFields(XmlNode root, XmlNamespaceManager nsmgr, Compilation compilation, IList<XmlnsDefinitionAttribute> xmlnsDefinitionCache) | |||
static IEnumerable<(string name, string type, string accessModifier)> GetNamedFields(XmlNode root, XmlNamespaceManager nsmgr, Compilation compilation, IList<XmlnsDefinitionAttribute> xmlnsDefinitionCache, CancellationToken cancellationToken) |
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.
Would it be possible to also get the cancellation token into GetTypeName, since I think that was the hotspot here?
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.
There is no loop inside of GetTypeName so the check in the GetNamedFields loop will take care of it.
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.
The loop was here if I recall:
string typeName = xmlType.GetTypeReference<string>(xmlnsDefinitionCache, null, |
Since that calls into:
maui/src/Controls/src/Xaml/XmlTypeXamlExtensions.cs
Lines 80 to 89 in 9459133
foreach (string typeName in lookupNames) | |
foreach (XmlnsDefinitionAttribute xmlnsDefinitionAttribute in lookupAssemblies) | |
potentialTypes.Add(new(typeName, xmlnsDefinitionAttribute.ClrNamespace, xmlnsDefinitionAttribute.AssemblyName)); | |
T? type = null; | |
foreach (var typeInfo in potentialTypes) | |
if ((type = refFromTypeInfo(typeInfo)) != null) | |
break; | |
return type; |
Which had that nested foreach loop.
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.
That nested foreach isn't actually doing that much, it just creates a list of potential full type names, which rather small. The rest gets called a lot more often. This was just about cancelling at strategic places. I'm working on another change which should address the real perf issues in this generator.
* ThrowIfCancellationRequested from several strategic points within the source generator. * Auto-format source code --------- Co-authored-by: GitHub Actions Autoformatter <autoformat@example.com>
* ThrowIfCancellationRequested from several strategic points within the source generator. * Auto-format source code --------- Co-authored-by: GitHub Actions Autoformatter <autoformat@example.com>
/backport to net7.0 |
Started backporting to net7.0: https://github.com/dotnet/maui/actions/runs/4254271920 |
Description of Change
Added ThrowIfCancellationRequested from several strategic points within the source generator.
Issues Fixed
MAUI source generator runs a lot and for a long time and without paying attention to any cancellation requests that can lead to thread pool starvation.
Fixes AB#1741294: ThreadPool starvation due to source generators running on multiple threads