godot icon indicating copy to clipboard operation
godot copied to clipboard

GDScript: Perform validated calls with static methods

Open vnen opened this issue 9 months ago • 2 comments

When the types are validated at compile time, this type of call runs faster. It is already used for instance methods, this adds this optimization to native static methods as well.

PS: Viewing the diff without whitespace makes it easier to review the changes. The opcodes table increased by one character, so the alignment creates a bigger diff.

vnen avatar Apr 26 '24 00:04 vnen

Thanks!

I'm not really qualified to review the code changes, since I don't know GDScript's internals very well.

However, I did try using this PR with godot-cpp's automated tests, which includes some static method calls that GDScript should do as validated calls. To be sure that a validated call to GDExtension was happening, I added some debug print statements:

diff --git a/core/extension/gdextension.cpp b/core/extension/gdextension.cpp
index 22a5df9935..a1c4606c80 100644
--- a/core/extension/gdextension.cpp
+++ b/core/extension/gdextension.cpp
@@ -259,6 +259,10 @@ public:
                ERR_FAIL_COND_MSG(vararg, "Vararg methods don't have validated call support. This is most likely an engine bug.");
                GDExtensionClassInstancePtr extension_instance = is_static() ? nullptr : p_object->_get_extension_instance();
 
+               if (is_static()) {
+                       print_line("validated_call: ", name);
+               }
+
                if (validated_call_func) {
                        // This is added here, but it's unlikely to be provided by most extensions.
                        validated_call_func(method_userdata, extension_instance, reinterpret_cast<GDExtensionConstVariantPtr *>(p_args), (GDExtensionVariantPtr)r_ret);

And, it printed out the function names I was expecting, as well as passed the tests!

dsnopek avatar Apr 26 '24 21:04 dsnopek

Not directly related to this PR, but is it possible to call static methods via reflection? I wanted to simulate falling back to regular calls (if no type information is available), but found it's only possible via actual instances.

Bromeon avatar Apr 30 '24 20:04 Bromeon

Thanks!

akien-mga avatar May 01 '24 08:05 akien-mga