From a1a31659ea5e015c6534c1795eb4597231517315 Mon Sep 17 00:00:00 2001 From: Iliya Katueshenock Date: Mon, 13 Nov 2023 18:46:04 +0100 Subject: [PATCH] Mesh: Make edge_other_vert function branchless This function shouldn't return invalid index. Instead assertion in used. While the branchless code path may not be observably faster in current code, it should work better with instruction level parallelism in the future. Pull Request: https://projects.blender.org/blender/blender/pulls/114682 --- source/blender/blenkernel/BKE_mesh.hh | 15 ++++++--------- .../nodes/node_geo_input_shortest_edge_paths.cc | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/source/blender/blenkernel/BKE_mesh.hh b/source/blender/blenkernel/BKE_mesh.hh index 79e5f1047c8..648ef023132 100644 --- a/source/blender/blenkernel/BKE_mesh.hh +++ b/source/blender/blenkernel/BKE_mesh.hh @@ -269,17 +269,14 @@ inline int face_triangles_num(const int face_size) /** * Return the index of the edge's vertex that is not the \a vert. - * If neither edge vertex is equal to \a v, returns -1. */ -inline int edge_other_vert(const int2 &edge, const int vert) +inline int edge_other_vert(const int2 edge, const int vert) { - if (edge[0] == vert) { - return edge[1]; - } - if (edge[1] == vert) { - return edge[0]; - } - return -1; + BLI_assert(ELEM(vert, edge[0], edge[1])); + BLI_assert(edge[0] >= 0); + BLI_assert(edge[1] >= 0); + /* Order is important to avoid overflow. */ + return (edge[0] - vert) + edge[1]; } /** \} */ diff --git a/source/blender/nodes/geometry/nodes/node_geo_input_shortest_edge_paths.cc b/source/blender/nodes/geometry/nodes/node_geo_input_shortest_edge_paths.cc index 1b45701a157..194acc7eebb 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_input_shortest_edge_paths.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_input_shortest_edge_paths.cc @@ -54,7 +54,7 @@ static void shortest_paths(const Mesh &mesh, visited[vert_i] = true; for (const int edge_i : vert_to_edge[vert_i]) { const int2 &edge = edges[edge_i]; - const int neighbor_vert_i = edge[0] + edge[1] - vert_i; + const int neighbor_vert_i = bke::mesh::edge_other_vert(edge, vert_i); if (visited[neighbor_vert_i]) { continue; }