You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While doing some general code cleanup on Npgsql, I came across a bit of strangeness in the PostGIS types; the types seem to be neither truly mutable, nor truly immutable.
For example, Coordinate2D (which is at the heart of all the current types) is a mutable struct. On the other hand, PostgisLineString contains an array of 2D coordinates which itself can not be modified. In other words, there's no API for adding/removing points on the line, but since the points themselves are mutable they can be changed. The same is true of the other PostGIS types (e.g. polygon).
A general design guideline is to make structs immutable - Coordinate2D in this case; mutable structs have some very confusing semantics and should generally avoided. On the other hand, if the PostGIS types can hold large amounts of data, making everything truly immutable must have a prohibitive performance cost as users would have to copy an entire polygon for changing one point.
@rossini-t, @AEMPhil, what are your views on this? I'm leaning towards making things immutable in 3.2 but I'd like to hear from people who actually use these types.
Note how all the GetHashCode methods reference Coordinate2D which is mutable - a big no-no. If using PostGIS objects as keys is important then they should probably be immutable.
I see the Postgis types as a quick way to prototype spatial related things or to just do some simple work (paint geometries to some surface), so performance is not really a concern. For this, one should use some third party library.
I think, as you said, that the postgis classes should be immutable. Or we could drop the overrided GetHashCode methods. I copied that bit from Postgres native geometry classes without really thinked of it.
@rwasef1830 I haven't looked at DbGeography yet, what are the guiding principles there? Also note that DbGeography is an Entity Framework API and isn't necessarily very modern. Of course that doesn't make it unsuitable, just to keep in mind.
I haven't looked at it sufficiently as well. It seems to be just a converter class that considers the WKB or the "provider specific value" as the first class citizen and it only exists as a facade to allow people to pull out properties from it. Hmmm... probably not what we're looking for here.
NpgsqlPath, NpgsqlPolygon and perhaps some other types also have the issue that they are mutable and override GetHashCode using their mutable parts within.
For types that are simple structs (like NpgsqlPoint, NpgsqlLSeg or Coordinate2D), which do not contain any reference types, it should not be any problem, if they are mutable and override GetHashCode.
Regarding the Postgis Types I would also recommend making them immutable.
(Although for my new PostgisRawGeometry I would leave the Wkb Byte[] accessable without the need to copy it for unmanaged interop)
Another issue would be that GetHashCode should normally be fast. Perhaps the hashcode should be cached once computed.
@deng0 thanks for looking into this. Yeah, I agree with most of what you say, except maybe that I'm not sure all Postgis types should be immutable. It seems that NpgsqlPoint should be, but the other types should probably not be for performance reasons (this seems to be the opposite of the current situation).
Unfortunately I have too many things going on to dive into this right now, but feel free to continue investigating and experimenting.
Activity
roji commentedon Jun 5, 2016
Note how all the GetHashCode methods reference Coordinate2D which is mutable - a big no-no. If using PostGIS objects as keys is important then they should probably be immutable.
rossini-t commentedon Jun 5, 2016
I see the Postgis types as a quick way to prototype spatial related things or to just do some simple work (paint geometries to some surface), so performance is not really a concern. For this, one should use some third party library.
I think, as you said, that the postgis classes should be immutable. Or we could drop the overrided GetHashCode methods. I copied that bit from Postgres native geometry classes without really thinked of it.
rwasef1830 commentedon Jun 5, 2016
Maybe it should be modeled on the design of DbGeography ?
roji commentedon Jun 5, 2016
@rwasef1830 I haven't looked at DbGeography yet, what are the guiding principles there? Also note that DbGeography is an Entity Framework API and isn't necessarily very modern. Of course that doesn't make it unsuitable, just to keep in mind.
rwasef1830 commentedon Jun 5, 2016
I haven't looked at it sufficiently as well. It seems to be just a converter class that considers the WKB or the "provider specific value" as the first class citizen and it only exists as a facade to allow people to pull out properties from it. Hmmm... probably not what we're looking for here.
deng0 commentedon Aug 1, 2016
Interesting issue. Wasn't really aware that Overriding GetHashCode on mutable types can be so problematic.
I've also read some more here:
https://blogs.msdn.microsoft.com/ericlippert/2011/02/28/guidelines-and-rules-for-gethashcode
http://stackoverflow.com/questions/873654/overriding-gethashcode-for-mutable-objects
NpgsqlPath, NpgsqlPolygon and perhaps some other types also have the issue that they are mutable and override GetHashCode using their mutable parts within.
For types that are simple structs (like NpgsqlPoint, NpgsqlLSeg or Coordinate2D), which do not contain any reference types, it should not be any problem, if they are mutable and override GetHashCode.
Regarding the Postgis Types I would also recommend making them immutable.
(Although for my new PostgisRawGeometry I would leave the Wkb Byte[] accessable without the need to copy it for unmanaged interop)
Another issue would be that GetHashCode should normally be fast. Perhaps the hashcode should be cached once computed.
roji commentedon Aug 1, 2016
@deng0 thanks for looking into this. Yeah, I agree with most of what you say, except maybe that I'm not sure all Postgis types should be immutable. It seems that NpgsqlPoint should be, but the other types should probably not be for performance reasons (this seems to be the opposite of the current situation).
Unfortunately I have too many things going on to dive into this right now, but feel free to continue investigating and experimenting.
roji commentedon Jun 8, 2018
The Postgis types are legacy in 4.0, now that we have first-class support via NetTopologySuite, so this issue should be much less important.
Am going to close for now, although if there's interest I'll reopen (and possibly accept a PR).