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 SubstitutedNamedTypeSymbol.GetMembers to avoid unnecessary allocations #69164

Merged
merged 3 commits into from
Aug 14, 2023

Conversation

Youssef1313
Copy link
Member

Fixes #69163

@Youssef1313 Youssef1313 requested a review from a team as a code owner July 22, 2023 08:15
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 22, 2023
@ghost ghost added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Jul 22, 2023
@Youssef1313 Youssef1313 marked this pull request as draft July 22, 2023 09:04
@Youssef1313 Youssef1313 marked this pull request as ready for review July 22, 2023 14:27
if (!_lazyMembers.IsDefault)
{
// If we already calculated the ordered members, return it directly.
return _lazyMembers;
Copy link
Contributor

@AlekseyTs AlekseyTs Jul 24, 2023

Choose a reason for hiding this comment

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

_lazyMembers

I think we should do ConditionallyDeOrder in this case. #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup makes sense. Thanks!

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 1)

if (!_lazyMembers.IsDefault)
{
// If we already calculated the ordered members, return it directly.
return _lazyMembers.ConditionallyDeOrder();
Copy link
Member

Choose a reason for hiding this comment

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

I realize the comment for GetMembersUnordered() states the order may change from call to call, but this may be the first implementation where that is the case (the order will be different before and after _lazyMembers is populated). Is that a concern?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is that a concern?

We do not expose GetMembersUnordered to external consumers. However, there is still a chance that an internal implementation somewhere took a dependency on getting items in the same order each time. Reviewing existing call sites might help. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Reviewing existing call sites might help.

There are 50 callsites for NamespaceOrTypeSymbol.GetMembersUnordered(), and a few of those are methods that return that value so the number of use-sites may be too many to review easily. That said, I skimmed through the direct callers of GetMembersUnordered() and didn’t see any obvious issues.

Copy link
Member

@cston cston Jul 26, 2023

Choose a reason for hiding this comment

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

@Youssef1313, it looks like the perf issue is allocations from GetMembers() and not GetMembersUnordered(). Given that, to avoid the risk of breaking callers of GetMembersUnordered(), let's revert the change to GetMembersUnordered(), and always recalculate that array rather than using _lazyMembers if available. Thanks.

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 2)

@jcouv jcouv added this to the 17.8 milestone Jul 26, 2023
@jcouv jcouv removed the untriaged Issues and PRs which have not yet been triaged by a lead label Jul 26, 2023
cston
cston previously approved these changes Jul 26, 2023
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.

LGTM (commit 2)

@cston cston self-requested a review July 26, 2023 17:56
@cston cston dismissed their stale review July 26, 2023 17:57

Adding comment.

@Youssef1313
Copy link
Member Author

@cston Sorry for leaving this one for long. I addressed your comment.

Is this ready to merge now?

@cston cston merged commit b91897c into dotnet:main Aug 14, 2023
@ghost ghost modified the milestones: 17.8, Next Aug 14, 2023
@cston
Copy link
Member

cston commented Aug 14, 2023

Thanks @Youssef1313.

@Youssef1313 Youssef1313 deleted the perf-getmembers branch August 15, 2023 02:05
@dibarbet dibarbet removed this from the Next milestone Aug 28, 2023
@dibarbet dibarbet added this to the 17.8 P2 milestone Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SubstitutedNamedTypeSymbol.GetMembers isn't cached causing 4% allocations of SubstitutedMethodSymbol
5 participants