Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question State transition with events causes absurd memory allocations

Discussion in 'Visual Scripting' started by DoubleIsLoveDoubleIsLife, Jul 21, 2023.

  1. DoubleIsLoveDoubleIsLife

    DoubleIsLoveDoubleIsLife

    Joined:
    Nov 29, 2014
    Posts:
    31
    I'm using VS to build AI, and basically I have it so all the main stuff is done in C#, while the flow is handled by visual scripting state machines. This means there is virtually zero code in the state machines, just a few function calls and events, which link states like "Idle", "Caution" etc together.

    I assumed this would be a fairly performant way to handle it since I would avoid most of the reflection overhead. But I guess I was wrong. When I turn on deep profiling I can see that when a state is entered, it seems to initialize all of the events by doing all sorts of boxing, then adds them to new hashsets. It doesn't seem to cache any of this whatsoever since the memory is allocated every time it happens. When I have many AIs active, this means suddenly 5mb+ garbage is generated when they all lock-on to the player.

    Am I missing something or is it just supposed to be this inefficient?

    I use EventBus.Trigger(string, gameObject, params);
    Would using some other method be faster?
     
  2. DoubleIsLoveDoubleIsLife

    DoubleIsLoveDoubleIsLife

    Joined:
    Nov 29, 2014
    Posts:
    31
    Update for anyone with the same issue. I mostly solved this problem by registering empty event handlers on gameobject creation. In my case it reduces GC by over 90%.

    Code (CSharp):
    1. private Action<Vector3> empty = _ => { };
    2. private EventHook canReachTargetEmptyEvent;
    3. private EventHook completePathEmptyEvent;
    4. private EventHook partialPathEmptyEvent;
    5.  
    6. protected virtual void RegisterEventsWorkaround()
    7. {
    8.     canReachTargetEmptyEvent = new EventHook(AIEventNames.OnCanReachTarget, gameObject);
    9.     completePathEmptyEvent = new EventHook(AIEventNames.OnCompletePathToNavigationGoal, gameObject);
    10.     partialPathEmptyEvent = new EventHook(AIEventNames.OnPartialPathToNavigationGoal, gameObject);
    11.  
    12.     EventBus.Register(canReachTargetEmptyEvent, empty);
    13.     EventBus.Register(completePathEmptyEvent, empty);
    14.     EventBus.Register(partialPathEmptyEvent, empty);
    15. }
    Make sure to unregister the handlers when needed (for example when the gameobject is disabled).

    Code (CSharp):
    1. protected virtual void UnregisterEventsWorkaround()
    2. {
    3.     EventBus.Unregister(canReachTargetEmptyEvent, empty);
    4.     EventBus.Unregister(completePathEmptyEvent, empty);
    5.     EventBus.Unregister(partialPathEmptyEvent, empty);
    6. }
     
    iScriptz likes this.