Checking for single Entity change in main thread is worth the cost?

I am aware I could spend some hours to profile this, but I am hoping some else has already done so, I am asking this rather than researching myself for the same reason I look for a json library rather than making my own when I need to use json on my projects.

I am using lots of reflection to get data from an entity and use it to construct a string and then finally update a UI with said information.
So the question is, is it worth to

if (em.Exists(ent) && em.GetChunk(ent).GetChangeVersion(no idea what goes here) != old_value) {
old_value = em.GetChunk(ent).GetChangeVersion(no idea what goes here);

I assume it’s always faster to actually check for chunk change before processing the whole chunk in a system, but what about getting the chunk + checking it’s version for a single entity being used in the main thread?

1 Like

It really depends on how much work you skip if the change check says no change. Also, if you are doing this for a bunch of entities, pre-checking everything in Burst would be beneficial. You could create a NativeStream that record Entity, ComponentType count that changed, and then the ComponentType values.

1 Like

I was talking about comparing the cost of testing for chunk change vs the cost of getting the value.
Since the change check is for the whole chunk I can safely assume it can spare taking longer than taking a single value from a single entity and still be worth using it in systems. There is also the overhead of having to get the chunk of the entity, which might or might not be just as costly of getting the IComponentData from the entity.
If getting the value is faster, I can just compare the previous value with whatever I got now.

Maybe it does not even need someone to have actually profiled this before to answer. If anyone can say that the cost of EntityManager.GetComponentData is equivalent to EntityManager.GetChunk then, for the specific use case of checking if a individual entity has changed is never worth. The use case is: Easy ECS Access for UI Toolkit (requires Harmony) , specifically:

                    SourceEntity sourent = Utils.UIElementUserDataInParent<SourceEntity>(target);
                    if (sourent != null && sourent.source != default) {
                        Entity ent = sourent.source;
                        if (em.Exists(ent)/* change check would go here*/) {
                            object data = GetEntityDataFunction(em, ent);
                            if (data != null) {
                                string newval = TransformationFunction(data);
                                if (OldValue != newval) {
                                    OldValue = newval;
                                    TextElement eltarg = target as TextElement;
                                    eltarg.text = newval;
                                }
                            }
                        }
                    }