Add methods for vessel-wide PartModule lookups - #425
Open
siimav wants to merge 2 commits into
Open
Conversation
Unlike stock counterparts, these have caching!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds the vessel-level equivalent of the existing per-part
FindModuleImplementingFast<T>()helpers. StockVessel.FindPartModuleImplementing<T>()/FindPartModulesImplementing<T>()walk every module of every part on every call and cache nothing.New extension methods on
Vessel:FindPartModuleImplementingFast<T>()HasPartModuleImplementingFast<T>()FindPartModulesImplementingReadOnly<T>()Results are cached per vessel and per type — including "there is no such module on this vessel", the expensive case stock never memoizes. The cache is a component on the vessel GameObject, so it can't outlive the vessel and needs no static registry. It is invalidated by:
GameEvents.onVesselStandardModification,vessel.parts.Countcheck on every access, for the paths that mutatevessel.partswithout firing anything (
ProtoVessel.Load()and friends),Part.ClearModuleReferenceCache()postfix, which stock calls from everyPartModuleListmutator, for modules added/removed at runtime.Second commit converts AsteroidAndCometDrillCache to use it.
IsSituationValid()(called everyOnUpdate) andGetAttachedPotato()no longer fall back to a full vessel scan when theModuleResourceHarvestercache they previously piggybacked on is stale or missing, and the drill part no longer needs a harvester module at all.This only pays off for call sites polled repeatedly between vessel modifications; a lookup made from an
onVesselWasModifiedhandler is always cold and gains nothing. So at a glance there doesn't appear to be good other candidates for using the new Fast methods in stock code.For testing I have used the AsteroidAndCometDrillCache patch and had validation in code that compared the results against fresh part and partmodule loops. Slipping that in required some rather ugly code changes so probably best not to push those.