Skip to content

bevy_reflect: streamline generated FromReflect::from_reflect#19906

Merged
alice-i-cecile merged 1 commit intobevyengine:mainfrom
nnethercote:from_reflect
Jul 2, 2025
Merged

bevy_reflect: streamline generated FromReflect::from_reflect#19906
alice-i-cecile merged 1 commit intobevyengine:mainfrom
nnethercote:from_reflect

Conversation

@nnethercote
Copy link
Contributor

Objective

Generated from_reflect methods use closures in a weird way, e.g.:

    x: (|| {
        <f32 as ::bevy::reflect::FromReflect>::from_reflect(
            ::bevy::reflect::Struct::field(__ref_struct, "x")?,
        )   
    })()?,

The reason for this is because when #[reflect(Default)] is used, you instead get stuff like this:

    if let ::core::option::Option::Some(__field) = (|| {
        <f32 as ::bevy::reflect::FromReflect>::from_reflect(
            ::bevy::reflect::Struct::field(__ref_struct, "x")?,
        )   
    })() {
        __this.x = __field;
    } 

and the closure is necessary to contain the scope of the ?. But the first case is more common.

Helps with #19873.

Solution

Avoid the closure in the common case.

Testing

I used cargo expand to confirm the closures are no longer produced in the common case.

-Zmacro-stats output tells me this reduces the size of the Reflect code produced for bevy_ui by 0.5%.

@alice-i-cecile alice-i-cecile added C-Performance A change motivated by improving speed, memory usage or compile times S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it A-Reflection Runtime information about types labels Jul 2, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jul 2, 2025
Merged via the queue into bevyengine:main with commit 1b4cf02 Jul 2, 2025
39 checks passed
@nnethercote nnethercote deleted the from_reflect branch July 2, 2025 21:29
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 S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants