bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Remove redundant table and sparse set component IDs from Archetype

Open james7132 opened this issue 3 years ago • 1 comments

Objective

Archetype is a deceptively large type in memory. It stores metadata about which components are in which storage in multiple locations, which is only used when creating new Archetypes while moving entities.

Solution

Remove the redundant Box<[ComponentId]>s and iterate over the sparse set of component metadata instead. Reduces Archetype's size by 4 usizes (32 bytes on 64-bit systems), as well as the additional allocations for holding these slices.

It'd seem like there's a downside that the origin archetype has it's component metadata iterated over twice when creating a new archetype, but this change also removes the extra Vec<ArchetypeComponentId> allocations when creating a new archetype which may amortize out to a net gain here. This change likely negatively impacts creating new archetypes with a large number of components, but that's a cost mitigated by the fact that these archetypal relationships are cached in Edges and is incurred only once for each edge created.

Additional Context

There are several other in-flight PRs that shrink Archetype:

  • #4800 merges the entities and rows Vecs together (shaves off 24 bytes per archetype)
  • #4809 removes unique_components and moves it to it's own dedicated storage (shaves off 72 bytes per archetype)

Changelog

Changed: Archetype::table_components and Archetype::sparse_set_components return iterators instead of slices. Archetype::new requires iterators instead of parallel slices/vecs.

Migration Guide

Do I still need to do this? I really hope people were not relying on the public facing APIs changed here.

james7132 avatar Jun 04 '22 21:06 james7132

I'm fine without a migration guide here. Anyone attempting to rely on this is experienced enough to piece it together.

alice-i-cecile avatar Jun 04 '22 21:06 alice-i-cecile

bors r+

cart avatar Nov 15 '22 21:11 cart

Build failed:

bors[bot] avatar Nov 15 '22 21:11 bors[bot]

bors retry

cart avatar Nov 15 '22 21:11 cart