Skip to content

Spawning child entities is extremely slow #17301

@aevyrie

Description

@aevyrie

Bevy version

0.15, has existed for as long as constructing Parent and Children has been locked down.

What you did

Attempt to optimize spawning large batches of children in a custom command for big_space.

What went wrong

Profiling shows that applying system commands dominates system time. Spawning 320k entities took 31s.

The problem appears to be that even when spawning a brand new entity, bevy is doing reparenting checks for every child when its Parent component is added, and again for every child when the children are added to the parent entity's Children component. These checks are completely unnecessary - the entities are new and cannot yet exist in the hierarchy.

By doing some transmute shenanigans, I was able to see what a niave solution would look like if I could simply construct the Parent and Children entities myself. The performance improvement was stark: what previously took 31s now took 83ms.

I was also able to work around this without unsafe by using reflection to construct a Parent and Children, bypassing the private constructors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-PerformanceA change motivated by improving speed, memory usage or compile timesD-ModestA "normal" level of difficulty; suitable for simple features or challenging fixesS-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions