Skip to content

Defer work in vessel modules until the data is actually needed#120

Open
Phantomical wants to merge 2 commits intopost-kerbin-mining-corporation:masterfrom
Phantomical:deferred-update
Open

Defer work in vessel modules until the data is actually needed#120
Phantomical wants to merge 2 commits intopost-kerbin-mining-corporation:masterfrom
Phantomical:deferred-update

Conversation

@Phantomical
Copy link

DBS has shown up in my profiles for a while in cases where a vessel gets modified (crashing, launch clamps decoupling, etc.) For large ships DBS can easily account for 15-20s of a 50s frame. This PR improves that from 15-20s to sub-100ms for an 1000 part ship that is in the process of exploding.

The main changes here are:

  • VesselDataManager and ModuleDynamicBatteryStorage no longer refresh their internal data in onVesselWasModified. Instead, they invalidate their current data and recalculate either the next time it is needed, or on the next FixedUpdate, respectively.
  • The refresh event handlers for those same vessel modules now only run if the event is for their vessel, instead of running any time that event is fired. This brings down the amount of work that needs to be done to O(n) instead of O(n^2).

There are also a bunch of other minor changes:

  • Both modules now use ShouldBeActive to disable themselves when there is nothing for them to do.
  • Adding/removing event handlers has been moved to OnEnable/OnDisable to account for this.
  • VesselDataManager no longer has a FixedUpdate method. Instead, the vessel data is computed when it is requested.
  • Some of the string formats have been moved behind an explicit check that their corresponding log method is enabled, to save some allocations.

Fixes #119

For some performance numbers, here's what the dottrace profile looked like before these changes (ignore total times, only the relative sizes mean anything)
image

And here's what it looks like after. DBS has completely disappeared from the profile. (Again, ignore total times only relative sizes mean anything here)
image

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.

VesselDataManager.RefreshVesselData causes extreme lag when parts are decoupled, destroyed, or otherwise broken off a vessel

1 participant