# GC problems surfaced... there is a reason Julia can't have void references. How do I make a type-stable entity update system?

**URL:** <https://discourse.julialang.org/t/gc-problems-surfaced-there-is-a-reason-julia-cant-have-void-references-how-do-i-make-a-type-stable-entity-update-system/118971>\
**Category:** General Usage\
**Created:** [September 3, 2024, 6:01am UTC](https://discourse.julialang.org/t/gc-problems-surfaced-there-is-a-reason-julia-cant-have-void-references-how-do-i-make-a-type-stable-entity-update-system/118971 "2024-09-03T06:01:25Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [September 3, 2024, 10:09am UTC](https://discourse.julialang.org/t/gc-problems-surfaced-there-is-a-reason-julia-cant-have-void-references-how-do-i-make-a-type-stable-entity-update-system/118971/21 "2024-09-03T10:09:25Z")

</div>

One of the most critical questions you need to ask yourself: Is it possible to add components (i.e. different kinds of data) to existing entities?

If I read the flecs FAQ right, it allows that. This is something that doesn’t work with types – a julia, C++ or java object cannot change its class at runtime.

Supposing you don’t need that, a reasonable architecture is the following:

Entity handles are 64 bit integers. Of these, you reserve e.g. 16 bit for entity kind, and 32 bit for entity index within the type.

When you have e.g. a position `Tuple{UInt32, UInt32}` that is attached to some kinds of entities, then you have a `positions::Vector{Vector{Tuple{UInt32, UInt32}}}` and you access it by `get_position(positions, untypedHandle) = positions[kind(untypedHandle)][seq(untypedHandle)]`. (consider using `Memory` instead!)

When you e.g. want to apply an update function for all entities, you would write something like

```julia
for typedEntityIterator = getTypedEntityIterators(system)
foreach(typedEntityIterator) do entity
update!(entity)
end
end

```

The crucial point is that in this kind of setting, the `getTypedEntityIterators` will return typed iterators of handles. So the `foreach` call will be type-unstable.

This is intended! It is a function barrier! And then, within the `foreach` call, all the entities you handle have the same type, which makes for nice fast code.

You typically want to do the same in e.g. java. This is because its 1. nice for the branch predictor and 2. helps the JIT specialize the code.

The main difference is that julia performance will be abysmal without these techniques, while C++ / java will degrade to merely mediocre performance.

PS. Another difference is that in java/C++ you can have your cake on typed handles, and eat it: You can have different subclasses on the handle, and store the kind information twice: In the 16 bit, plus inside the RTTI / class pointer. Then, via overloading, you get very fast operations if the runtime type is statically known, and still get fast operations if it is unknown, and you never get slow megamorphic virtual function calls.

In julia this doesn’t work: Julia can never work efficiently on objects of unknown type, even if the missing info on the type would be superfluous for the operation at hand. So you need to be more disciplined about where the type is known or not.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 3, 2024, 10:26am UTC](https://discourse.julialang.org/t/gc-problems-surfaced-there-is-a-reason-julia-cant-have-void-references-how-do-i-make-a-type-stable-entity-update-system/118971/22 "2024-09-03T10:26:13Z")

</div>

> [@foobar\_lv2](#):
>
> Is it possible to add components (i.e. different kinds of data) to existing entities?
> 
> If I read the flecs FAQ right, it allows that. This is something that doesn’t work with types – a julia, C++ or java object cannot change its class at runtime.

I noticed that Flecs supports C++ and provides an example in the README initializing a system with a number of components via template parameters. There’s a C++ example of components being [added and removed](https://www.flecs.dev/flecs/md_docs_2Quickstart.html#component) in the Quickstart. I don’t really know C or C++ so I don’t know what’s going on there; it’s not apparent to me how worlds, systems, and entities are structured (Maybe this would be informative? [How are components/entity stored in memory? · Issue #3 · SanderMertens/flecs · GitHub](https://github.com/SanderMertens/flecs/issues/3)). If any of that can be translated to Julia, that would be a good start.

---

<div class="post-metadata">

**Author:** ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)\
**Post date:** [September 3, 2024, 10:43am UTC](https://discourse.julialang.org/t/gc-problems-surfaced-there-is-a-reason-julia-cant-have-void-references-how-do-i-make-a-type-stable-entity-update-system/118971/23 "2024-09-03T10:43:46Z")

</div>

Thank you… however, my ECS is kinda not like Flecs or others along the line. There wouldn’t even be entity indices. Instead, there would be bulk updates.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [September 3, 2024, 11:34am UTC](https://discourse.julialang.org/t/gc-problems-surfaced-there-is-a-reason-julia-cant-have-void-references-how-do-i-make-a-type-stable-entity-update-system/118971/24 "2024-09-03T11:34:22Z")

</div>

> [@Tarny\_GG\_Channie](#):
>
> I’m not sure if that supports non-bits types. Still, you now need to manage the memory pool yourself and so on. I was thinking of not supporting non-bits types at all.

You won’t need none bitstypes with that setup 😉  
The MutableRef is a bitstype so, as long as you use that in your structs you should be fine.  
But I’m not sure if I understand your problem completely, so might not really help 🤷

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [September 9, 2024, 4:04pm UTC](https://discourse.julialang.org/t/gc-problems-surfaced-there-is-a-reason-julia-cant-have-void-references-how-do-i-make-a-type-stable-entity-update-system/118971/25 "2024-09-09T16:04:19Z")

</div>

You may already be aware of its existence, but it’s worth mentioning existing julia ECS packages like Overseer.jl. It may be sufficient for you or at least a source of ideas.

[Previous page](https://discourse.julialang.org/t/gc-problems-surfaced-there-is-a-reason-julia-cant-have-void-references-how-do-i-make-a-type-stable-entity-update-system/118971.md?page=1)
