-
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
[Performance] Reduce allocations in PENamedTypeSymbol.EnsureTypeParametersAreLoaded
#70766
[Performance] Reduce allocations in PENamedTypeSymbol.EnsureTypeParametersAreLoaded
#70766
Conversation
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.
Special casing doesn't really seem worth it to me for this case. If future perf traces indicate that it would be a real improvement, we can look at it at that point.
var moduleSymbol = ContainingPEModule; | ||
|
||
// If this is a nested type generic parameters in metadata include generic parameters of the outer types. | ||
int firstIndex = _genericParameterHandles.Count - _arity; | ||
|
||
TypeParameterSymbol[] ownedParams = new TypeParameterSymbol[_arity]; |
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.
@333fred avoiding this unnecessary allocation on every type does seem like a nice to have.
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.
Huh?
@@ -2658,19 +2658,26 @@ private void EnsureTypeParametersAreLoaded() | |||
{ | |||
if (_lazyTypeParameters.IsDefault) | |||
{ | |||
if (_arity == 0) | |||
{ | |||
_lazyTypeParameters = ImmutableArray<TypeParameterSymbol>.Empty; |
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 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.
As an alternative, we could check arity and assign this way inside the constructor. There is a guarantee that there is no race with any other thread there,
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 would prefer that approach myself.
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 would prefer that approach myself.
I assume "that" refers to the second comment? 2456a06
Done with review pass (commit 1) |
The interlocked API is also used to make the assignment atomic. In that sense, it really doesn't matter whether we are trying to assign the same instance underneath or not. What we are trying to avoid as well is partial assignment. |
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.
LGTM (commit 3)
@Youssef1313 Thank you for the contribution. |
ImmutableArray.Create(element1)
andImmutableArray.Create(element1, element2)
respectively, which would be even more efficient than ArrayBuilder. I'm not sure though if it's worth doing it that way.