Skip to content

optimize-movement-vector-normalization#610

Open
VeVeVeVel wants to merge 3 commits intoWinds-Studio:ver/1.21.11from
VeVeVeVel:optimize-movement-vector-normalization
Open

optimize-movement-vector-normalization#610
VeVeVeVel wants to merge 3 commits intoWinds-Studio:ver/1.21.11from
VeVeVeVel:optimize-movement-vector-normalization

Conversation

@VeVeVeVel
Copy link

PR Description: Optimize movement vector normalization

Summary

This PR optimizes the movement vector calculation by avoiding redundant Math.min() and normalize() operations when the squared length of the vector is already within known bounds.

Problem

The previous implementation always calculated the scalar length and normalized the vector, even if the vector's length was already less than or equal to the maximum threshold ($8.0$). Since vec3.length() involves a square root operation ($\sqrt{x}$), performing it unnecessarily can impact performance in high-frequency movement logic.

Changes

I have replaced the unconditional normalization with a conditional check based on the squared length ():

  • If ($d > 64.0$ ($8^2$)): The vector exceeds the maximum distance. We normalize and scale it to .
  • If $d \leq 64.0$: The vector is already within the limit. We can use the original vec3 directly, skipping the length(), normalize(), and scale() calls entirely.

Impact

  • Performance: Reduces CPU cycles by skipping square root and division operations for movement vectors with a magnitude .
  • Logic: Maintains identical behavior to the original code but with better execution efficiency.

- Vec3 vec31 = this.position().add(vec3.normalize().scale(min));
+ // Leaf start - optimize movement vector normalization
+ //double min = Math.min(vec3.length(), 8.0);
+ //Vec3 vec31 = this.position().add(vec3.normalize().scale(min));
Copy link
Member

Choose a reason for hiding this comment

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

The concept looks great. However It's better to directly replace the original code instead of commenting them, the patch already helps keep the original code part.

And I think we can inline vec3's normalize() and scale(8.0) call below, to avoid unnecessary new Vec3 instance creation and useless calls.
Make sure also open a pull request to upload a benchmark to SunBox, since it's the math-related micro optimization.

@Dreeam-qwq Dreeam-qwq added the type: optimization Pull request for optimization label Feb 5, 2026
@VeVeVeVel
Copy link
Author

@Dreeam-qwq I finished it.
Small Case: 40% faster
Large Case: 20% faster

Benchmark                            Mode  Cnt  Score   Error  Units
EntityFallClipVector.largeOptimized  avgt   10  6.550 ± 0.037  ns/op
EntityFallClipVector.largeVanilla    avgt   10  8.263 ± 0.048  ns/op
EntityFallClipVector.smallOptimized  avgt   10  4.946 ± 0.029  ns/op
EntityFallClipVector.smallVanilla    avgt   10  8.267 ± 0.178  ns/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: optimization Pull request for optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants