Skip to content

Optimize BoundingBox distanceToEdge math#2749

Merged
riccardobl merged 1 commit into
jMonkeyEngine:masterfrom
8Keep:performance-boundingbox-distance
May 9, 2026
Merged

Optimize BoundingBox distanceToEdge math#2749
riccardobl merged 1 commit into
jMonkeyEngine:masterfrom
8Keep:performance-boundingbox-distance

Conversation

@8Keep
Copy link
Copy Markdown
Contributor

@8Keep 8Keep commented May 9, 2026

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

  • before: about 23 ns/op
  • optimized inside point: 2.090 ns/op
  • optimized outside point: 2.524 ns/op

~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.

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.
@8Keep 8Keep changed the title Optimize BoundingBox distanceToEdge scalar math Optimize BoundingBox distanceToEdge math May 9, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread jme3-core/src/main/java/com/jme3/bounding/BoundingBox.java
@8Keep 8Keep marked this pull request as ready for review May 9, 2026 03:47
@riccardobl riccardobl merged commit f629be3 into jMonkeyEngine:master May 9, 2026
13 checks passed
@riccardobl
Copy link
Copy Markdown
Member

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.

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

@8Keep 8Keep deleted the performance-boundingbox-distance branch May 15, 2026 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants