Skip to content

Optimize Object::cast_to<T>() using Object::is_class() now that it takes a StringName#1974

Merged
dsnopek merged 1 commit intogodotengine:masterfrom
dsnopek:better-object-cast-to
May 4, 2026
Merged

Optimize Object::cast_to<T>() using Object::is_class() now that it takes a StringName#1974
dsnopek merged 1 commit intogodotengine:masterfrom
dsnopek:better-object-cast-to

Conversation

@dsnopek
Copy link
Copy Markdown
Collaborator

@dsnopek dsnopek commented Apr 21, 2026

After godotengine/godot#118582, in Godot 4.7, Object::is_class() now takes a StringName which allows us to write a much simpler and faster version of Object::cast_to<T>() that doesn't use dynamic_cast<T>()

I've also chosen to simplify the code for Godot 4.6 and earlier to only do the dynamic_cast<T>(). Since we have to do that in the end anyway, we may as well skip all the other junk. This is theoretically slower if the object can't be cast to the requested type, but faster if it can, so I expect it to be a wash

This is marked as a DRAFT because it depends on #1972

@dsnopek dsnopek added this to the 10.x milestone Apr 21, 2026
@dsnopek dsnopek requested a review from Ivorforce April 21, 2026 19:30
@dsnopek dsnopek requested a review from a team as a code owner April 21, 2026 19:30
@dsnopek dsnopek added the enhancement This is an enhancement on the current functionality label Apr 21, 2026
@dsnopek dsnopek marked this pull request as draft April 21, 2026 19:31
@dsnopek dsnopek force-pushed the better-object-cast-to branch from 134d83f to bc96761 Compare May 4, 2026 13:45
@dsnopek dsnopek changed the title [DRAFT] Optimize Object::cast_to<T>() using Object::is_class() now that it takes a StringName Optimize Object::cast_to<T>() using Object::is_class() now that it takes a StringName May 4, 2026
@dsnopek dsnopek marked this pull request as ready for review May 4, 2026 13:45
Copy link
Copy Markdown
Member

@Ivorforce Ivorforce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Agree with the decision to go with dynamic_cast plain in 4.6-.

@Ivorforce
Copy link
Copy Markdown
Member

By the way, should we deprecate classdb_get_class_tag and object_cast_to upstream? It feels like unnecessary complexity.

@dsnopek
Copy link
Copy Markdown
Collaborator Author

dsnopek commented May 4, 2026

should we deprecate classdb_get_class_tag and object_cast_to upstream?

That makes sense to me!

@dsnopek dsnopek merged commit 0d61c1c into godotengine:master May 4, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement This is an enhancement on the current functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants