-
Notifications
You must be signed in to change notification settings - Fork 676
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
Conversation
@@ -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; |
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 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;
}
}
}
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.
Definitely! That's much cleaner. Added NoAllocEnumerate()
as well to avoid the enumerator boxing where possible too.
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
Documentation