Miho Performance Hediff Fix
Description
Got nerd sniped on Dub's discord to fix Miho mod's custom CompTick issue. I don't even use Miho personally,.
Replaces Miho's multiple ThingComps with one aggregate ThingComp and make it tick less as well as some optimizations in Hediff matching. Screenshot from this mod should show the improvement from this change. Those numbers were tested with 25+ Miho pawns.
Technically should not be called a "Hediff Fix" since the main issue is not Hediffs, but I named this before I started looking deeper into the code so the name will stay.
Technical Information
Miho mod has a list of 10 Hediffs that control some stuff in the mod. This includes the Hediffs for its main drug, the hidden hediffs for each of its psycaster classes, the Hediff for no classes, and some other junk I don't remember. For each of these Hediffs, the mod will spawn a ThingComp on each Miho pawn. That means each Miho pawn is attached with 10 ThingComps. Each ThingComp will tick every single tick and iterate through the pawn's Hediff list to check if its respective Hediff it is tracking is in that list. This mean that for every Miho pawn, there are 10 ThingComps, all ticking every tick scanning Hediff lists. This is why that Tick is so expensive.
This mod fixes this issue by deleting all 10 ThingComps from all Miho pawns. It then also completely skips the Tick function of the original ThingComp for good measure. Then it adds 1 custom ThingComp that checks for all of the Hediffs. This custom ThingComp iterates through the Hediff list once, and checks each Hediff on the list against a dictionary of the 10 Hediffs for membership (this is a O(1) check), if a hit is found, stuff is done like the original mod. This is ran every 60 ticks instead of every tick.
During furthur testing, the custom comp was hitting ~0.001ms on 25+ Miho pawns at about 120fps/900tps on my computer. You can check in the analyzer in the Tick->ThingComp section. You can see the difference in the screenshot provided.
Additional Note About Miho Hediffs and Performance
Making this has made me look deeper into Miho's code then I would have liked. Miho depends heavily on Hediffs for a lot of things, including its pyscaster class progression system. The way it is done is basically use Hediffs as a very bad state machine to manage current job state, including spamming the Hediff list with a ton of hidden Hediffs to block out multiple pyscaster classes from being picked up. The mod also adds a ton of Hediffs for apparel stats. Most of these behaviors and features [b]do not[/b] need to use Hediffs, they can be moved into other systems that are more robust and preformat. I am not touching those in this mod since this would require a fundamental reworking of the mod's logic, and it is outside the scope of this mod.
Compatibility
[b][url=https://steamcommunity.com/sharedfiles/filedetails/?id=3703738952]Miho Hediff Performance Optimization Sub-mod[/url][/b]
Not compatible. This was actually the mod that spawned the discussion on Dub's discord that lead to me making my mod. This mod throttles the original ThingComp's tickrate. There are still 10 Comps ticking. My mod completely removes the original ThingComps at startup and adds an additional block to the Tick function as a safeguard, so this mod will not run either way. This mod also adds some notify checks for apparel Hediffs, but in testing does not seem to do anything and does not seem to have any measurable performance improvements. Technically soft incompatibility as both mods modify the same space, but they both can run at the same time I believe. Not tested and would not recommend though.
FAQ
Q: Can I add this mid save?
Yes
Q: Can I remove this mid save?
Yes
Links
[url=https%3A%2F%2Fgithub.com%2FZadesSC%2FMiho-Hediff-Fix]Github[/url][github.com]