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

Use string.Contains instead of IEnumerable.Contains #5551

Merged
merged 1 commit into from
Jan 4, 2024

Conversation

Erarndt
Copy link
Contributor

@Erarndt Erarndt commented Dec 20, 2023

Bug

Fixes: NuGet/Home#13124

Regression? Last working version:

Description

This code was mistakenly using the Enumerable.Contains() overload which ends up allocating an enumerator. Using string.Contains() avoids this allocation and is faster. Example benchmark over 100 invocations:

Method Count Mean Error StdDev Gen0 Allocated
Enumerable.Contains 100 29.830 us 0.0658 us 0.0583 us 0.3662 2003 B
string.Contains 100 5.516 us 0.0105 us 0.0082 us - -

PR Checklist

  • PR has a meaningful title

  • PR has a linked issue.

  • Described changes

  • Tests

    • Automated tests added
    • OR
    • Test exception
    • OR
    • N/A
  • Documentation

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

@Erarndt Erarndt requested a review from a team as a code owner December 20, 2023 21:40
@ghost ghost added the Community PRs created by someone not in the NuGet team label Dec 20, 2023
@@ -204,7 +204,7 @@ public static bool TryParse(string value, bool allowFloating, [NotNullWhen(true)
#if NETCOREAPP2_1_OR_GREATER
if (allowFloating && minVersionString.Contains('*', StringComparison.Ordinal))
Copy link
Member

Choose a reason for hiding this comment

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

Does this have the same problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Additional overloads for string.Contains were added in 2.1+, so this call doesn't have the same issue.

@nkolev92
Copy link
Member

@Erarndt I think we'd probably want more granular issues here, since there's some changes that aren't related to what the main issue is claiming.

@Erarndt
Copy link
Contributor Author

Erarndt commented Dec 20, 2023

@Erarndt I think we'd probably want more granular issues here, since there's some changes that aren't related to what the main issue is claiming.

Sure, I defer to you folks on how to organize these. I've found these while investigating the main issue, so I was unsure how you'd like to categorize them.

@ghost ghost added the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Dec 28, 2023
@ghost
Copy link

ghost commented Dec 28, 2023

This PR has been automatically marked as stale because it has no activity for 7 days. It will be closed if no further activity occurs within another 90 days of this comment. If it is closed, you may reopen it anytime when you're ready again, as long as you don't delete the branch.

@ghost ghost removed the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Jan 2, 2024
@nkolev92 nkolev92 merged commit 15b4da0 into NuGet:dev Jan 4, 2024
16 checks passed
@Erarndt Erarndt deleted the dev-erarndt-TryParseCharEnum branch January 12, 2024 20:30
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
Development

Successfully merging this pull request may close these issues.

Use string.Contains instead of IEnumerable.Contains in VersionRange parsing
3 participants