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
throwutil::IllegalArgumentException("Declared dimension must be 2, 3, or 4");
}
initialize();
}
In the code above, we see that if the CoordinateSequence is generated by calling with two arguments, sz and dim, then initialize is always called. It calls fillVector which calls std::fill and loops through the size of the new vector to initialize all elements to 0.
Looking at commit 60edf0e we see that creating the CoordinateSequence directly like this is instead of calling getGeometrySequenceFactory()->create(sz, dim).
It's making a FixedSizeCoordinateSequence,templated on the number of dimensions. It's like a fixed size list, like a tuple! Let's look at that constuctor:
We see that it is just coping the dimension. It is not initializing anything.
Conclusion
For all cases of create(size_t, size_t), the code before 60edf0e did not initialize the coordinates. The new code could also skip the initialization to maintain the same behavior. This would also improve performance because we'd eliminate the slow call to std::fill.
The text was updated successfully, but these errors were encountered:
eyal0
linked a pull request
Dec 8, 2022
that will
close
this issue
the code before 60edf0e did not initialize the coordinates
We didn't initialize them explicitly, but they were still initialized. If I create a std::vector<Coordinate>(5) or std::array<Coordinate, 5>, the five elements are initialized using the default constructor for Coordinate.
That said, it should be possible to skip the initialization in many places. At one point I added an initialize flag to the the constructor with a dim parameter. But I think it's better to move in the direction of eliminating this constructor entirely, because the dim flag is ambiguous (does a value of 3 indicate XYZ or XYM?)
tl;dr
No need to initialize the CoordinateSequence when we construct a new one because we never used to do that.
The new code (d463bcb)
geos/src/geom/CoordinateSequence.cpp
Lines 68 to 79 in d463bcb
In the code above, we see that if the
CoordinateSequence
is generated by calling with two arguments,sz
anddim
, theninitialize
is always called. It callsfillVector
which callsstd::fill
and loops through the size of the new vector to initialize all elements to 0.I think that this is not necessary.
The old code (60edf0e)
Looking at commit 60edf0e we see that creating the CoordinateSequence directly like this is instead of calling
getGeometrySequenceFactory()->create(sz, dim)
.60edf0e#diff-95f3875df536135fe955deecfaeab0af5f6af52008d0fd5a56a3ed1b6638aca1L2427-R2429
getCoordinateSequenceFactory
is a method of theGeometryFactory
class:geos/include/geos/geom/GeometryFactory.h
Lines 446 to 449 in 0e8d436
It returns a
CoordinateSequenceFactory
, which has acreate
method. It's here in the old code:geos/include/geos/geom/CoordinateSequenceFactory.h
Lines 87 to 98 in 0e8d436
We see that it is a pure virtual function so it must be implemented by inheritors of this class.
There are only two classes that inherit from this class, the
DefaultCoordinateSequenceFactory
and theCoordinateArraySequenceFactory
:geos/include/geos/geom/CoordinateArraySequenceFactory.h
Line 42 in 0e8d436
geos/include/geos/geom/DefaultCoordinateSequenceFactory.h
Line 24 in 0e8d436
Let's look at them one-by-one
CoordinateArraySequenceFactory
Here's the implementation of
create(size_t, size_t)
:geos/include/geos/geom/CoordinateArraySequenceFactory.h
Lines 69 to 74 in 0e8d436
It's creating a
CoordinateArraySequence
using the constructor and returning aunique_ptr
. Here's that constuctor:geos/src/geom/CoordinateArraySequence.cpp
Lines 37 to 42 in 0e8d436
It is resizing the vector to make it the right size but not initializing anything.
DefaultCoordinateSequenceFactory
Here's the implementation of
create(size_t, size_t)
:geos/include/geos/geom/DefaultCoordinateSequenceFactory.h
Lines 43 to 53 in 0e8d436
It's making a
FixedSizeCoordinateSequence
,templated on the number of dimensions. It's like a fixed size list, like a tuple! Let's look at that constuctor:geos/include/geos/geom/FixedSizeCoordinateSequence.h
Line 35 in 0e8d436
We see that it is just coping the
dimension
. It is not initializing anything.Conclusion
For all cases of
create(size_t, size_t)
, the code before 60edf0e did not initialize the coordinates. The new code could also skip the initialization to maintain the same behavior. This would also improve performance because we'd eliminate the slow call tostd::fill
.The text was updated successfully, but these errors were encountered: