-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Reflect auto registration #15030
base: main
Are you sure you want to change the base?
Reflect auto registration #15030
Conversation
…atic registration
…omatic reflect registration functionality at compile time
Is this compared to the equivalent manual registration? Can / should we feature flag this for authors who particularly care? |
You added a new feature but didn't update the readme. Please run |
As far as I understand it, wasm-init works by exporting a bunch of symbols to final wasm and calling them from outside when |
All sizes are in KB as reported by
Update: Disregard that, I misunderstood how reflection works and missed registering type data. |
Automatic reflect registration registeres all reflect types within testing module. This creates multiple type registrations with the same type path, which breaks `ReflectDeserializer` in `bevy_reflect` tests.
Registering types on `AppTypeRegistry` creation breaks dynamic scene builder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this! I like that you updated examples, but can you also add some unit tests?
Added a basic test for
What other tests do you have in mind? I'm not really sure what else should be tested here, so If you have any ideas I'd love to hear them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do a more thorough review tonight, but at first glance I believe this is compatible with my no_std
work for non-wasm
platforms. On wasm
, I will need to replace that RwLock
with something else (probably spin::RwLock
). However, no action needs to be taken here from a no_std
perspective, I'll update my PR if this gets merged first, or provide a change-request in the reverse.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few comments. Overall looks really clean!
Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! I think this is a good compromise between fully automatic registration and fully manual registration. It's completely opt-in via a feature flag and even opt-in on a per-registry basis.
I do think we should recommend libraries—including Bevy—manually register their types whenever possible to make it easier for downstream users to opt-out of this behavior.
But overall, this is going to make things a lot cleaner and easier for our users!
Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com>
You added a new example but didn't add metadata for it. Please update the root Cargo.toml file. |
Objective
This PR adds automatic registration of non-generic types annotated with
#[derive(Reflect)]
.Manually registering all reflect types can be error-prone, failing to reflect during runtime if some type is not registered. #5781 fixed this for nested types, improving ergonomics of using reflection dramatically, but top-level types still need to be registered manually. This is especially painful when prototyping and using inspector to change values of components.
Solution
Automatically register all types that derive Reflect. This works for all non-generic types, allowing users to just add
#[derive(Reflect)]
to any compatible type and not think about where to register it.Automatic registration can be opted-out of by adding
#[reflect(no_auto_register)]
reflect attribute to a type:Registration normally happens at app creation time, but
TypeRegistry
itself has a newregister_derived_types
method to register all collected types at any appropriate time:Testing
reflect
example and removing explicit type registration, both on native and onwasm32-unknown-unknown
.register_derived_types
Impact on startup time is negligible: <40ms on debug wasm build and <25ms on debug native build for 1614 registered types .
Wasm binary size comparison. All sizes are in KB as reported by
du
on the modifiedreflection
example.Considerations
While automatic type registration improves ergonomics quite a bit for projects that make heavy use of it, there are also some problems:
1000 simple structs with reflect wasm size comparison, no engine types