godot
godot copied to clipboard
Optimize `Object::notification` method performance
Revision 1
Optimize Object::notification method performance
This PR optimizes the Object::notification() function by splitting it into two specialized functions: notification_forward() and notification_reverse(). This change significantly reduces the CPU usage of this frequently called function.
Key changes:
- Split
Object::notification()intonotification_forward()andnotification_reverse() - Update
notification()to act as a simple dispatcher to the appropriate specialized function
Revision 2
Refactor Object::notification using if constexpr and templates
Key changes:
- Implemented the optimization using C++17's
if constexprand template specialization. - The compiler now generates separate, optimized code paths for reversed and non-reversed cases.
Before
Before: 5.7% CPU usage inside Object::notification(). Hotspot is referring to this line.
After
After: 1.49% CPU usage inside Object::_notification_fast()
This optimization results in a significant reduction in CPU usage for this function.
The performance improvement is achieved by:
- Eliminating conditional branching within the function
- Allowing better compiler optimizations for each specialized case
- Improving code locality and potential for inlining
Testing
Tested locally with a scene that contains 20K cubes, all of them were moving around. Link to the test project: https://github.com/CrazyRoka/godot-benchmark-moving-cubes/tree/main
Would it make sense to use template and constexpr if to avoid duplicating the code inside the function?
Please squash your commits into one, see here
I see you're introducing a new convention for template arguments (t_ prefix). Not a big deal since we don't have uniformity in that regard across the codebase. However, it may be better to choose one of the already used ones (like the suggested all-caps, or CamelCase).
That said, t_ looks great to me as a potential new convention for the whole codebase later.
Good idea, we use these template functions in the same way already in a few places for optimization. :+1:
Superseded by #104381. Thanks for the contribution!