Optimize BoundingBox distanceToEdge math#2749
Conversation
Avoid TempVars and temporary Vector3f mutation in the point distance path by computing axis deltas directly. Adds direct edge-case coverage for inside, surface, single-axis outside, and multi-axis outside points. Measured locally with BoundingVolumeBenchmark: inside/outside points improved from about 23 ns/op to about 2.1/2.5 ns/op.
There was a problem hiding this comment.
Code Review
This pull request refactors the distanceToEdge method in BoundingBox to improve performance by eliminating TempVars usage and vector projections in favor of direct coordinate calculations. It also introduces a new test suite in TestBoundingBox to validate the distance logic for points both inside and outside the box. Review feedback recommends an additional optimization to return early when the squared distance is zero, thereby skipping the square root operation for points located within the bounding box.
maybe we can add an additional jme3-benchmarks module that is not included by default in normal builds, this way we avoid increasing the build time |
Avoid TempVars and temporary Vector3f mutation in the point distance path by computing axis deltas directly.
Adds direct edge-case coverage for inside, surface, single-axis outside, and multi-axis outside points.
Measured a perf improvement with a jmh microbenchmark. See BoundingVolumeBenchmark.java on my branch that adds some jmh benchmarks to jmonkey: 8Keep@195e376#diff-cff928ca242470aac884e59988007461fc7415aa1c16d5484a249485b38c0d46
~10x speedup for this
Whether we want to try to actually land some jmh benchmarks into the repo is a good question, and if anyone would like my jme benchmarks I'm working on to be included, I can, but I figured it's best to split out the result of that into commits like this one.
Also add some tests for this function.