Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#40 Performance improvements #45

Merged
merged 8 commits into from
Oct 26, 2021
Merged

Conversation

artem-ogre
Copy link
Owner

@artem-ogre artem-ogre commented Oct 21, 2021

  • Implement randomized vertex insertion option and make it default
  • Allow custom nearest point locators
  • Implement boost::rtree and random-nearest as locators
  • Implement kd-tree for nearest point location
  • Switch index types used in CDT (e.g., vertices, triangles) from 64-bit to 32-bit
  • 64-bit index types can be enabled with CDT_USE_64_BIT_INDEX_TYPE
  • CDT code refactoring, some APIs changed
  • Make necessary adjustments to visualizer code
  • Compatibility with c++98 standard

@artem-ogre artem-ogre linked an issue Oct 21, 2021 that may be closed by this pull request
- Implement randomized vertex insertion option and make it default
- Allow custom nearest point locators
- Implement boost::rtree and random-nearest as locators
- Implement kd-tree for nearest point location
- Switch index types used in CDT (e.g., vertices, triangles) from 64-bit to 32-bit
- 64-bit index types can be enabled with CDT_USE_64_BIT_INDEX_TYPE
- CDT code refactoring, some APIs changed
- Make necessary adjustments to visualizer code
- Compatibility with c++98 standard
@artem-ogre artem-ogre force-pushed the feature/40-performance-improvements branch from a38de72 to eddbe7c Compare October 21, 2021 19:25
Copy link

@AndreFecteau AndreFecteau left a comment

Choose a reason for hiding this comment

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

Everything looks good, a few questions/recommendations but nothing that I can tell is a show stopper.

@@ -180,27 +198,22 @@ class CDT_EXPORT Triangulation
private:
/*____ Detail __*/
void addSuperTriangle(const Box2d<T>& box);
void insertVertex(const V2d<T>& pos);
void addNexVertex(const V2d<T>& pos, const TriIndVec& tris);

Choose a reason for hiding this comment

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

I'm assuming this should be addNextVertex(...)?

Suggested change
void addNexVertex(const V2d<T>& pos, const TriIndVec& tris);
void addNextVertex(const V2d<T>& pos, const TriIndVec& tris);

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thanks, fixed in b3143fb
It was supposed to be addNewVertex :)


vertices.reserve(nExistingVerts + std::distance(first, last));
for(TVertexIter it = first; it != last; ++it)
addNexVertex(V2d<T>::make(getX(*it), getY(*it)), TriIndVec());

Choose a reason for hiding this comment

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

Suggested change
addNexVertex(V2d<T>::make(getX(*it), getY(*it)), TriIndVec());
addNextVertex(V2d<T>::make(getX(*it), getY(*it)), TriIndVec());

/// @tparam MaxStackDepth size of statically allocated stack for nearest query.
template <
typename TCoordType,
size_t NumVerticesInLeaf = 20,

Choose a reason for hiding this comment

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

Did you test to see if this is a good enough leaf size. I did some testing with my original tree but it had to compete against pointer calls. It might be more efficient to use smaller leafs.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I did some quick testing and smaller size does not improve the situation but makes it slightly worse. Bigger sized can speed-up things just a notch. I've ended up setting 32 as default for now.

{}
};
// allocated in class (not in the 'nearest' method) for better performance
mutable CDT::array<NearestTask, MaxStackDepth> m_tasksStack;

Choose a reason for hiding this comment

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

I personally don't like mutable, I do understand that this is probably a good efficiency boost.

I would warn though that since we always split in half. We don't have access to all the data at the start. This tree is not balanced, if you add lets say 100 points close together and then one point really far away I believe (I haven't tried it) you will hit the MaxStackDepth pretty easily.

Copy link
Owner Author

@artem-ogre artem-ogre Oct 23, 2021

Choose a reason for hiding this comment

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

a4c7808
I switched array to std::vector so now we should not run out of stack depth. Performance looks to be virtually the same.

