[Help] DOTS Unity Physics performance issue

Hello,
I want you to help me about performance issue on my game.
First of all, I’m using Entites 1.0.16, Unity Physics 1.0.16 .
When I spawn 2,500 Entites, CPU time took 72ms.
I don’t know how to analyse profiler well. So I will show you the result.


I guess that main bottle necks are

  • World Unity.Physics.Systems.SolveAndIntegrateSystem: 11.91ms
  • Solver:ParallelBuildJacobiansJob (Burst): 5.63ms
  • Solver:ParallelSolverJob (Burst)
    Current frame accumulated time: 11.81ms for 36 instances on thread ‘Main Thread’
    (89.58ms for 215 instances over 8 threads)

Each entity has either sphere, cylinder or box physics body.

Here is my code for InfectionProgressSystem.

using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Transforms;
using Unity.Collections;
using Unity.Burst;
using EntityDebug;

[RequireMatchingQueriesForUpdate]
public partial struct InfectionProgressSystem : ISystem
{
    public EntityQuery virusPrefabQuery;
    public RefRW<RandomComponent> random;

    [BurstCompile]
    public void OnCreate(ref SystemState state) {
        virusPrefabQuery = new EntityQueryBuilder(Allocator.Temp)
        .WithAll<PhysicsVelocity, PhysicsMass, Prefab, CategorySharedComponent,VariantCategory,VirusCategory>()
        .WithOptions(EntityQueryOptions.IncludePrefab)
        .Build(ref state);
        
        state.RequireForUpdate(virusPrefabQuery);
        state.RequireForUpdate<RandomComponent>();
        state.RequireForUpdate<BeginFixedStepSimulationEntityCommandBufferSystem.Singleton>();

    }

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        var ecb = SystemAPI.GetSingleton<BeginFixedStepSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);
        random = SystemAPI.GetSingletonRW<RandomComponent>();


        foreach ((RefRW<Host> hostRef, LocalTransform transform,Entity entity) in SystemAPI.Query<RefRW<Host>,LocalTransform>().WithEntityAccess())
        {
            Host host = hostRef.ValueRW;
            if (host.timer > 0)
            {
                host.timer -= SystemAPI.Time.DeltaTime;
                hostRef.ValueRW = host;
            }
            else
            {
                if(host.PathogenCategory.x == (uint)Category.Virus){
                    virusPrefabQuery.SetSharedComponentFilter(new VirusCategory { type = (Virus)host.PathogenCategory.y } ,new VariantCategory{ type = (Variant)host.PathogenCategory.z });
                Entity pathogenPrefab = virusPrefabQuery.GetSingletonEntity();
                var physicsVelocity = SystemAPI.GetComponent<PhysicsVelocity>(pathogenPrefab);
                var inversePhysicsMass = SystemAPI.GetComponent<PhysicsMass>(pathogenPrefab).InverseMass;
                for (int i = 0; i < host.number; i++)
                {
                    Entity newPathogen = ecb.Instantiate(pathogenPrefab);
                    physicsVelocity.Linear += inversePhysicsMass * new float3(random.ValueRW.random.NextFloat2Direction(), 0f);
                    ecb.SetComponent(newPathogen, transform);
                    ecb.SetComponent(newPathogen, physicsVelocity);
                    }
                }
                ecb.SetComponentEnabled<Change>(entity,true);
                ecb.SetComponent(entity,new Change{
                    isRemoved = true,
                });
            }
        }
    }
}

Is it common that DOTS physics has this low performance?
Also I don’t get why InfectionProgressSystem take so much time caused by physics.
Can anybody help me to fix this? Thank you a lot.

Make sure burst is enabled.

Also have a look at the Jobs section in the profiler to see where the time is really spent.
The systems spawn the jobs but don’t wait for completion.
So the time displayed on the systems is not representative or the actual time spent in the physics pipeline.