Nodes: Improve socket node lookup performance

Use the newly added node topology cache to find the node that contains
a socket rather than looping through all nodes every time. The change
improves performance of drawing a some large node trees by 2-3x.
This commit is contained in:
Hans Goudey 2022-10-31 16:31:36 +01:00
parent 0e6f2d9fe0
commit c39eb09ae5
Notes: blender-bot 2023-02-14 06:49:57 +01:00
Referenced by commit 57dd1b7799, Fix T102386: crash when trying to link sockets from different node trees
Referenced by issue #103321, Regression: NodeSocket.node is None on Node.copy
1 changed files with 12 additions and 4 deletions

View File

@ -2022,21 +2022,29 @@ bNode *nodeFindNodebyName(bNodeTree *ntree, const char *name)
bool nodeFindNode(bNodeTree *ntree, bNodeSocket *sock, bNode **r_node, int *r_sockindex)
{
*r_node = nullptr;
if (!ntree->runtime->topology_cache_is_dirty) {
bNode *node = &sock->owner_node();
*r_node = node;
if (r_sockindex) {
ListBase *sockets = (sock->in_out == SOCK_IN) ? &node->inputs : &node->outputs;
*r_sockindex = BLI_findindex(sockets, sock);
}
return true;
}
LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
ListBase *sockets = (sock->in_out == SOCK_IN) ? &node->inputs : &node->outputs;
int index = 0;
LISTBASE_FOREACH (bNodeSocket *, tsock, sockets) {
int i;
LISTBASE_FOREACH_INDEX (bNodeSocket *, tsock, sockets, i) {
if (sock == tsock) {
if (r_node != nullptr) {
*r_node = node;
}
if (r_sockindex != nullptr) {
*r_sockindex = index;
*r_sockindex = i;
}
return true;
}
index++;
}
}
return false;