Skip to content

Speed up the reflection#537

Merged
liuzicheng1987 merged 3 commits into
mainfrom
f/speed_up_reflection
Nov 2, 2025
Merged

Speed up the reflection#537
liuzicheng1987 merged 3 commits into
mainfrom
f/speed_up_reflection

Conversation

@liuzicheng1987
Copy link
Copy Markdown
Contributor

No description provided.

@gemini-code-assist
Copy link
Copy Markdown

Note

Gemini is unable to generate a summary for this pull request due to the file types involved not being currently supported.

@liuzicheng1987 liuzicheng1987 merged commit e47dfbe into main Nov 2, 2025
292 checks passed
@liuzicheng1987 liuzicheng1987 deleted the f/speed_up_reflection branch November 2, 2025 21:01
@ZXShady
Copy link
Copy Markdown
Contributor

ZXShady commented Nov 3, 2025

Any tests that proved it actually improved it?

@liuzicheng1987
Copy link
Copy Markdown
Contributor Author

@ZXShady , I have done a lot of profiling with Clang's -f-time-trace and to be honest, none of this is going to move the needle very much. You can replace all of the structs by rfl::NamedTuple thus completely eliminating the need for any struct reflection and there wouldn't be much of a change in compile time.

The bottleneck isn't the reflection, it is generating the code for serialization and deserialization. We have already done a lot of optimization there, such as implementing a non-recursive tuple (rfl::Tuple) class rather than using the recursively implemented std::tuple. glaze does something similar, but they use a third-party tuple implementation rather than their own. Same idea nonetheless.

However, I liked your idea of nesting the field extractions as a binary tree of constexpr if. It's easy to do and a clear win for larger structs. So I thought why not do it.

@liuzicheng1987
Copy link
Copy Markdown
Contributor Author

@ZXShady , I will show you what I mean. This is the profile for the test for the README function. The total duration of the struct reflection is about 92 milliseconds, or less than 5% of the total compile time.

image

@liuzicheng1987
Copy link
Copy Markdown
Contributor Author

liuzicheng1987 commented Nov 3, 2025

@ZXShady , here is the profile for the test_many_fields - test, which is a struct with 70 float fields.

This is obviously a construed example that is unlikely to ever occur in the real world. If you are going to have structs with that many fields, the fields are probably going to be more complex than just floats. In other words, this is probably the greatest share of time spent on struct reflection in any of our tests.

image

But even for this construed example...we talking about 20-25% of the total compile time. If you can cut that in half, you will have reduced the overall compile time by 10-12% or so.

But this is really an upper bound...in the real world it's going to be a lot less.

@ZXShady
Copy link
Copy Markdown
Contributor

ZXShady commented Nov 18, 2025

I wish github mobile actually worked and I got notifications when you pinged me.....

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