perf: reuse Vector3f/Quaternionf between game ticks
I noticed that the `AudioSystem` creates new instances for player position, velocity, and view rotation with every game tick.
It can just reuse the same instance between game ticks, saving on unnecessary instance creation.
@In
private AudioManager audioManager;
+ // reuse same vector and quaternion instances when updating listeners during delta update.
+ private final Vector3f playerPosition = new Vector3f();
+ private final Vector3f playerVelocity = new Vector3f();
+ private final Quaternionf playerViewRotation = new Quaternionf();
+
/**
* Toggles the muting of sounds.
*
@@ -60,10 +65,11 @@ public String mute(@Sender EntityRef sender) {
* @param zOffset The z axis offset from the player to play the sound at.
*/
@Command(shortDescription = "Plays a test sound")
- public void playTestSound(@Sender EntityRef sender, @CommandParam("xOffset") float xOffset, @CommandParam("zOffset") float zOffset) {
+ public void playTestSound(@Sender EntityRef sender,
+ @CommandParam("xOffset") float xOffset,
+ @CommandParam("zOffset") float zOffset) {
Vector3f position = localPlayer.getPosition(new Vector3f());
- position.x += xOffset;
- position.z += zOffset;
+ position.add(xOffset, 0, zOffset);
audioManager.playSound(Assets.getSound("engine:dig").get(), position);
}
@@ -107,8 +113,8 @@ public void onPlaySound(PlaySoundForOwnerEvent playSoundEvent, EntityRef entity)
@Override
public void update(float delta) {
audioManager.updateListener(
- localPlayer.getPosition(new Vector3f()),
- localPlayer.getViewRotation(new Quaternionf()),
- localPlayer.getVelocity(new Vector3f()));
+ localPlayer.getPosition(playerPosition),
+ localPlayer.getViewRotation(playerViewRotation),
+ localPlayer.getVelocity(playerVelocity));
}
}
