godot icon indicating copy to clipboard operation
godot copied to clipboard

Optimize `Object::notification` method performance

Open CrazyRoka opened this issue 1 year ago • 3 comments

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() into notification_forward() and notification_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 constexpr and 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.

image

After

After: 1.49% CPU usage inside Object::_notification_fast()

image


image

This optimization results in a significant reduction in CPU usage for this function.

The performance improvement is achieved by:

  1. Eliminating conditional branching within the function
  2. Allowing better compiler optimizations for each specialized case
  3. 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

CrazyRoka avatar Jul 11 '24 22:07 CrazyRoka

Would it make sense to use template and constexpr if to avoid duplicating the code inside the function?

alvinhochun avatar Jul 12 '24 07:07 alvinhochun

Please squash your commits into one, see here

AThousandShips avatar Jul 20 '24 08:07 AThousandShips

Please squash your commits into one, see here

Done

CrazyRoka avatar Jul 27 '24 20:07 CrazyRoka

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.

RandomShaper avatar Dec 03 '24 06:12 RandomShaper

Good idea, we use these template functions in the same way already in a few places for optimization. :+1:

lawnjelly avatar Mar 19 '25 12:03 lawnjelly

Superseded by #104381. Thanks for the contribution!

akien-mga avatar Apr 01 '25 09:04 akien-mga