Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Optimize AudioServer::_driver_process() #63430

Merged
merged 1 commit into from
Aug 8, 2022

Conversation

lawnjelly
Copy link
Member

@lawnjelly lawnjelly commented Jul 25, 2022

Move expensive calculations outside inner hot loops.

Notes

  • Profiling reveals audio is now the biggest CPU user in the editor (when using vital_redraws_only mode). While turning off the audio processing completely is the most effective option for the editor, I noticed some low hanging fruit in the hot spots, which could also be useful in running games.
  • This change is 8.69 / 2.61 = 3.3x faster for the AudioServer::_driver_process() in the empty case, and should also be significantly faster with active audio.
  • I also tried using memset(), but bizarrely profiling revealed it was approximately equal in speed to the old code, and wasn't as fast as the new approach here. This may be system dependent, but without evidence of a benefit to using memset() I've left it out for now. It might be more advantageous with larger buffer sizes.
  • Reducing audio processing cost means less load for batteries on mobile / laptops, and more CPU for other game processing on desktop, so seems well worth doing.
  • Similar optimizations could be possible in 4.x, I haven't looked at the audio processing there yet.

Further Discussion

As well as changes here, it looks like most important factor for reducing CPU in editor would either be to just turn off audio, or to sleep the AudioDriver (for longer, as it looks like it is slept for 1ms when there is no audio), or to close PulseAudio (or whatever platform equivalent) after some silence, as suggested here: #45948 (comment)
The wider problem is that we can optimize the code in the audio threads as much as we want, but if they just spin, it will just do more iterations, so ultimately we probably need to sleep / sleep more / pause threads.

Move expensive calculations outside inner hot loops.
@lawnjelly lawnjelly requested a review from a team as a code owner July 25, 2022 10:20
@lawnjelly lawnjelly added this to the 3.x milestone Jul 25, 2022
@akien-mga akien-mga modified the milestones: 3.x, 3.5, 3.6 Aug 8, 2022
@akien-mga akien-mga merged commit ee3bad3 into godotengine:3.x Aug 8, 2022
@akien-mga
Copy link
Member

Thanks!

@akien-mga akien-mga changed the title Optimize AudioServer::_driver_process() Optimize AudioServer::_driver_process() Jan 25, 2024
@akien-mga akien-mga changed the title Optimize AudioServer::_driver_process() [3.x] Optimize AudioServer::_driver_process() Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants