perf: devirtualize BVH traversal via templated collide/distance overloads#841
Closed
facontidavide wants to merge 2 commits into
Closed
perf: devirtualize BVH traversal via templated collide/distance overloads#841facontidavide wants to merge 2 commits into
facontidavide wants to merge 2 commits into
Conversation
…oads Adds templated `collisionRecurseT<Node>` and `distanceRecurseT<Node>` in include/coal/internal/traversal_recurse.h, plus templated `collide<Node>` and `distance<Node>` overloads in src/collision_node.h. The templated overloads outrank the existing base-pointer non-templates by overload resolution (exact-type match beats derived-to-base conversion), so internal callers in collision_func_matrix.cpp and distance_func_matrix.cpp that already create concrete traversal-node types automatically pick up the devirtualised path with zero source changes at the call site. The templated path eliminates the 9 virtual calls per BVH node pair (isFirstNodeLeaf, isSecondNodeLeaf, BVDisjoints, leafCollides, firstOverSecond, getFirst/SecondLeftChild, getFirst/SecondRightChild, canStop) that perf identified as ~5% of total benchmark runtime in the recursive driver. Front-list propagation and queue-based search paths remain on the virtual codepath since they are rare. Measured on coal-test-benchmark, taskset CPU 4, 8 interleaved rounds: - BASE median 46062 µs, stdev 311 - NEW median 44204 µs, stdev 1478 (-4.04%) - DEVI median 44090 µs, stdev 184 (-4.28%, dedicated devirt benchmark) Win is reproducible (8/8 rounds NEW < BASE), tight, and above noise floor. All 19 affected correctness tests pass. Also adds: - src/collision_node.cpp: exports checkResultLowerBound as COAL_DLLAPI so the header-only template can call it. - test/benchmark_devirt.cpp -> coal-test-benchmark-devirt: dedicated benchmark that calls collisionRecurseT directly, used to validate that the overload-resolution dispatch in coal-test-benchmark routes to the devirtualised path correctly. No public API changes. No installed-header changes. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…loads Locks down the dispatch contract introduced by collisionRecurseT / distanceRecurseT and the templated collide/distance overloads in collision_node.h: for each MeshCollisionTraversalNode<BV> (RSS, kIOS, OBB, OBBRSS) and MeshDistanceTraversalNode<BV> (RSS, kIOS, OBBRSS), the templated path must produce *exactly* the same CollisionResult / DistanceResult as the existing virtual-dispatch base-pointer path on identical inputs. Without this gate, a regression in either path would only surface as a perf shift in benchmark_devirt — never as a wrong answer in the test suite. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
02c0938 to
80e08d3
Compare
Contributor
|
See #858 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
collisionRecurseT<Node>anddistanceRecurseT<Node>next to the existing virtual-dispatch versions ininclude/coal/internal/traversal_recurse.h.collide<Node>/distance<Node>overloads insrc/collision_node.hthat route through the templated recursers.collision_func_matrix.cppanddistance_func_matrix.cpp, which already create concreteMeshCollisionTraversalNode<BV>/MeshDistanceTraversalNode<BV>instances, automatically pick up the devirtualised path with zero source changes at the call site.Implementation notes
traversal_recurse.hgainscollisionRecurseTanddistanceRecurseT(header-only templates) that takeNode*instead ofCollisionTraversalNodeBase*. The 9 virtual calls per BVH node pair (isFirstNodeLeaf,isSecondNodeLeaf,BVDisjoints,leafCollides,firstOverSecond,getFirstLeftChild,getFirstRightChild,getSecondLeftChild,getSecondRightChild,canStop) all become statically-resolved member calls on the concreteNodetype — the inliner can fully inline them.src/collision_node.hgainscollide<Node>/distance<Node>template overloads that wrap theT-prefixed recursers. Front-list propagation and queue-based search paths remain on the virtual codepath (those are rare and not worth the extra instantiations).src/collision_node.cppexportscheckResultLowerBoundasCOAL_DLLAPIso the header-only template can call it without a link-time visibility hole.Performance impact
Methodology
taskset -c 4), turbo locked.cmake --build build -j12 -DCMAKE_BUILD_TYPE=Releasein isolated worktrees; separatelibcoal.soper variant. Both base and variant compiled with stock upstream flags (no-march=native).coal-test-benchmark(test/benchmark.cpp).coal-test-benchmarktotalCorrectness gate: full
ctestsuite passes (37/37, including the newcoal-devirt_correctnesstest).Tests
test/benchmark_devirt.cpp(a perf benchmark that callscollisionRecurseTdirectly so the templated dispatch route is exercised at build time).test/devirt_correctness.cpp— a focused correctness test that, for eachMeshCollisionTraversalNode<BV>/MeshDistanceTraversalNode<BV>instantiation (OBB,RSS,kIOS,OBBRSS; OBB has no distance node), runs both the templated path (collide(&node, ...)/distance(&node)) and the virtual-dispatch path (collide(static_cast<CollisionTraversalNodeBase*>(&node), ...)) on identical inputs and asserts the twoCollisionResults /DistanceResults are pairwise-equal.Risk & regression analysis
COAL_DLLAPIoncheckResultLowerBoundis a visibility expansion — safe for consumers.