Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlightIntegrator and friends micro-optimization Round 2 #257

Merged
merged 16 commits into from
Oct 12, 2024
Merged

Conversation

JonnyOThan
Copy link
Contributor

Many experimental perf improvements, basically a big collection of micro-optimizations for various pieces of code running every frame on every part. Won't do much in low part count situations, and some patches are highly situational (atmospheric flight, docking ports, struts, particles repositionning on floating origin shifts...)

Patches affecting the FlightIntegrator and VesselPrecalculate are broably incompatible with mods trying to override/extend stuff through MFI. There are a bunch of perf issues with "end of the call stack" methods being inefficient because they do extra work that is already done at the call site or can be made faster by using state from the call site, so patching those methods doesn't cut it, but this mean many stock methods aren't called anymore, so if those methods are replaced/prefixed/postfixed with MFI, things will break. Notable examples are FI.UpdateAerodynamics() and FI.Integrate(Part). This could probably be avoided at the cost of some of the perf gains, but I wanted to see how far things could be pushed perf wise. On a side note, there is a bug lurking in the VesselPrecalculate.CalculatePhysicsStats reimplementation, not sure what it is exactely but a side effect is camera target being wrongly offset. Likely something with the CoM calcs.

gotmachine and others added 5 commits June 3, 2024 13:24
…nly relevant in large part count situations. More can be done : I didn't touch yet stuff specific to aero situations (drag, conduction occlusion...) and there is probably something to be done about Integrate()
…cro-optimizations for various pieces of code running every frame on every part. Won't do much in low part count situations, and some patches are highly situational (atmospheric flight, docking ports, struts, particles repositionning on floating origin shifts...)

Patches affecting the FlightIntegrator and VesselPrecalculate are broably incompatible with mods trying to override/extend stuff through MFI. There are a bunch of perf issues with "end of the call stack" methods being inefficient because they do extra work that is already done at the call site or can be made faster by using state from the call site, so patching those methods doesn't cut it, but this mean many stock methods aren't called anymore, so if those methods are replaced/prefixed/postfixed with MFI, things will break. Notable examples are FI.UpdateAerodynamics() and FI.Integrate(Part). This could probably be avoided at the cost of some of the perf gains, but I wanted to see how far things could be pushed perf wise.
On a side note, there is a bug lurking in the VesselPrecalculate.CalculatePhysicsStats reimplementation, not sure what it is exactely but a side effect is camera target being wrongly offset. Likely something with the CoM calcs.
-most notably this caused the camera to be anchored to the wrong place
…cro-optimizations for various pieces of code running every frame on every part. Won't do much in low part count situations, and some patches are highly situational (atmospheric flight, docking ports, struts, particles repositionning on floating origin shifts...)

Patches affecting the FlightIntegrator and VesselPrecalculate are broably incompatible with mods trying to override/extend stuff through MFI. There are a bunch of perf issues with "end of the call stack" methods being inefficient because they do extra work that is already done at the call site or can be made faster by using state from the call site, so patching those methods doesn't cut it, but this mean many stock methods aren't called anymore, so if those methods are replaced/prefixed/postfixed with MFI, things will break. Notable examples are FI.UpdateAerodynamics() and FI.Integrate(Part). This could probably be avoided at the cost of some of the perf gains, but I wanted to see how far things could be pushed perf wise.
On a side note, there is a bug lurking in the VesselPrecalculate.CalculatePhysicsStats reimplementation, not sure what it is exactely but a side effect is camera target being wrongly offset. Likely something with the CoM calcs.
@JonnyOThan
Copy link
Contributor Author

This is the other set of changes from #230

@JonnyOThan JonnyOThan marked this pull request as draft October 7, 2024 14:09
…atic" patches, fixed a few things, added a bunch of library methods for querying part modules, models and renderers.
- Small refactor of BasePatch for better error handling and avoiding the "this" boilerplate
- Various small improvements for the FlightCoreSystemsPerf patches
…fficult to get right than anticipated. But after a lot of testing, I think I finally got it right.
…t code on drugs, kids !)

- Refactored the FI & VesselPre patches to make them MFI-compliant. They should no longer interfere with mods using MFI such as FAR.
…emperatureGaugeSystem.Update()

- Updated readme and settings.cfg
@gotmachine
Copy link
Contributor

So, I feel this is ready for a (pre)release.

Compatibility with MFI and relevant plugins using it (FAR, KerbalWeatherProject, AdvancedAtmosphereTools...) has been fixed, although not tested extensively, but I don't foresee issues here.

The various perf fixes have been reorganized into various "thematic" patches :

  • ModuleDockingNodeFindOtherNodesFaster : Faster lookup of other docking nodes.
  • CollisionEnhancerFastUpdate : Optimization of the CollisionEnhancer component (responsible for part to terrain collision detection).
  • PartSystemsFastUpdate : Optimization of various flight scene auxiliary subsystems : temperature gauges, highlighter, strut position tracking...
  • MinorPerfTweaks : Various small performance patches (volume normalizer, eva module checks)
  • FlightIntegratorPerf : General micro-optimization of FlightIntegrator and VesselPrecalculate, components in charge of most of heavy lifting for newtonian physics as well as atmospheric and thermal physics.
  • FloatingOriginPerf : General micro-optimization of floating origin shifts. Main benefit is in large particle count situations (ie, launches with many engines) but this helps a bit in other cases as well.

There are two other (not really related) changes in this PR :

  • Small internal refactor of the patching infrastructure for less verbose patch declaration.
  • Introduced a new "override" patch type, basically an automatic transpiler allowing to replace a method body with another. This has a little less overhead than a prefix doing the same thing, and allow for other patches (including non-KSPCF ones) to prefix the patched method as usual. This was surprisingly difficult to put together and to get right, and while I have done quite a bit of validation, it wouldn't surprise me if further issues arise with this. For now, this new patch type is only used by the patches in this PR, but if everything works out, updating all the existing "prefix return false" patches would be nice.

@gotmachine gotmachine marked this pull request as ready for review October 12, 2024 08:41
@gotmachine gotmachine merged commit 6e65ef2 into dev Oct 12, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants