Class vs struct for value types?

29 views
Skip to first unread message

Ivan Kolev

unread,
Mar 26, 2016, 4:39:23 AM3/26/16
to NetTopologySuite
Hi all,

I noticed that GeoAPI defines some basic value types like Coordinate and Envelope as reference types (class) instead of value types (struct).
I suppose this is a consequence of the Java port. Changing these to value types would cause a lot of other changes in the code, maybe even some bugs (if the compiler fails to identify some of the problems caused by the conversion). But this is an important change that I think GeoAPI/NTS could benefit a lot from in the long run. Value types reduce memory usage and usually improve performance. Reducing memory usage is especially important for mobile devices. And in cases where for some reason a reference type is required, the code could work with interfaces (ICoordinate, IEnvelope) which would cause the values to be boxed.
I also suppose this would deviate further the C# code from the original Java library and make future synchronization harder. Still, when porting from Java to C# it makes sense to put to work all the extra tools that .NET provides.
I just noticed that the Interval type is already defined as a struct, so there has been some experience with this matter. But maybe that type is less used and it has been easier to convert.

Best regards,
Ivan

Diego Guidi

unread,
Mar 26, 2016, 9:15:43 AM3/26/16
to NetTopologySuite
Yep, there was a lot of discussions about this in the past months (maybe years).
Anyway, you can obtain similar results with a different ICoordinateSequence implementation that, as example, store values in value type arrays.  

Ivan Kolev

unread,
Mar 26, 2016, 10:52:22 AM3/26/16
to NetTopologySuite
Yes, ICoordinateSequence can help in the cases when I want to pass my data to NTS. Of course, for that to work the NTS class should have a method like AddRange(ICoordinateSequence), which for example ISpatialIndex does not have.
But there are also cases when it would be better if NTS used a value type to store the data. And as data is normally private, this would have just local effect on the implementation of a single class, not the library as a whole or its clients. For example, I tried changing KdNode to store the point as a pair of doubles instead of a Coordinate and measured memory usage and performance improvements.
Reply all
Reply to author
Forward
0 new messages