Skip to content

perf: Optimize RocketSoundEnhancement.LateUpdate by patching AudioSource ctor#42

Merged
JonnyOThan merged 1 commit intoKSPModStewards:masterfrom
Phantomical:optimize-late-update
Nov 8, 2025
Merged

perf: Optimize RocketSoundEnhancement.LateUpdate by patching AudioSource ctor#42
JonnyOThan merged 1 commit intoKSPModStewards:masterfrom
Phantomical:optimize-late-update

Conversation

@Phantomical
Copy link
Contributor

Every frame, RocketSoundEnhancement makes a FindObjectsOfType call in order to discover any new AudioSources that have been created in the last frame. This is slow.

This commit does away with that by:

  • Patching AudioSource::.ctor to add the newly created AudioSource object to a list (if there is currently an instance of RocketSoundEnhancement active).
  • Processing the new items in the list in LateUpdate instead of checking all available items.

There might be some further simplifications that can be done here now that we know that we're only processing new AudioSources. None of them would have an effect on performance though, so I have left them in just to be safe.

In my testing this change reduces the time spent in RSE.LateUpdate from 4.4s to 90ms over a 253s launch to orbit.

In addition to the optimization itself, I have also:

  • Added Harmony2 as a dependency in the csproj
  • Updated CI to install harmony2

Every frame, RocketSoundEnhancement makes a FindObjectsOfType call in
order to discover any new AudioSources that have been created in the
last frame. This is slow.

This commit does away with that by:
- Patching AudioSource::.ctor to add the newly created AudioSource
  object to a list (if there is currently an instance of
  RocketSoundEnhancement active).
- Processing the new items in the list in LateUpdate instead of checking
  all available items.

There might be some further simplifications that can be done here now
that we know that we're only processing new AudioSources. None of them
would have an effect on performance though, so I have left them in just
to be safe.

In my testing this change reduces the time spent in RSE.LateUpdate from
4.4s to 90ms over a 253s launch to orbit.
build:
uses: KSPModdingLibs/KSPBuildTools/.github/workflows/build.yml@0.0.4
with:
dependency-identifiers: Harmony2
Copy link
Member

Choose a reason for hiding this comment

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

This should be use-ckan: true and set the properties appropriately in the csproj


// This makes sure things still work even if a new audio source ends
// up being created while we are running.
source = newAudioSources[newAudioSources.Count - 1];
Copy link
Member

@JonnyOThan JonnyOThan Nov 8, 2025

Choose a reason for hiding this comment

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

Maybe should check if the source is destroyed here and only return non-destroyed ones?

Oh never mind I thought you might be spreading them out over several frames.

{
AudioSource source = (AudioSource)sourceObj;
if (source != null) source.outputAudioMixerGroup = null;
if (source != null)
Copy link
Member

Choose a reason for hiding this comment

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

There is a faster null check, see KSPCF

@JonnyOThan JonnyOThan merged commit 1798013 into KSPModStewards:master Nov 8, 2025
1 check passed
@Phantomical Phantomical deleted the optimize-late-update branch November 8, 2025 09:09
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.

2 participants