Skip to content

Conversation

@capdevon
Copy link
Contributor

@capdevon capdevon commented Jun 17, 2025

Reduce object allocations when ALAudioRenderer update listener each time the Camera is moved:

This pull request refactors the Listener class in the jMonkeyEngine audio system. The main changes include initializing the location, velocity, and rotation fields at declaration instead of in the constructor, removing redundant constructor assignments, and introducing reusable Vector3f instances (left, up, direction) for direction calculations.

Methods that return direction vectors now store results in these fields rather than creating new objects, improving memory efficiency and reducing unnecessary allocations. Overall, the changes streamline object management and optimize performance in the Listener class.

https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-core/src/main/java/com/jme3/audio/openal/ALAudioRenderer.java#L768

    private void applyListenerRotation(Listener listener) {
        Vector3f dir = listener.getDirection(); // <--
        Vector3f up = listener.getUp(); // <--
        // Use the shared FloatBuffer fb
        fb.rewind();
        fb.put(dir.x).put(dir.y).put(dir.z);
        fb.put(up.x).put(up.y).put(up.z);
        fb.flip();
        al.alListener(AL_ORIENTATION, fb);
    }

@yaRnMcDonuts yaRnMcDonuts added this to the v3.9.0 milestone Jun 19, 2025
@codex128 codex128 added the Refactoring and Cleanup This PR is all about improving code quality and javadoc. label Dec 22, 2025
@codex128 codex128 closed this Dec 22, 2025
@riccardobl riccardobl reopened this Jan 13, 2026
@riccardobl riccardobl added defect Something that is supposed to work, but doesn't. Less severe than a "bug" and removed Refactoring and Cleanup This PR is all about improving code quality and javadoc. labels Jan 13, 2026
@riccardobl riccardobl self-assigned this Jan 13, 2026
@riccardobl
Copy link
Member

riccardobl commented Jan 13, 2026

I've reverted the changes to the existing getters. Since the listener isn't only an internal API, changing their behavior could break existing code, and because the defect isn't severe, it isn't worth risking hard-to-debug regressions for developers, imo.

Instead, I added new getters that populate the passed store object (a pattern used frequently elsewhere in jME), and updated the Audio Renderer to use these new getters.

I think this is a good resolution.

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

Labels

defect Something that is supposed to work, but doesn't. Less severe than a "bug"

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants