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

Stop using anonymous functions in ResolverUtility.FindLibraryByVersionAsync to reduce memory allocations #4348

Merged
merged 3 commits into from
Nov 24, 2021

Conversation

kartheekp-ms
Copy link
Contributor

@kartheekp-ms kartheekp-ms commented Nov 23, 2021

Bug

Fixes: NuGet/Home#11409

Regression? Last working version:

Description

I added a few details about the memory allocations in the linked issue. After reading Use local function instead of lambda (IDE0039) doc and https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions#heap-allocations, I modified the code path to use local function instead of lambda expressions. The reason I chose static local function is because of If you know that your local function won't be converted to a delegate and none of the variables captured by it are captured by other lambdas or local functions that are converted to delegates, you can guarantee that your local function avoids being allocated on the heap by declaring it as a static local function. Note that this feature is available in C# 8.0 and newer. in the doc.

This method has ~880 MB allocations during solution restore for OrchardCore solution https://github.com/OrchardCMS/OrchardCore/tree/75923732c36d2785cb869affb805f0ff19d1847c

image

After code modifications the heap allocations of ResolverUtility.FindLibraryByVersionAsync method have reduced by ~200 MB. for the same solution.

image

PR Checklist

  • PR has a meaningful title

  • PR has a linked issue.

  • Described changes

  • Tests

    • N/A No functional change to the product.
  • Documentation

    • N/A

@kartheekp-ms kartheekp-ms requested a review from a team as a code owner November 23, 2021 20:33
@kartheekp-ms kartheekp-ms changed the title Reduce heap allocations by ~200MB in ResolverUtility.FindLibraryByVersionAsync Reduce heap allocations by ~200MB in Solution restore scenario Nov 23, 2021
Copy link
Member

@zivkan zivkan left a comment

Choose a reason for hiding this comment

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

Firstly, I think "Stop using anonymous functions in ResolverUtility.FindLibraryByVersionAsync" would be a better PR title (and easier to understand when looking at the commit history after this is merged), since 200MB depends on the solution and the current title doesn't say much about how it was fixed. I think pull requests should be about the technicalities of what changed, while linked issues should be the customer friendly explanation/high level of what changed.

Secondly, on the topic of customer friendly titles, the linked issue is currently a private issue in Client.Engineering, and therefore won't appear in release notes. Shouldn't perf improvements like this be listed in the release notes, and therefore the linked issue should be in Home?

@kartheekp-ms kartheekp-ms changed the title Reduce heap allocations by ~200MB in Solution restore scenario Stop using anonymous functions in ResolverUtility.FindLibraryByVersionAsync to reduce memory allocations Nov 23, 2021
@kartheekp-ms kartheekp-ms merged commit 9187a50 into dev Nov 24, 2021
@kartheekp-ms kartheekp-ms deleted the dev-kartheekp-ms-FindLibraryFromSourcesAsync branch November 24, 2021 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reduce heap allocations in ResolverUtility.FindLibraryByVersionAsync
3 participants