Trouv / bevy_ecs_ldtk

ECS-friendly ldtk plugin for bevy, leveraging bevy_ecs_tilemap
Other
664 stars 75 forks source link

refactor: change #[from_entity_instance] to use references #149

Closed neocturne closed 1 year ago

neocturne commented 1 year ago

EntityInstance is a rather large struct, and cloning it requires allocating for several fields. Despite this fact, each use of the #[from_entity_instance] attribute was cloning the struct once, wasting CPU time for no good reason - especially for Bundles that contain several #[from_entity_instance] fields.

Change the attribute to work on From<&EntityInstance> instead of From, avoiding the implicit clone in most cases. The clone is still necessary when a plain EntityInstance field exists in a struct deriving LdtkEntity, which is handled by having EntityInstance implement From<&EntityInstance>.

While being a breaking change, fixing up users should be trivial - in most cases no owned EntityInstance is needed at all, and where it is an explicit clone can be inserted.

Trouv commented 1 year ago

This makes perfect sense, thanks for the contribution. I bet there's several places in the repo where I was a bit too clone-happy where I could have borrowed. This is definitely a breaking change though, so I'll mark it as such in the squashed commit.