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

Framerate drops while hovering over inventory #2

Closed
ducakar opened this issue Mar 15, 2015 · 4 comments
Closed

Framerate drops while hovering over inventory #2

ducakar opened this issue Mar 15, 2015 · 4 comments
Labels
Milestone

Comments

@ducakar
Copy link

ducakar commented Mar 15, 2015

Significant framerate drop (to only a few frames per second) occurs when hovering a mouse cursor over an inventory window during the flight scene.

There are no messages printed to the log. It only occurs during flight, it works fine in editor.

@ducakar
Copy link
Author

ducakar commented Mar 15, 2015

FindObjectsOfType() call in ModuleKISInventory.cs:835 seems to be the cause of this. Functions like Object.FindObjectsOfType(), Resources.FindObjectsOfTypeAll() etc. are extremly slow and should be avoided whereever possible, especially in methods that are called per-frame. A single such call per frame has drastic impact on framerate.

@smjjames
Copy link

I've gotten this as well.

Theres also some camera wierdness going on in the VAB.SPH that might be related.

The lag is particularily bad when at a base that is already laggy

@xEvilReeperx
Copy link

The call to FindObjectsOfType in ModuleKISInventory can be eliminated by adding a component to the window prefab which updates a static list of open UIPartActionWindows. Example:

[KSPAddon(KSPAddon.Startup.EditorAny, true)]
public class InitPartActionWindowTracker : MonoBehaviour
{
    private void Awake() { UIPartActionWindowTracker.Init(); }
}

[KSPAddon(KSPAddon.Startup.Flight, true)]
public class FlightBootstrapper : InitPartActionWindowTracker
{
}


public class UIPartActionWindowTracker
{
    private class WindowTrackerComponent : MonoBehaviour
    {
        private void Awake() { Add(GetComponent<UIPartActionWindow>()); }
        private void OnDestroy() { Remove(GetComponent<UIPartActionWindow>()); }
    }


    public static readonly List<UIPartActionWindow> Windows = new List<UIPartActionWindow>();

    public static void Init()
    {
        if (UIPartActionController.Instance.windowPrefab.gameObject.GetComponent<WindowTrackerComponent>() == null)
            UIPartActionController.Instance.windowPrefab.gameObject.AddComponent<WindowTrackerComponent>();
    }

    private static void Add(UIPartActionWindow window)
    {
        if (window != null && !Windows.Contains(window))
            Windows.Add(window);
    }

    private static void Remove(UIPartActionWindow window)
    {
        if (window != null && Windows.Contains(window))
            Windows.Remove(window);
    }
}

Then this line:

foreach (var window in GameObject.FindObjectsOfType(typeof(UIPartActionWindow))
.OfType<UIPartActionWindow>().Where(p => p.Display == UIPartActionWindow.DisplayType.Selected))

could be changed to

foreach (var window in UIPartActionWindowTracker.Windows.Where(p => p.Display == UIPartActionWindow.DisplayType.Selected)

and avoid the performance penalty

@KospY KospY added the bug label Mar 21, 2015
@KospY
Copy link
Collaborator

KospY commented Mar 22, 2015

Fixed for next version (1.0.1)

@KospY KospY closed this as completed Mar 22, 2015
@KospY KospY modified the milestone: KIS 1.0.1 Mar 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants