question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Soooo lets talk about archetypes.

If i am correct, archetypes offer a higher iteration speed. This is because entities with the same components are stored in one big array. Which means that the iteration over them can be loaded more efficient into the cpu. So iteration is a lot more cache friendly, which results in a performance boost.

So we should probably look into this. I assume that this wouldnt even require a major rework. Only the component storage would need some optimization.

I recently looked into those articles. Theory behind archetypes Unitys ECS C# Archetype ECS

So a very quick and dirty first draft of a archetype based component storage would look like this…

    // Just stores an array of components, any smarter solution ? 
    public unsafe class ComponentPool {

        public void* components;  // We tightly pack all archetype components behind each other e.g. Player, Transform, Player, Transform
        public int length;

        // Unsafe operations to add, set or get certain components from the array
    }
    
    // An archetype, basically all entities with the same components. 
    public class Archetype {

        // An id which represents a bunch of types, somehow generated from a Type[] array basically
        public int id;
        public Type[] types;

        // The entities within that archetype
        public Entity[] entities;
        public ComponentPool components; 
        public int size;
    }

A few problems remain…

  • Adding/Removing components is getting costly ( Can be solved by just disabling then )
  • Iteration over a subset is a bit more difficult i guess ( Not sure if its slower or faster, but more complicated )

You know the source code of default.ECS way better than i do, would you mean that such an archetype based architecture would improve the speed and how hard would it be to change the underlaying component storage ?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:23 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
Dorakucommented, Nov 11, 2021

It is starting to look great, when using Single or Shared mode for component, the overhead is ~x2 compared to current implementation, all api should stay intact. In archetype mode the overhead is ~x4 when using the old Get<T> api for convenience but the new api gives x3 more performance compared to current implementation!

A lot of work still remains, the next big question is what to do with MultiMap systems, is it possible to do something so they can benefit from archetypes? A new api should be also created for entities to batch modifications together so the archetype is only changed once even if we do multiple Set/Remove to limit copy.

1reaction
nrader95commented, Nov 8, 2021

Interface IToggleable which can be implemented by components and queries check that one during iteration

This would be causing boxing if your component is a struct and you work with it after casting your type to interface.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Archetype - Wikipedia
In Jung's psychological framework, archetypes are innate, libidinally collective schemas, universal prototypes for idea-sensory impression images and may be ...
Read more >
Understanding Personality: The 12 Jungian Archetypes
The 12 Jungian Archetypes. Ruler. Creator/Artist; Sage; Innocent; Explorer; Rebel; Hero; Wizard ...
Read more >
What Are the Jungian Archetypes?
Archetypes are universal, inborn models of people, behaviors, and personalities that play a role in influencing human behavior.
Read more >
Archetypes - Discover yours
Discover your Archetypes. Who are you? Take the Quiz. ... Download your free Archetype report. Take the quiz to discover yours.
Read more >
Archetype Definition & Meaning
The meaning of ARCHETYPE is the original pattern or model of which all things of the same type are representations or copies :...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found