Skip to content

optimize-movement-vector-normalization#610

Merged
Dreeam-qwq merged 4 commits intoWinds-Studio:ver/1.21.11from
VeVeVeVel:optimize-movement-vector-normalization
Feb 19, 2026
Merged

optimize-movement-vector-normalization#610
Dreeam-qwq merged 4 commits intoWinds-Studio:ver/1.21.11from
VeVeVeVel:optimize-movement-vector-normalization

Conversation

@VeVeVeVel
Copy link
Contributor

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.

@Dreeam-qwq Dreeam-qwq added the type: optimization Pull request for optimization label Feb 5, 2026
@VeVeVeVel
Copy link
Contributor 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

@Dreeam-qwq Dreeam-qwq merged commit 0207e47 into Winds-Studio:ver/1.21.11 Feb 19, 2026
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