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

Reduce boxing when enumerating lists #5659

Merged
merged 3 commits into from
Mar 5, 2024

Conversation

Erarndt
Copy link
Contributor

@Erarndt Erarndt commented Mar 1, 2024

Bug

Fixes: NuGet/Home#13279

Regression? Last working version:

Description

Reduces boxing by expanding LINQ queries, using .Count instead of .Any(), and uses .NoAllocEnumerate() where possible.

PR Checklist

  • PR has a meaningful title

  • PR has a linked issue.

  • Described changes

  • Tests

    • Automated tests added
    • OR
    • Test exception - Existing coverage
    • OR
    • N/A
  • Documentation

    • Documentation PR or issue filled
    • OR
    • N/A

@Erarndt Erarndt requested a review from a team as a code owner March 1, 2024 19:36
@jeffkl jeffkl self-assigned this Mar 1, 2024
@jeffkl jeffkl added Approved for CI Community PRs created by someone not in the NuGet team labels Mar 1, 2024
@jeffkl jeffkl changed the title Dev erarndt reduce boxing Reduce boxing when enumerating lists Mar 1, 2024
jeffkl
jeffkl previously approved these changes Mar 1, 2024
@@ -123,7 +123,29 @@ private static bool TryConvertOrNormalize(string key, IDictionary<string, string
}
else if (reverse.ContainsKey(key))
{
value = reverse.Where(p => StringComparer.OrdinalIgnoreCase.Equals(p.Key, key)).Select(s => s.Key).Single();
bool found = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

I just realized this is getting a value from a dictionary, can't this just be:

else if (reverse.ContainsKey(key))
{
    foreach (KeyValuePair<string, string> item in reverse)
    {
        if (StringComparer.OrdinalIgnoreCase.Equals(item.Key, key))
        {
            value = item.Key;
            return true;
        }
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely! That's much cleaner. Added NoAllocEnumerate() as well to avoid the enumerator boxing where possible too.

@jeffkl jeffkl merged commit dffcada into NuGet:dev Mar 5, 2024
16 checks passed
@Erarndt Erarndt deleted the dev-erarndt-ReduceBoxing branch March 5, 2024 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community PRs created by someone not in the NuGet team
Projects
None yet
3 participants