Skip to content

Reduce object allocations in MotionEvent's direction computation - #2447

Merged
yaRnMcDonuts merged 2 commits into
jMonkeyEngine:masterfrom
capdevon:capdevon-MotionEvent
Jun 15, 2025
Merged

Reduce object allocations in MotionEvent's direction computation#2447
yaRnMcDonuts merged 2 commits into
jMonkeyEngine:masterfrom
capdevon:capdevon-MotionEvent

Conversation

@capdevon

Copy link
Copy Markdown
Contributor

This PR introduces a minor but impactful performance optimization within the MotionEvent class.

Previously, the computeTargetDirection() method repeatedly created new Quaternion objects (q and q2) on each call when handling Path and PathAndRotation direction types. This constant object allocation can lead to unnecessary garbage collection overhead, particularly in performance-critical game loops or animations.

To address this, I've refactored the method to utilize a single, reusable Quaternion class member variable (tempRotation). By reusing this existing object, we significantly reduce memory allocations and subsequent garbage collection cycles, resulting in a modest but noticeable performance improvement, especially when many MotionEvents are active or when the method is called frequently.

@yaRnMcDonuts yaRnMcDonuts added this to the v3.9.0 milestone May 22, 2025
Comment thread jme3-core/src/main/java/com/jme3/cinematic/events/MotionEvent.java
@yaRnMcDonuts
yaRnMcDonuts merged commit 6b143d7 into jMonkeyEngine:master Jun 15, 2025
@capdevon
capdevon deleted the capdevon-MotionEvent branch June 30, 2025 10:10
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.

3 participants