struct Node
{
children_type children; ///< two children if not leaf; {0,0} if leaf
point_data_vec data; ///< points' data if leaf

Choose a reason for hiding this comment

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

In my experience allocation of deallocation of vectors is expensive. Without testing it I can't tell if this will be significant in this case. Just something to keep an eye on in the future.

Copy link
Owner Author

@artem-ogre artem-ogre Oct 23, 2021

Choose a reason for hiding this comment

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

In this implementation it's reserved to NumVerticesInLeaf on construction and deallocated when the leaf is split.
So re-allocations should not be causing too many problems I hope.

Make initial stack size and stack increment when re-sizing template parameters

Performance is the same
Andre Fecteau contributed original KD-tree implementation:
https://github.com/AndreFecteau/CDT/tree/OptimizationByUsingKDTree
It was heavily modified and included into CDT source.
@artem-ogre
Copy link
Owner Author

Removed other locators and make kd-tree the only default one. The reason is to have less code to maintain as kd-tree is clearly superior.

Add Andre Fecteau to contributors list
@artem-ogre artem-ogre force-pushed the feature/40-performance-improvements branch from bbcf5c4 to 43dd70c Compare October 26, 2021 08:29
@artem-ogre artem-ogre merged commit ff12d27 into master Oct 26, 2021
@artem-ogre artem-ogre deleted the feature/40-performance-improvements branch October 26, 2021 12:17
zhivkob added a commit to zhivkob/CDT that referenced this pull request Oct 26, 2021
After changes from pull request artem-ogre#45 initialization with a grid no longer worked and the file didn't compile at all.
artem-ogre pushed a commit that referenced this pull request Oct 26, 2021
After changes from pull request #45 initialization with a grid no longer worked and the file didn't compile at all.
zhivkob added a commit to zhivkob/CDT that referenced this pull request Oct 27, 2021
commit 6e86109
Author: artem-ogre <artem.ogre@gmail.com>
Date:   Tue Oct 26 21:17:32 2021 +0200

    Update CDT version in CMakeLists.txt

commit a5e1339
Author: artem-ogre <artem.ogre@gmail.com>
Date:   Tue Oct 26 21:00:06 2021 +0200

    Fix InitializeWithGrid and VerifyTopology after CDT re-factoring

    Add instantiations to CDT.cpp to avoid such problems in the future

commit 6008441
Author: Zhivko Bogdanov <zhivkobog@gmail.com>
Date:   Tue Oct 26 19:51:31 2021 +0200

    Fixed output from `generateGridVertices`.

    After changes from pull request artem-ogre#45 initialization with a grid no longer worked and the file didn't compile at all.
zhivkob added a commit to zhivkob/CDT that referenced this pull request Oct 28, 2021
commit 42fdbb7
Author: artem-ogre <artem.ogre@gmail.com>
Date:   Thu Oct 28 14:14:21 2021 +0200

    Bump version to 1.1.2 in CMakeLists.txt

commit 37fe95d
Author: artem-ogre <artem.ogre@gmail.com>
Date:   Wed Oct 27 15:14:37 2021 +0200

    Update doxygen comments

commit 62c6047
Author: artem-ogre <artem.ogre@gmail.com>
Date:   Wed Oct 27 14:18:30 2021 +0200

    Remove default template arguments from KDTree class

commit 9b7ad4f
Author: Zhivko Bogdanov <zhivkobog@gmail.com>
Date:   Wed Oct 27 12:42:21 2021 +0200

    Format changes using the provided .clang-format style.

commit 1b356db
Author: Zhivko Bogdanov <zhivkobog@gmail.com>
Date:   Wed Oct 27 09:29:18 2021 +0200

    Renamed coordinate type template argument failing compilation.

commit 6ceafa5
Author: Zhivko Bogdanov <zhivkobog@gmail.com>
Date:   Wed Oct 27 09:22:27 2021 +0200

    Added missing additional template argument to the triangulation.

    After the refactor there is an additional template argument that was missing from the output parameter in `initializeWithRegularGrid` and `initializeWithIrregularGrid`.

commit b8cf8f8
Author: Zhivko Bogdanov <zhivkobog@gmail.com>
Date:   Wed Oct 27 09:10:31 2021 +0200

    Provide customization points to the k-d tree in the locator.

    If we want to customize any of the k-d tree template values, we have to change the instance within the locator. To support multiple locators with different configurations, it's best to have the template arguments available in the locator declaration as well.

commit 86510f5
Author: Artem Amirkhanov <artem.ogre@gmail.com>
Date:   Wed Oct 27 10:04:23 2021 +0200

    Change 'C++03' to 'C++98' in README

commit 8f1b434
Author: artem-ogre <artem.ogre@gmail.com>
Date:   Wed Oct 27 09:51:00 2021 +0200

    Move in generateGridVertices if c++11 is supported

commit 6e86109
Author: artem-ogre <artem.ogre@gmail.com>
Date:   Tue Oct 26 21:17:32 2021 +0200

    Update CDT version in CMakeLists.txt

commit a5e1339
Author: artem-ogre <artem.ogre@gmail.com>
Date:   Tue Oct 26 21:00:06 2021 +0200

    Fix InitializeWithGrid and VerifyTopology after CDT re-factoring

    Add instantiations to CDT.cpp to avoid such problems in the future

commit 6008441
Author: Zhivko Bogdanov <zhivkobog@gmail.com>
Date:   Tue Oct 26 19:51:31 2021 +0200

    Fixed output from `generateGridVertices`.

    After changes from pull request artem-ogre#45 initialization with a grid no longer worked and the file didn't compile at all.
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.

Performance improvements
2 participants