-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
#[derive(Reflect)] generates excessive amounts of code #19873
Copy link
Copy link
Closed
Labels
A-ReflectionRuntime information about typesRuntime information about typesC-PerformanceA change motivated by improving speed, memory usage or compile timesA change motivated by improving speed, memory usage or compile timesD-MacrosCode that generates Rust codeCode that generates Rust codeS-Needs-InvestigationThis issue requires detective work to figure out what's going wrongThis issue requires detective work to figure out what's going wrong
Description
Activity
Metadata
Metadata
Assignees
Labels
A-ReflectionRuntime information about typesRuntime information about typesC-PerformanceA change motivated by improving speed, memory usage or compile timesA change motivated by improving speed, memory usage or compile timesD-MacrosCode that generates Rust codeCode that generates Rust codeS-Needs-InvestigationThis issue requires detective work to figure out what's going wrongThis issue requires detective work to figure out what's going wrong
I recently added a new
-Zmacro-statsoption to the Rust compiler, which prints measurements on the size of code generated by macros. I soon heard that Bevy's#[derive(Reflect)]is generating large amounts of code.For example, consider the
bevy_uicrate. It is around 16,000 lines of code, and 563,000 bytes of text. Here are the top five entries in the-Zmacro-statsoutput:All the
#[derive(Reflect)]invocations inbevy_uiproduce more than 1.5MB of source code, an average of 21KB per invocation. That's about 3x the size of the original code, and more than 15x the size of the next biggest derive.The generated code for a simple example can be seen here. It is very verbose and contains multiple redundancies.
All this code has to be processed by the compiler: parsed, type-checked, borrow-checked, optimized, and codegen'd. Reducing the size of the generated code could improve compile times for Bevy projects significantly.