Skip to content

Conversation

klianc09
Copy link
Contributor

Hi, after some profiling found that this library produces quite some garbage every frame/update.

  • so what I did is to cache ControllerButton.values() and ControllerAxis.values() for the update methods, since those were creating new arrays all the time. (some JVMs might be able to correctly optimize this, but the one I profiled did not)
  • also the line axisState.put(id, value); was causing the values to be boxed to new Floats every update, so I introduced a simple wrapper class with a mutable float, which does not create any garbage

This only covers the desktop version, similar optimizations could be made for other platforms probably.

IDK if this can be merged as-is, but would love to hear about any suggestions or doubts.

cache ControllerButton.values() and ControllerAxis.values() to prevent creating garbage
also axis values are no longer boxed into Float objects every update
@@ -164,7 +167,7 @@ private void updateButtonsState() {

private void initializeState() {
for (ControllerAxis axis : ControllerAxis.values()) {
Copy link
Member

Choose a reason for hiding this comment

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

I know this is only called once, but is there a reason this does not use CONTROLLER_AXIS_VALUES?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly, I was only concerned with the update method. Since this is only called once, it doesn't matter here. I'll happily change it to what you prefer.

Copy link
Member

Choose a reason for hiding this comment

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

I think it is better to use the new constant, otherwise code readers may ask why it is used only at some places.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright. Won't get to it this weekend, but will implement the changes after the weekend.

also use the constants instead of ControllerButton.values() consistently throughout the file
@klianc09
Copy link
Contributor Author

Forgot about this one, sorry, but finally made the changes.

@Berstanio Berstanio mentioned this pull request Jun 4, 2025
@MrStahlfelge MrStahlfelge merged commit 6dbf61c into libgdx:master Jun 5, 2025
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