Skip to content
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

Cache the compilation states in compilation order to avoid recalculation #76380

Conversation

ToddGrun
Copy link
Contributor

@ToddGrun ToddGrun commented Dec 11, 2024

Cache the compilation states in compilation order to avoid recalculation

GetStatesInCompilationOrder shows up pretty heavily in CPU usage in the typing scenario in the csharp editing speedometer. The enumerating of the ImmutableList and the repeated searching in the ImmutableSortedDictionary are actually more expensive than one would think.

GetStatesInCompilationOrder is optimized out of the profile, but it looks like it is accounting for about 5% of CPU.

I'm going to run a test insertion with this change and see what the CPU/memory delta of this method looks like with this change.

*** CPU in typing scenario in csharp editing speedometer test ***
image

*** Memory allocations under RegularCompilationTracker.WithDoNotCreateCreationPolicy ***
image

Cache the compilation states in compilation order to avoid recalculation

GetStatesInCompilationOrder shows up pretty heavily in CPU usage in the typing scenario in the csharp editing speedometer. The enumerating of the ImmutableList and the repeated searching in the ImmutableSortedDictionary are actually more expensive than one would think.

GetStatesInCompilationOrder is optimized out of the profile, but it looks like it is accounting for about 5% of CPU.

I'm going to run a test insertion with this change and see what the CPU/memory delta of this method looks like with this change.
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 11, 2024
@@ -397,6 +397,22 @@ public static ImmutableArray<TResult> SelectAsArray<TSource, TResult>(this IRead
return ImmutableCollectionsMarshal.AsImmutableArray(builder);
}

public static ImmutableArray<TResult> SelectAsArray<TSource, TResult, TArg>(this IReadOnlyCollection<TSource>? source, Func<TSource, TArg, TResult> selector, TArg arg)
{
if (source == null)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (source == null)

Didn't realize when I made this change this was under compilers. I can move elsewhere if the compiler folks don't want this override (but it does map closely to some other overloads in this file)

@ToddGrun
Copy link
Contributor Author

ToddGrun commented Dec 12, 2024

VS Test insertion for speedometer results:

https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/598012

Run shows similar CPU/Memory in baseline for WithDoNotCreateCreationPolicy

With the changes, there is a pretty dramatic difference in CPU usage in WithDoNotCreateCreationPolicy. The ImmutableDictionary/ImmutableList costs in the method are pretty much gone. I do feel like this might have been a lucky run with numbers that were just better, but the WithDoNotCreateCreationPolicy changes are definitely outside the bounds of luck.

CPU with changes:
image

Allocations with changes:
image

@ToddGrun ToddGrun changed the title *** WIP -- Cache the compilation states in compilation order to avoid recalculation Cache the compilation states in compilation order to avoid recalculation Dec 12, 2024
@ToddGrun ToddGrun marked this pull request as ready for review December 12, 2024 14:59
@ToddGrun ToddGrun requested review from a team as code owners December 12, 2024 14:59
@ToddGrun
Copy link
Contributor Author

@dotnet/roslyn-compiler ptal

@ToddGrun
Copy link
Contributor Author

@CyrusNajmabadi ptal

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes under Compilers LGTM (commit 1)

@ToddGrun
Copy link
Contributor Author

@dotnet/roslyn-compiler -- need 2nd compiler review (fairly trivial change)

@ToddGrun ToddGrun merged commit abcb361 into dotnet:main Dec 13, 2024
28 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Dec 13, 2024
@dibarbet dibarbet modified the milestones: Next, 17.13 P3 Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead VSCode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants