Skip to content

Commit

Permalink
Micro-optimizations for prominence. (#37)
Browse files Browse the repository at this point in the history
Saves 7% on flat, slow tiles.
  • Loading branch information
akirmse authored Jun 14, 2024
1 parent e5e6f5b commit 300c5b3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
16 changes: 12 additions & 4 deletions code/divide_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ bool DivideTree::setOrigin(const CoordinateSystem &coordinateSystem) {
void DivideTree::compact() {
unordered_set<int> removedIndices;
unordered_set<int> emptyIndices;
removedIndices.reserve(mSaddles.size());

// TODO: May want to save basin saddles for debugging, only delete during merge
for (int i = 0; i < (int) mSaddles.size(); ++i) {
Expand Down Expand Up @@ -542,8 +543,14 @@ void DivideTree::makeNodeIntoRoot(int nodeId) {

int DivideTree::findCommonAncestor(int nodeId1, int nodeId2) {
// First go up tree on one side until both nodes are at the same depth
int depth1 = getDepth(nodeId1);
int depth2 = getDepth(nodeId2);
int ancestor1, ancestor2;
int depth1 = getDepth(nodeId1, &ancestor1);
int depth2 = getDepth(nodeId2, &ancestor2);

// If the topmost ancestors aren't the same, they're in different trees entirely.
if (ancestor1 != ancestor2) {
return Node::Null;
}

while (depth1 > depth2) {
nodeId1 = mNodes[nodeId1].parentId;
Expand Down Expand Up @@ -576,14 +583,15 @@ int DivideTree::findCommonAncestor(int nodeId1, int nodeId2) {
}
}

int DivideTree::getDepth(int nodeId) {
int DivideTree::getDepth(int nodeId, int *ancestorId) {
int depth = 0;

do {
depth += 1;
*ancestorId = nodeId;
nodeId = mNodes[nodeId].parentId;
} while (nodeId != Node::Null);

return depth;
}

Expand Down
3 changes: 2 additions & 1 deletion code/divide_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ class DivideTree {
int findCommonAncestor(int nodeId1, int nodeId2);

// Return the depth of the given node in the tree. A node with no parent has depth 1.
int getDepth(int nodeId);
// ancestor is set to the highest (non-Null) ancestor ID.
int getDepth(int nodeId, int *ancestorId);

// Convert pairs of runoffs at the same location into saddles
void spliceAllRunoffs();
Expand Down

0 comments on commit 300c5b3

Please sign in to comment.