erdelf / AlienRaces

Rimworld mod alien race framework
MIT License
103 stars 69 forks source link

Performance improvements for CanGetThought #99

Open bbradson opened 1 year ago

bbradson commented 1 year ago

There's been a couple of reports of HarmonyPatches.CanGetThoughtPostfix acting up with a high performance hit displayed in Dub's analyzer when used with races containing thought replacers, like Rim Robots (https://steamcommunity.com/sharedfiles/filedetails/?id=2743171832)

The most recent screenshot I've seen would be this: https://cdn.discordapp.com/attachments/708435078614614116/1135651082295574528/image.png

The performance improvement here is achieved by lazily caching results in a dictionary after the first lookup and keeping them there as they're not expected to change with arguments only being defs. I've changed arguments for the original method to defs and added another overload matching the old arguments to maintain compatibility with potential patches from other mods. The short hashes used for indexing get created within Verse.ShortHashGiver and are guaranteed to be unique within the DefDatabase of their type and its subclasses.

The new performance hit looks something like this: https://cdn.discordapp.com/attachments/708435078614614116/1135702317933527040/Screenshot_2247.png