#Getting the count of a component in World?

1 messages · Page 1 of 1 (latest)

late burrow
#

How can we get the count of a certain component in the World from the main thread (normal Monobehaviour)?

tardy flame
#

You can create query (remember that it's IDisposable) from EntityManager and use CalculateEntityCount

late burrow
tardy flame
#

no

#

that's like 10000 times more epxensive method

late burrow
#

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?

ocean patio
#

you create an EntityQuery with the thing you're interested in first

late burrow
#

seems none of the methods here have sync point, nice

tardy flame
#

is your game Mono based?

late burrow
#

wanted to get the count of all FloatAroundComponentData components

late burrow
tardy flame
#

if you are just benchmarking, you might just want to create your own world

ocean patio
#

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

ocean patio
tardy flame
#

Array aloc is fine

#

Temp*

#

while overload is mentioned is actually creating a GC array

ocean patio
#

well potato potato it's all very similar in the end 😄

you can also see entity counts using some of the entities inspectors

ocean patio
tardy flame
#

yeah, in the end it's all same

#

but I suggested the most recent and mainstream API for that

late burrow
#

I guess it can't go any faster than this

#

thanks @tardy flame and @ocean patio 🙂🙏

late burrow
#

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

tardy flame
#

static field of what?

late burrow
#

i was trying to get the count of a certain component

#

so I just hacked my way by doing that

tardy flame
#

well, sure. But in real development it'd be anti-pattern and not burst compatible

late burrow
#

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

tardy flame
#

idk what exactly you are benchmarking

#

but feels like you are doing it wrong

#

😅

late burrow
#

i mean the results are OK

tardy flame
#

there's a test framework

#

for benchmarking

late burrow
late burrow
tardy flame
#
    "com.unity.test-framework": "2.0.1-pre.18",
    "com.unity.test-framework.performance": "3.0.0-pre.2",
tardy flame
#

it's using NUnit as base

#

and is nicely integrated with engine

late burrow
#

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

tardy flame
#

outperform what?

late burrow
#

mine is too tiny for that

tardy flame
#

tests are done to compare something

#

in pure environment

late burrow
tardy flame
#

well

#

that doesn't say anything about what is being tested

late burrow
#

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

tardy flame
#

is it jobified?

late burrow
tardy flame
#

jobified stuff is not that trivial to benchmark

late burrow
#

the spawner is using plain query , the mover/floater is using IJobEntity

#

I tested with GameObject and got to ~2,100 spawned cubes floating around. with ECS it reached up to ~45,000

#

the number is when the fps hits 60