Skip to content

perf: Speed up texture replacment#1

Merged
Phantomical merged 1 commit intoKSPModStewards:masterfrom
Phantomical:optimize-replace-textures-2
Nov 8, 2025
Merged

perf: Speed up texture replacment#1
Phantomical merged 1 commit intoKSPModStewards:masterfrom
Phantomical:optimize-replace-textures-2

Conversation

@Phantomical
Copy link
Collaborator

When I profile my save with HUDReplacer installed (+ZTheme) it takes about 1.5s as part of every scene switch. Most of that time was spent iterating over the images dictionary again and again for every loaded texture.

This commit gets the overhead of HUDReplacer down to ~140ms (on my save).

I'm not sure if this entirely fixes issue UltraJohn#7, since there are still things that can be improved. It does make things drastically better though.

There are a bunch of linked changes here, most of which are needed in order to make the whole thing work. I'll walk through these one by one.

  1. The first thing is an overhaul of how the replacement info is stored. There are now two dictionaries: a global set of replacements, and a set of per-scene replacement dictionaries. These are keyed off of the image name, and have a list of all the available replacements for that image name, ordered by priority.

    This means that we can parse all the available texture replacements at startup and then not have to worry about needing to parse them again.

    We also pre-parse the texture filenames into their basename and size, if available. This means that we can actually just look them up by name, instead of having to search everything.

    As a bonus, I have added a ModuleManagerPostLoad call to load the textures so that it happens during the loading screen, instead of during the black screen when switching to the main screen.

  2. Next, I have rewritten ReplaceTextures to take advantage of the new dictionary. There's a bit of extra work, because we need to deal with the fact that the replacements could possibly be either in the global dictionary or the scene-specific one. This all gets wrapped up in the GetMatchingReplacement methods. I have used the priority I saved from the first step to disambiguate. If that doesn't I just pick the scene specific one.

    This is a small behaviour change but I don't think it will matter in practice.

  3. Caching. With the two changes above the runtime goes down to about 300ms - which is split 50/50 between reading textures off disk and loading them into a Texture2D. To speed this up I added a cachedTextureBytes field to ReplacementInfo. This makes it so the disk read can be avoided the next time the replacement is used.

I have also made a couple of small changes and/or bugfixes as I was going through:

  • LoadTextures now emits useful error messages if the config node is invalid or if the format of the file name is wrong, instead of crashing.
  • ReplaceTextures now picks the texture with the highest priority, instead of the one with the lowest.

Flame Graphs

Before. Note that HUDReplacer.Awake takes 1.5s here.
image

After. Now it takes 140ms.
image

When I profile my save with HUDReplacer installed (+ZTheme) it takes
about 1.5s as part of every scene switch. Most of that time was spent
iterating over the `images` dictionary again and again for every loaded
texture.

This commit gets the overhead of HUDReplacer down to ~130ms (on my
save).

There are a bunch of linked changes here, most of which are needed in
order to make the whole thing work. I'll walk through these one by one.

1. The first thing is an overhaul of how the replacement info is stored.
   There are now two dictionaries: a global set of replacements, and a
   set of per-scene replacement dictionaries. These are keyed off of the
   image name, and have a list of all the available replacements for
   that image name, ordered by priority.

   This means that we can parse all the available texture replacements
   at startup and then not have to worry about needing to parse them
   again.

   We also pre-parse the texture filenames into their basename and size,
   if available. This means that we can actually just look them up by
   name, instead of having to search everything.

   As a bonus, I have added a ModuleManagerPostLoad call to load the
   textures so that it happens during the loading screen, instead of
   during the black screen when switching to the main screen.

2. Next, I have rewritten ReplaceTextures to take advantage of the new
   dictionary. There's a bit of extra work, because we need to deal with
   the fact that the replacements could possibly be either in the global
   dictionary or the scene-specific one. This all gets wrapped up in the
   GetMatchingReplacement methods. I have used the priority I saved from
   the first step to disambiguate. If that doesn't I just pick the scene
   specific one.

   This is a small behaviour change but I don't think it will matter in
   practice.

3. Caching. With the two changes above the runtime goes down to about
   300ms - which is split 50/50 between reading textures off disk and
   loading them into a Texture2D. To speed this up I added a
   cachedTextureBytes field to ReplacementInfo. This makes it so the
   disk read can be avoided the next time the replacement is used.

I have also made a couple of small changes and/or bugfixes as I was
going through:
- LoadTextures now emits useful error messages if the config node is
  invalid or if the format of the file name is wrong, instead of
  crashing.
- ReplaceTextures now picks the texture with the highest priority,
  instead of the one with the lowest.
@Phantomical Phantomical force-pushed the optimize-replace-textures-2 branch from bf4def5 to 75c0ae3 Compare November 8, 2025 23:44
@Phantomical Phantomical merged commit dc6c060 into KSPModStewards:master Nov 8, 2025
1 check passed
@Phantomical Phantomical deleted the optimize-replace-textures-2 branch November 8, 2025 23:45
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.

1 participant