Skip to content

Support for nullable arrays and hash code calculation - #1521

Merged
roji merged 3 commits into
npgsql:mainfrom
roji:NullableArrays
Oct 5, 2020
Merged

roji merged 3 commits into
npgsql:mainfrom
roji:NullableArrays

Conversation

@roji

@roji roji commented Oct 5, 2020

Copy link
Copy Markdown
Member

Also cleaned up some value comparer logic which wasn't getting used at all.

Closes #1025

@roji
roji requested a review from YohDeadfall as a code owner October 5, 2020 06:54
Also cleaned up some value comparer logic which wasn't getting used at all.

Closes npgsql#1025
Also implement array hash code calculation
@roji

roji commented Oct 5, 2020

Copy link
Copy Markdown
Member Author

Note: I've also implemented proper hash code calculation as part of this PR.

@roji roji changed the title Support for nullable arrays Support for nullable arrays and hash code calculation Oct 5, 2020
@roji
roji merged commit c81e848 into npgsql:main Oct 5, 2020
@roji
roji deleted the NullableArrays branch October 5, 2020 09:57

@YohDeadfall YohDeadfall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, but can be simplified.

roji added a commit that referenced this pull request Oct 5, 2020
* Support for nullable arrays
* Implemented proper hash code calculation
* Cleaned up some value comparer logic which wasn't getting used at all.

Closes #1025
Closes #1522
@roji

roji commented Oct 5, 2020

Copy link
Copy Markdown
Member Author

Merged via a3976bc

@roji

roji commented Oct 5, 2020

Copy link
Copy Markdown
Member Author

@YohDeadfall fixed in fe63b36. Not super happy that a comparer gets allocated for each GetHashCode call, but it's only for nullable arrays and probably really doesn't matter.

@YohDeadfall

Copy link
Copy Markdown
Contributor

You don't need a new comparer, just some write it this way:

foreach (var element in source)
    if (element is null)
        code.Add(element);
    else
        code.Add(element.Value, comparer);

Internally it will fallback to Nullable<T>.GetHashCode() which returns 0. Or return to the code you used here before:

foreach (var element in source)
    code.Add(element is { } value ? comparer.GetHashCode(value) : 0);

The last variant does the same thing as HashCode.Add.

@roji

roji commented Oct 5, 2020

Copy link
Copy Markdown
Member Author

@YohDeadfall note that HashSet.Add doesn't accept a hash code as its parameter - it accepts a value on which it invokes GetHashCode. If we use the overload without an IEqualityComparer, that means that the default GetHashCode for the type gets invoked, instead of the one defined on the element's value comparer.

@YohDeadfall

Copy link
Copy Markdown
Contributor

Ah, it's private. Anyway the hash code of an int is the value of that int. Consider the complain about calling Add without the comparer, take a look at the previous line, element is null and the hash code will be 0. Your code does the same.

@roji

roji commented Oct 5, 2020

Copy link
Copy Markdown
Member Author

But not all elements are int, right? This needs to work for arrays over arbitrary value types, and I'm trying to make sure that element's value comparer is always used, and not the type's GetHashCode.

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.

Support for nullable arrays

2 participants