Optimize Object::cast_to for final classes by using typeid instead of dynamic_cast.#103693
Closed
Ivorforce wants to merge 1 commit intogodotengine:masterfrom
Closed
Optimize Object::cast_to for final classes by using typeid instead of dynamic_cast.#103693Ivorforce wants to merge 1 commit intogodotengine:masterfrom
Object::cast_to for final classes by using typeid instead of dynamic_cast.#103693Ivorforce wants to merge 1 commit intogodotengine:masterfrom
Conversation
…d of `dynamic_cast`.
6729934 to
2b9d03e
Compare
Member
In 3.x we have some alternate implementation on some platforms (Android, web looks like): |
Member
Author
|
Closing because this is probably not needed after #103708 has been merged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Object::cast_tois used to check if an object is of a certain type. It uses RTTI (runtime type information) to do this (dynamic_cast). If the object type matches, it returns the pointer (reinterpreting the pointee), otherwise it returns anullptr.Explanation
For
finalclasses, this operation is as easy as checking the type ID itself, because the type cannot be inherited from. The inheritance tree does not need to be checked, which is the slowest part ofdynamic_cast.For some reason, compilers (at least
clang) are not optimizing this case. I'm not sure why. Either I'm missing something fundamental, or they just haven't managed to do it yet. In the future, this explicit check may become unnecessary.Benchmarks
I benchmarked a performance difference of at least
740x(see misses; due to a call that prevents inlining the loop the hits are slower).Code
This printed:
Caveats
It's not clear whether we need this PR yet:
Currently, (almost?) no
Objectderived classes are evenfinal.But for a lot of existing
Objectclasses, it doesn't make sense to derive from them. Some could probably be madefinalwithout much risk.Also, it is unclear to me whether
dynamic_castis used in any performance critical functions. There are around 2500 separate calls, but one would hope none are in per-tick functions.Still, I wanted to open this PR, just to show that this optimization is possible.