#Getting the count of a component in World?
1 messages · Page 1 of 1 (latest)
You can create query (remember that it's IDisposable) from EntityManager and use CalculateEntityCount
thanks! I think the new API has changed that to GetAllEntities() ?
yeah the docs says it creates a sync point..
but EntityManager doesn't have any CalculateEntityCount function
maybe it's been deleted in the 1.0.0?
the query should have it
you create an EntityQuery with the thing you're interested in first
so like
int sum = 0;
for (var i = 0; i < World.All.Count; i++) {
using var query = World.All[i].EntityManager.CreateEntityQuery( typeof(FloatAroundComponentData) );
sum += query.CalculateEntityCount();
}
``` right?
seems none of the methods here have sync point, nice
whatever is it you are doing here is not really a good practice
is your game Mono based?
no game. just benchmarking
wanted to get the count of all FloatAroundComponentData components
btw that seems to work but halved the fps
if you are just benchmarking, you might just want to create your own world
creating entity queries is not free
also, a detail, but typeof(FloatAroundComponentData) will create a temp managed array iirc, try with ComponentType.ReadOnly<FloatAroundComponentData>()
also CalculateEntityCount itself is not free, actually
ah didnt know that. thanks!
im not sure, look at the function you use 🙂
nah, it's best to just use EntityQueryBuilder
Array aloc is fine
Temp*
while overload is mentioned is actually creating a GC array
well potato potato it's all very similar in the end 😄
you can also see entity counts using some of the entities inspectors
well thats what i meant
yeah, in the end it's all same
but I suggested the most recent and mainstream API for that
this made the results ~1.3 times faster on average (3 times)
I guess it can't go any faster than this
thanks @tardy flame and @ocean patio 🙂🙏
Update
I used a static field inside my ISystem where I instantiate entities with that prefab and obviously it worked lightning fast. if anyone's in the same situation and can take advanatge of that, it's the fastest approach I found
static field of what?
the count
i was trying to get the count of a certain component
so I just hacked my way by doing that
well, sure. But in real development it'd be anti-pattern and not burst compatible
it's just for benchmarking purposes
in real scenario the component may be converted by a baker , not necessarily instantiated by another system
so yeah this will work for scenarios where you know that this component is only gonna be instantiated by a certain ISystem
man i feel the same way lol
i mean the results are OK
but yeah there has to be a more super-fast general purpose way that im missing
I really wanna avoid using editor-only assemblies with this
"com.unity.test-framework": "2.0.1-pre.18",
"com.unity.test-framework.performance": "3.0.0-pre.2",
they support build testing too
it's using NUnit as base
and is nicely integrated with engine
but i mean in the end it's not gonna outperform a static field so why bother
that seems to be meant for bigger tests
outperform what?
mine is too tiny for that
the static field that I mentioned above with a big UPDATE h2 header
ah, that, well thats kinda irrelevant, but its this:
a Spawner spawns a prefab every n seconds.
the prefab has a component that makes the LocalTransform move to a random location, randomly
the component count I wanted to measure was the moving one's
the test performance is FloatingAroundComponent count per DeltaTime milliseconds (frame time)
the more the components, the less the frame time
the benchmark is to see how much
is it jobified?
yup. with the new IJobEntity that auto generates the source
jobified stuff is not that trivial to benchmark