Speeding up Random Access?

Is there some method of speeding up GetElementData? Say I have 30 random entities that need ComponentA, besides splitting it into multiple threads, there is any way to speed it up?

There’s a couple of tricks you can use for high algorithmic complexity workloads of O(n^2) or higher. But in general, the best thing you can do is rework your design to do the random accesses less.

1 Like

Nah, it’s a very long term planning for a project where players can code up npc actions, that would include random access to an entity component data. I was wondering if just doing whatever or attempting to execute all of them at once or even some other random trick could speed it up, since I want to have 10000+ npcs doing things at once.
Edit:
Maybe sorting all entities accessing the same ComponentData and then searching for the data through chunk iteration? Although I have no idea how to sort Entities by chunk?

ComponentDataFromEntity is your best bet. Hashmaps also work but in terms of performance they are very equal so there’s really no need to do the additonal work. CDFE is really fast enough when you end up in random lookups.

To reduce random access, you are right, some sort of ordering would be necessary.
Take a look at the EntityInChunk struct:

public unsafe struct EntityInChunk : IComparable<EntityInChunk>, IEquatable<EntityInChunk>
    {
        internal Chunk* Chunk;
        internal int IndexInChunk;

        public int CompareTo(EntityInChunk other)
        {
            ulong lhs = (ulong)Chunk;
            ulong rhs = (ulong)other.Chunk;
            int chunkCompare = lhs < rhs ? -1 : 1;
            int indexCompare = IndexInChunk - other.IndexInChunk;
            return (lhs != rhs) ? chunkCompare : indexCompare;
        }

        public bool Equals(EntityInChunk other)
        {
            return CompareTo(other) == 0;
        }
    }

We can find IndexInChunk in there, this could be used for changing the sort order. I have no idea if this would be stable though.

From my testing, presorting a list of entities by chunk and index in chunk is slower than CDFE for a small number of components. For a medium to large number of components, it is viable if your job was already single-threaded without sorting. But sorting 96 bits does not parallelize well at scale.