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 Array.Empty<T>() #1210

Merged
merged 1 commit into from
Mar 4, 2019
Merged

Use Array.Empty<T>() #1210

merged 1 commit into from
Mar 4, 2019

Conversation

martincostello
Copy link
Member

Use Array.Empty<string>() instead of allocating a new array if either of the strings for comparison are null.

Use Array.Empty<string>() instead of allocating a new array if either of the strings for comparison are null.
@Tratcher Tratcher self-assigned this Mar 2, 2019
@Tratcher Tratcher added this to the 3.0.0-preview4 milestone Mar 2, 2019
Copy link
Member

@gfoidl gfoidl left a comment

Choose a reason for hiding this comment

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

Not related to your change, but when you're on it...

@@ -41,7 +41,7 @@ public int Compare(string x, string y)
var xIsInt = x != null && int.TryParse(x, out value1);
Copy link
Member

Choose a reason for hiding this comment

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

var xIsInt = int.TryParse(x, out int value1);
var yIsInt = int.TryParse(y, out int value2);

The null-check is done in TryParse, so here it is redundant. Variable initialized by the out directly.

var xParts = x?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
var yParts = y?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
var xParts = x?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
var yParts = y?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();

// Compare each part until we get two parts that are not equal
for (int i = 0; i < Math.Min(xParts.Length, yParts.Length); i++)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for (int i = 0; i < Math.Min(xParts.Length, yParts.Length); i++)
var n = Math.Min(xParts.Length, yParts.Length);
for (var i = 0; i < n; i++)

Otherwise n is evaluated at each iteration.

@@ -26,8 +26,8 @@ public class ConfigurationKeyComparer : IComparer<string>
/// <returns></returns>
public int Compare(string x, string y)
{
var xParts = x?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
var yParts = y?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
var xParts = x?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
Copy link
Member

Choose a reason for hiding this comment

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

Is it a common case that either x or y is null?

If so, it may be better to hold Array.Empty<string> in a field, as the call to Empty won't be inlined (last time I checked this was true).

If not, nevermind this comment.

@Tratcher Tratcher merged commit afb772f into dotnet:master Mar 4, 2019
@Tratcher
Copy link
Member

Tratcher commented Mar 4, 2019

Thanks

@martincostello martincostello deleted the Use-Array-Empty branch March 4, 2019 16:00
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Feb 28, 2020
Use Array.Empty<string>() instead of allocating a new array if either of the strings for comparison are null.


Commit migrated from dotnet/extensions@afb772f
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Mar 2, 2020
Use Array.Empty<string>() instead of allocating a new array if either of the strings for comparison are null.


Commit migrated from dotnet/extensions@afb772f
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Mar 11, 2020
Use Array.Empty<string>() instead of allocating a new array if either of the strings for comparison are null.


Commit migrated from dotnet/extensions@afb772f
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Mar 27, 2020
Use Array.Empty<string>() instead of allocating a new array if either of the strings for comparison are null.


Commit migrated from dotnet/extensions@afb772f
@ghost ghost locked as resolved and limited conversation to collaborators May 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants