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

Add FromReflect::from_reflect_owned #6419

Closed
Shatur opened this issue Oct 30, 2022 · 5 comments
Closed

Add FromReflect::from_reflect_owned #6419

Shatur opened this issue Oct 30, 2022 · 5 comments
Labels
A-Reflection Runtime information about types C-Performance A change motivated by improving speed, memory usage or compile times C-Usability A simple quality-of-life change that makes Bevy easier to use

Comments

@Shatur
Copy link
Contributor

Shatur commented Oct 30, 2022

What problem does this solve or what need does it fill?

Currently to apply a reflect value that was deserialized from bytes it copies all its fields. It would be nice to have an ability avoid this extra copy. For example, when doing networking and sending world state in a form of reflect - it could be a lot of extra copies.

What solution would you like?

Add from_reflect_owned to FromReflect trait.

Implementation strategy.

  1. Add ReflectOwned enum with boxes like ReflectRef to get owned version.
  2. Add reflect_owned method to Reflect like reflect_ref that returns ReflectOwned introduced above.
  3. Use drain method from [Merged by Bors] - bevy_reflect: Get owned fields #5728 for arrays / maps and add owned versions of things like Struct::field.
  4. Use all of the above to implement from_reflect_owned similar to non-owned version:
    fn from_reflect(reflect: &dyn #bevy_reflect_path::Reflect) -> Option<Self> {
    if let #bevy_reflect_path::ReflectRef::#ref_struct_type(#ref_struct) = reflect.reflect_ref() {
    #constructor
    } else {
    None
    }
@Shatur Shatur added C-Enhancement A new feature S-Needs-Triage This issue needs to be labelled labels Oct 30, 2022
@Shatur Shatur changed the title Add FromReflect::from_reflect_owned Add FromReflect::from_reflect_owned Oct 30, 2022
@alice-i-cecile alice-i-cecile added D-Trivial Nice and easy! A great choice to get started with Bevy C-Usability A simple quality-of-life change that makes Bevy easier to use A-Reflection Runtime information about types and removed C-Enhancement A new feature S-Needs-Triage This issue needs to be labelled labels Oct 30, 2022
@alice-i-cecile
Copy link
Member

Great writeup! This is a bit more advanced than most good-first-issues, but the detailed plan and existing methods to learn from should help a ton (assuming you're not planning to tackle this yourself).

@alice-i-cecile alice-i-cecile added the C-Performance A change motivated by improving speed, memory usage or compile times label Oct 30, 2022
@Shatur
Copy link
Contributor Author

Shatur commented Oct 30, 2022

I think I could try to tackle if people like this suggestion.

@alice-i-cecile
Copy link
Member

I like it a lot! The basic motivation is solid and the approach seems good.

@Shatur
Copy link
Contributor Author

Shatur commented Oct 30, 2022

Having an issue with implementing from_reflect_owned for structs. DynamicStruct stores its elements inside Vec:

fields: Vec<Box<dyn Reflect>>,

Non-owned version uses acess by index or field name:
field_names: Vec<Cow<'static, str>>,
field_indices: HashMap<Cow<'static, str>, usize>,

But in owned version I need to move out values somehow. Can't figure out how to do it, so help is appreciated (just take a look at todo!() in #6421).

@alice-i-cecile alice-i-cecile removed the D-Trivial Nice and easy! A great choice to get started with Bevy label Oct 30, 2022
@Shatur
Copy link
Contributor Author

Shatur commented Feb 17, 2024

No longer interested in it, closing.

@Shatur Shatur closed this as completed Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Reflection Runtime information about types C-Performance A change motivated by improving speed, memory usage or compile times C-Usability A simple quality-of-life change that makes Bevy easier to use
Projects
Status: Done
Development

No branches or pull requests

2 participants