Where I’m now there’s a weird happening, I’ve tried isolated instantiation tests. Instantiating my archetype 250k times measures in around 4ms. So the test case isn’t that bad. In practice the instantiation measures around 8-10ms. I think the reason is because the CPU is under load from before but it’s really weird that the timings are so different for doing essentially the same thing.
Still, I feel like entity instantiation should be faster. Copying overall 45MB of the same data, 250k times shouldn’t even measure over 1ms, yet it does and it makes me stuck in how I should tackle this problem.
Any ideas are welcome.
Here’s some code for my current test system:
using Unity.Collections;
using Unity.Entities;
using UnityEngine.Profiling;
public struct Comp1 : IComponentData
{
public int val1;
public Entity reference1;
}
public struct Comp2 : IComponentData{ } // just a tag
public struct Comp3 : IComponentData
{
public Entity reference1;
public Entity reference2;
}
public struct Comp4 : IComponentData
{
public float val1;
public float val2;
public float val3;
public int val4;
public int val5;
public int val6;
public float val7;
public float val8;
public float val9;
public float val10;
public float val11;
public int val12;
public int val13;
public int val14;
}
[AlwaysUpdateSystem]
public class InstantiateTestSystem : SystemBase
{
EntityArchetype CalculateArchetype;
EntityArchetype CalculateArchetypeSmall;
EntityQuery querySmall;
EntityQuery queryBig;
protected override void OnCreate()
{
CalculateArchetype = EntityManager.CreateArchetype(typeof(Comp1), typeof(Comp2), typeof(Comp3), typeof(Comp4));
CalculateArchetypeSmall = EntityManager.CreateArchetype(typeof(Comp1), typeof(Comp2), typeof(Comp3));
queryBig = GetEntityQuery(typeof(Comp1), typeof(Comp2), typeof(Comp3), typeof(Comp4));
querySmall = GetEntityQuery(typeof(Comp1), typeof(Comp2), typeof(Comp3));
}
protected override void OnUpdate()
{
TestBig();
//TestSmall();
}
public void TestBig()
{
Create(CalculateArchetype, 250000);
Destroy(queryBig);
}
public void TestSmall()
{
Create(CalculateArchetypeSmall, 250000);
Destroy(querySmall);
}
public void Create(EntityArchetype archetype, int count)
{
Profiler.BeginSample("EntityManager.CreateEntity");
var entities = EntityManager.CreateEntity(archetype, count, Allocator.Temp);
Profiler.EndSample();
entities.Dispose();
}
public void Destroy(EntityQuery query)
{
EntityManager.DestroyEntity(query);
}
}
Instantiation without any checks/safety results in 3.73ms for TestSmall and 5.75ms for TestBig. The big comp sadly takes around 2ms more, maybe I can find ways to not make it as big or not use it at all but that’s the current state.
