performance of BehaviorManager.Restart

JohnTomorrow

New member
We are seeing big spikes when this function is called, up to 230 ms for 7 trees. Our entities are pooled and we use behaviorTree.EnableBehavior(), behaviorTree.DisableBehavior(true), and BehaviorManager.instance.RestartBehavior (activeBehavior) to handle starting and stopping our trees.

Any suggestions on how to improve this hitch? What is the correct usage for pooled objects?
 
Hey Justin, I checked out the documentation you posted and I am not sure I understand it correctly. I think I probably have the wrong setup. Currently I have the following:

- an AI entity prefab with a behavior tree component and other various components for game logic. The behavior tree component is pointed to an external tree stored in the project.
- this prefab is instantiated 128 times. the tree component calles EnableBehavior and DisableBehavior(true) and the gameObject is deactivated so that it is pooled properly.
- as the player travels through the world these entities are toggled on and off and recycled. EnableBehavior, RestartBehavior and DisableBehavior(true) are used on the tree component.

Here's a screenshot of the profiler. I wrapped the RestartBehavior call with profile sample calls to get it to report.

frame hitch.png


On an semi-related topic we also have a very long loading time on the initial load of the app as the these trees are initially allocated. Looking at the documentation you posted, is it possible to allocate one tree and have every entity reuse it? Or am I completely misunderstanding it? Currently it takes a tremendous amount of time to start the game as the referenced external tree is quite large and uses many external trees times multiple times to create its logic.
 
Here's a screenshot of the profiler. I wrapped the RestartBehavior call with profile sample calls to get it to report.
Can you enable deep profile to see what is causing the bottleneck? BehaviorManager.Restart really doesn't do much besides start a new task so I'm surprised that it is taking such a long time. What happens if you disable Reset Values on Restart on the Behavior Tree component?

On an semi-related topic we also have a very long loading time on the initial load of the app as the these trees are initially allocated. Looking at the documentation you posted, is it possible to allocate one tree and have every entity reuse it? Or am I completely misunderstanding it? Currently it takes a tremendous amount of time to start the game as the referenced external tree is quite large and uses many external trees times multiple times to create its logic.
There are two different steps to initialization: deserializing the tree, and then loading that deserialized tree into the arrays. The deserialization aspect is not tied to a specific GameObject, but the loading aspect is. You can reuse external trees which will save on total time spent, but those external trees still need to be associated with a specific GameObject. It sounds like you are doing this?
 
Can you enable deep profile to see what is causing the bottleneck? BehaviorManager.Restart really doesn't do much besides start a new task so I'm surprised that it is taking such a long time. What happens if you disable Reset Values on Restart on the Behavior Tree component?

Deep profile shot, a ton of function calls for 2 ai entities being awakened from pool.

1543954326343.png

Disabling Reset Values on Restart decreases time spent to 1ms or less! But don't I need to reset the tree?

There are two different steps to initialization: deserializing the tree, and then loading that deserialized tree into the arrays. The deserialization aspect is not tied to a specific GameObject, but the loading aspect is. You can reuse external trees which will save on total time spent, but those external trees still need to be associated with a specific GameObject. It sounds like you are doing this?

Currently I have the ai prefab in the resources folder with the external tree variable assigned to a tree in the project. Its instantiated over 100x on Awake. Is this causing the tree to be deserialized per each gameObject that is instantiated? Inside the external tree is references to other external trees with the external tree node. I am not doing anything in code for the initial loading. Does the system know to reuse the same trees by default or do I have to do something special?
 
Disabling Reset Values on Restart decreases time spent to 1ms or less! But don't I need to reset the tree?
Reset Values will use reflection to restore the values back to their starting value. It looks like you have a deep tree with lots of variables so this is causing the bottleneck. I would continue to try it with the value disabled and see if it works - a lot of the variables by the included tasks are reset within OnEnd so you may not need this feature.

Currently I have the ai prefab in the resources folder with the external tree variable assigned to a tree in the project. Its instantiated over 100x on Awake. Is this causing the tree to be deserialized per each gameObject that is instantiated? Inside the external tree is references to other external trees with the external tree node. I am not doing anything in code for the initial loading. Does the system know to reuse the same trees by default or do I have to do something special?
Here's what I would do:

1. Instantiate 100 copies of the external tree.
2. Instantiate 100 copies of the prefab.
3. Assign the external tree to the instantiated prefab as you need it. This will then ensure the tree doesn't get instantiated twice.
 
Top