SetSharedComponent allocates too much?

So far this is the best way I found to set a shared component. Please ignore the code that you can’t recognise because it’s not UnityECS

 protected override void OnUpdate()
        {
            var renderEntityStructs = entitiesDB
               .QueryEntities<RenderUECSEntityStruct>(MachineEditingGroups.RENDER_GROUP).GetEnumerator();

            var entityType = GetArchetypeChunkEntityType();

            using (var chunks = _group.CreateArchetypeChunkArray(Allocator.TempJob))
            {
                for (var index = 0; index < chunks.Length; index++)
                {
                    var chunk = chunks[index];
                    var entityArray = chunk.GetNativeArray(entityType);

                    for (int i = 0, count = chunk.Count; i < count; ++i)
                    {
                        renderEntityStructs.MoveNext();
                        PostUpdateCommands.SetSharedComponent(entityArray[i], renderEntityStructs.Current.UECSMesh);
                    }
                }
            }
        }

it works, but I noticed that even if I use the PostUpdateCommands, it does allocate quite a lot! Maybe it’s in debug only?

I think I may have misunderstood how the sharedcomponent thing works, I assumed it is per entity, but it seems to be per chunk instead? I would need the RenderMesh component to be unique per entity, but the mesh pointer inside shared, is this a thing or I have to be sure that the RenderMesh in a chunk are actually sharing the same mesh pointer too?

Modifying the shared component every frame can be deadly for performance
https://docs.unity3d.com/Packages/com.unity.entities@0.0/manual/shared_component_data.html

I can see it, but then isn’t there any way at the moment to be able to change meshes continuously without performance impact?

I can’t understand why the RenderMesh struct must be shared at all. I understand it needs to support the hybrid architecture to hold references, but the references should be shared not the component itself

@sebas771 - i think you built an own ECS (I looked into this before Unity ECS).

I highly recommend @5argon blog posts… you will also find something about shared component data

https://gametorrahod.com/tag/unity-ecs/

1 Like

I think I start to understand how they work. If it’s true that the shared components are hashed, it shouldn’t move the entity if the hash is the same. I have only two “different” (has different hash) components there, so I am not sure why it’s so slow.

I don’t think there is check to see if the old component data is the same as the new one,
it’s just a thought, but maybe you could make a check in your code to see if the new mesh is not the same as the old one, and if it’s not the same, then execute “SetSharedComponent” ?

yep that can work too, but if I understood correctly how it works, since the hashed shared components create a new archetype, I could just query the entities with that shared component.

If this is possible, I would solve my problem too. Is there a way to return all the entities that share the same shared entity component? I will need to ask something like give me all the entities that share the RenderMesh component with the hash X

you can filter by SCD value - check the blog - I can not explain it better than @5argon :slight_smile:

1 Like

thanks, just got there, seems clear

Shared component index.