Skip to content

Define equality/GetHashCode for the Vector class - #22

Merged
ankane merged 3 commits into
pgvector:masterfrom
roji:Equality
Oct 13, 2023
Merged

ankane merged 3 commits into
pgvector:masterfrom
roji:Equality

Conversation

@roji

@roji roji commented Oct 13, 2023 •

Copy link
Copy Markdown
Contributor

@ankane here's a change adding equality/hash code logic to the Vector class.

Note that this implementation compares the float[] reference and not its contents, so two Vector instances with identical float contents are not considered equal.

I actually think that true value semantics (i.e. comparing the data) would be more useful. However, although the array itself is currently immutable (I added readonly to vec to make that explicit), its contents are mutable (since the full array is exposed rather than e.g. ReadOnlyMemory<float>). This means that Vector is currently a mutable type, and so it's a bad idea to define GetHashCode() over the contents.

IMHO ideally Vector would be fully immutable, accepting and exposing only ReadOnlyMemory<float>; this would allow full value semantics (i.e. equality and hash code based on the data contents), and would incidentally also allow users to define Vectors over slices of data/native memory (this is a general advantage of ReadOnlyMemory over working only with arrays). FYI we're trying to more or less standardize on ReadOnlyMemory<float> in general as the representation for vector databases (e.g. in the SemanticKernel project). Let me know if you want to take it in this direction.

One unrelated note: I'd recommend changing the ToArray() method; there are many places in .NET where ToArray() means that an array copy is created (e.g. IEnumerable.ToArray), whereas here it simply returns the vector (this confused me as well).

@roji
roji force-pushed the Equality branch 2 times, most recently from 1d703a9 to 00518ef Compare October 13, 2023 11:11
@roji roji mentioned this pull request Oct 13, 2023
@ankane

ankane commented Oct 13, 2023

Copy link
Copy Markdown
Member

Thanks @roji. I think it should use value-based equality. Happy to consider using ReadOnlyMemory<float> as the internal representation (just need to make sure the float[] constructor and ToArray still work).

@ankane

ankane commented Oct 13, 2023

Copy link
Copy Markdown
Member

Switched in 5602594.

@roji roji left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ankane rebased this on top of your ReadOnlyMemory changes, changed equality/GetHashCode to do value comparison, and optimized writing a bit.


var vec = value.ToArray();
var dim = vec.Length;
var span = value.Memory.Span;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This accesses the data directly, removing an additional memory allocation and copy.

Comment thread src/Pgvector/Vector.cs
public class Vector : IEquatable<Vector>
{
private ReadOnlyMemory<float> vec;
public ReadOnlyMemory<float> Memory { get; }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This exposes the ReadOnlyMemory publicly, so that users (as well as the plugin's converter) can access the data without copying (which is what ToArray does below)

Comment thread src/Pgvector/Vector.cs
public Vector(ReadOnlyMemory<float> v)
=> vec = v;

public Vector(float[] v)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note that arrays are implicitly convertible to Memory/ReadOnlyMemory, so there's no need for this constructor.

Comment thread src/Pgvector/Vector.cs Resolved
@ankane
ankane merged commit dafc276 into pgvector:master Oct 13, 2023
@roji
roji deleted the Equality branch October 13, 2023 21:36
@ankane

ankane commented Oct 13, 2023

Copy link
Copy Markdown
Member

Awesome, thanks!

@roji

roji commented Oct 13, 2023

Copy link
Copy Markdown
Contributor Author

Thanks for merging! Great to see this coming along.

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.

2 participants