Skip to content

Conversation

@dudeax
Copy link

@dudeax dudeax commented Dec 13, 2025

  • This PR adds first class zflecs support for using multiple worlds.

Previously multiple world support was blocked by the clever implementation of zigs comptime to get type id look ups for free. This relied on single global static variables which obviously didn't play well once you had more than world with potentially different ids.

For my implementation I attempted to keep (most of) the benefits of this implementation while also allowing multiple worlds to exist.

  • Type -> flecs id mapping is done via a nested hash map of world -> type -> flecs id. This will have the same runtime complexity as the old version, but will take slightly longer to look up.
  • This new mapping can be accessed via the new pub fn world_id(world: *const world_t, comptime T: type) id_t function.
  • Types are not actually stored as either types or strings, but as a unique usize to allow for faster hash lookups.
  • Internally all of flecs look ups have been switched to use this function, however the old singleton pub inline fn id(comptime T: type) id_t is still supported so this PR will be a drop in replacement for all existing implementations, and shouldn't contain any breaking changes.
  • Another change I made was to pull dynamic map creation for both first_world_pointers (formerly component_ids_hm) and world_component_lookup into the init function and to use the EcsAllocator rather than the page allocator. If there are any concerns around this I would be happy to move them back to the page allocator, but this felt cleaner than the previous iteration.

In general I think another option could be to fully deprecate any idea of a singleton look up for the first world, but this would be a breaking change, and technically slightly less performant for single world applications.

@Srekel
Copy link
Member

Srekel commented Dec 14, 2025

This looks fine to me. I would perhaps even add a comptime config bool that allows the user to mark it as a single-world application to reduce the world-lookup to just instantly resolve to the first world. But that could easily be its own PR.

I can't test this myself unfortunately due to being behind on zflecs and Zig so will see if anyone else have any comments before I merge it. (feel free to ping me if I forget)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants