Huge slowdown when EntityManager.GetComponentData() waits for Write dependency

Hello all, I’m new to unity’s ECS system, trying to implement some hybrid ECS in my game.

I’ve run into an issue where main-thread calls to EntityManager.GetComponentData(Entity) are causing a huge slowdown. The profiler traces the slowdown to the following function:

Update.ScriptRunBehaviourUpdate


EntityManager.GetComponentData()
EntityDataAccess.GetComponentData()
ComponentDepenedencyManager.CompleteWriteDependency()
ComponentDependencyManager.CompleteWriteDependencyNoChecks()
JobHandle.Complete()
JobHandle.ScheduleBatchedJobsAndComplete()
WaitForJobGroupID
Semaphor.WaitForSignal

It appears that when I call EntityManager.GetComponentData(Entity) in my update function, the ECS jobs that write to the relevant entity haven’t finished yet, and so the query must pause mainthread execution and wait for the JobHandle.Complete(). But I don’t understand why the dependency chain isn’t already completed. I’ve looked around and cant see anything that says .GetComponentData creates a sync point.

All of the ECS systems that read/write to the entity implement the Entities.Foreach().Schedule() lambda function in a SystemBase class, and they aren’t manually constructed/updated (no [DisableAutoCreation]), so the relevant dependency chain is auto-generated.

When should I be trying to call GetComponentData() from MonoBehaviours which are trying to read from an entity’s ComponentData? Moving the call to LateUpdate() doesn’t help.

I guess I’m confused as to the when dependencies generated by a SystemBase are completed in relation to the standard monobehaviour update loop.

Please Help!

A little more info about the use case:

I’m trying offload some heavy computations into ECS jobs, and I want to query the results on the main thread. In this case, it’s the AI decision making - the AI gameObject, when instantiated, creates and tracks an Entity which a few ECS systems write to, for example, setting a desired position for the AI to move to. Then on the main thread, the AI queries that position, and moves to it using SetDesiredPosition on it’s NavMeshAgent on the mainthread. You don’t really notice a slowdown when there are only ~20 entites, but start adding more you start losing major frames. For reference, the Entity Debugger says the total time to execute all the relevant ECS systems is ~1ms.

I’m on Unity version 2019.3.15f1, using entities package 0.11.1.preview 4

This is no a synch point, it only completes related to this type Write (and not touching Read chain) dependency chain, not all jobs (which is Synch Point - it’s specifically described thing in DOTS). Behaviour similar to SetComponentData (with one note that it completes not only Write chain for that type but also Read chain)
More info here, which I already described a couple of times.
https://discussions.unity.com/t/800723/6
And about what is synch point and which methods cause it you can read in EntityManager source code

1 Like