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

Performance: ModuleScienceExperiment registers with gameevents for part prefabs #153

Closed
JonnyOThan opened this issue Jun 30, 2023 · 1 comment
Labels
kspPerformance Possible performance improvement in KSP

Comments

@JonnyOThan
Copy link
Contributor

I saw someone with 560 lingering event handlers for the following events:

    public override void OnAwake()
    {
        GameEvents.onVesselSituationChange.Add(OnVesselSituationChange);
        GameEvents.onVesselStandardModification.Add(OnVesselModified);
        GameEvents.onGamePause.Add(OnGamePause);
        GameEvents.onGameUnpause.Add(OnGameUnpause);
        GameEvents.onCommandSeatInteraction.Add(OnCommandSeatInteraction);
    }

This gets called from the following callstack during loading:

>    Void ModuleScienceExperiment:OnAwake ()+0x1 at C:\Users\Jon\source\repos\ksp-assembly-csharp\ModuleScienceExperiment.cs:[206:3-206:67]    C#
     Void PartModule:Awake ()+0x41 at C:\Users\Jon\source\repos\ksp-assembly-csharp\PartModule.cs:[750:3-750:13]    C#
     Component UnityEngine.GameObject:AddComponent (Type)+0x3 at :-1    C#
     PartModule Part:AddModule (String, Boolean)+0x11f at C:\Users\Jon\source\repos\ksp-assembly-csharp\Part.cs:[5809:3-5809:81]    C#
     PartModule Part:AddModule (ConfigNode, Boolean)+0x30 at C:\Users\Jon\source\repos\ksp-assembly-csharp\Part.cs:[5830:3-5830:60]    C#
     AvailablePart PartLoader:ParsePart (UrlConfig, ConfigNode)+0xcd7 at C:\Users\Jon\source\repos\ksp-assembly-csharp\PartLoader.cs:[922:5-922:32]    C#
     Boolean <CompileParts>d__63:PartLoader+<CompileParts>d__63.MoveNext_Patch0 (<CompileParts>d__63)+0x736 at :-1    C#
     Boolean <FrameUnlockedCoroutine>d__62:MoveNext ()+0x73 at C:\Users\nderaney\Documents\GitHub\KSPCommunityFixes\KSPCommunityFixes\Performance\FastLoader.cs:[1880:21-1880:61]    C#
     Boolean <PartLoader_CompileAll>d__58:MoveNext ()+0x301 at C:\Users\nderaney\Documents\GitHub\KSPCommunityFixes\KSPCommunityFixes\Performance\FastLoader.cs:[1785:13-1785:54]    C#
     Void UnityEngine.SetupCoroutine:InvokeMoveNext (IEnumerator, IntPtr)+0x27 at :-1    C#

This probably doesn't matter much in the stock game, but in heavily modded installs this might get pretty expensive inside OnVesselModified/OnVesselSituationChange. Could be fixed with a prefix on OnAwake that checks the current game scene.

@gotmachine gotmachine added the kspPerformance Possible performance improvement in KSP label Aug 6, 2023
gotmachine added a commit that referenced this issue Dec 6, 2023
…egistered from ModuleScienceExperiment on the prefabs and in other scenes
@gotmachine
Copy link
Contributor

Fix released in 1.32.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kspPerformance Possible performance improvement in KSP
Development

No branches or pull requests

2 participants