Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 9, 2023

Conversation

neocturne
Copy link
Contributor

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.

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<EntityInstance>, 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
Copy link
Owner

Trouv commented Jan 9, 2023

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.

@Trouv Trouv merged commit 246880f into Trouv:main Jan 9, 2023
Trouv added a commit that referenced this pull request Mar 31, 2023
🤖 I have created a release *beep* *boop*
---


##
[0.6.0](v0.5.0...v0.6.0)
(2023-03-31)


### ⚠ BREAKING CHANGES

* In addition to updating to bevy 0.10, users may need define order
between `LdtkSystemSet::ProcessApi` and other 3rd party system sets,
like
[rapier](https://github.com/Trouv/bevy_ecs_ldtk/blob/5b8f17cc51f91ff9aedbed8afca560e750b557c8/examples/platformer/main.rs#L17).
* change LdtkEntity's #[with] attribute to borrow EntityInstance
([#158](#158))
* split `RegisterLdtkObjects` into two new traits with a different
naming convention
([#155](#155))
* change #[from_entity_instance] to use references
([#149](#149))

### Features

* add `#[sprite_sheet_bundle(no_grid)]` attribute for generating a
single-texture `TextureAtlas` instead of a grid
([#161](#161))
([d6d3c9c](d6d3c9c))
* add `with` attribute for LdtkIntCell derive macro
([#157](#157))
([d3fbd3c](d3fbd3c))
* add LevelSet::from_iid method
([#144](#144))
([fb17ae1](fb17ae1))
* add render feature for headless mode (tilemaps only)
([#159](#159))
([2f8000e](2f8000e))
* change #[from_entity_instance] to use references
([#149](#149))
([246880f](246880f))
* change LdtkEntity's #[with] attribute to borrow EntityInstance
([#158](#158))
([c052b31](c052b31))
* register TileMetadata and TileEnumTags types
([#153](#153))
([26cae15](26cae15))
* register types `GridCoords` and `LayerMetadata`
([#146](#146))
([ed4a0f9](ed4a0f9))
* upgrade to bevy 0.10
([#168](#168))
([5b8f17c](5b8f17c))


### Bug Fixes

* use normal sprite for background color instead of tile
([#171](#171))
([b22b11d](b22b11d))


### Example Changes

* improve ground detection in platformer example
([#137](#137))
([cafba57](cafba57))
* use rect_builder buffer instead of row-specific current_rects in
spawn_wall_collisions
([#147](#147))
([45303f3](45303f3))


### Code Refactors

* split `RegisterLdtkObjects` into two new traits with a different
naming convention
([#155](#155))
([156ae8c](156ae8c))


### Documentation Changes

* explain feature flags in crate-level documentation
([#164](#164))
([a832da0](a832da0))
* explain that sprite_bundle should not be used with tilemap editor
visuals ([#142](#142))
([1a7a8a1](1a7a8a1))
* repair doc links to bevy in app module
([#154](#154))
([0f928e8](0f928e8))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Trevor Lovell <[email protected]>
@neocturne neocturne deleted the entity-instance-ref branch August 11, 2023 18:11
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