-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Reduce allocations in ProjectState's ctor #76369
Conversation
The AsyncLazy allocations in the ProjectState ctor are accounting for 0.6% of allocations during the typing scenario in the csharp editing speedometer test. The construction of these objects can be deferred until they are queried (or copied). Local testing has about 80-90% of these lazy objects never being used, so this should save at around 0.5% of the allocations in this scenario.
{ | ||
get | ||
{ | ||
_lazyChecksums ??= AsyncLazy.Create(static (self, cancellationToken) => self.ComputeChecksumsAsync(cancellationToken), arg: this); |
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 oogy on this. there's a race here where multipel threads could see null and assign a new value. I'm not 100% convinced that computing the checksum is guaranteed to produce the exact same result both times.
can you make this an interlocked CAS so only one wins?
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.
Sure, I can do that. I was definitely under the impression that computing the checksum would generate the same values if it happened to get unlucky and run multiple times.
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 want to believe that that would be the case... but i don't think it's proven to be true.
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.
OK, switched to use Interlocked.CompareExchange
…verlapping work does occur, then have both callers use the same result.
The AsyncLazy allocations in the ProjectState ctor are accounting for 0.6% of allocations during the typing scenario in the csharp editing speedometer test.
The construction of these objects can be deferred until they are queried (or copied). Local testing has about 80-90% of these lazy objects never being used, so this should save at around 0.5% of the allocations in this scenario.