Cache function pointers in global tables by Bromeon 路 Pull Request #387 路 godot-rust/gdext (original) (raw)
馃敟 Detailed article here: https://godot-rust.github.io/dev/ffi-optimizations-benchmarking
Instead of repeatedly loading method pointers from Godot for each FFI call (including all the expensive StringName
construction), this PR preloads all function pointers at startup. Includes:
- Classes
- Builtin types
- Builtin lifecycle functions (constructors, destructors, conversion methods)
- Global utility functions
This not only improves performance (#11), but also reveals any incompatibilitities or missing methods in Godot immediately. For example, this helped reveal godotengine/godot#80852. On the downside, a single method can prevent gdext from loading (even if that method is not used); however this likely points to an incompatibility that needs to be addressed anyway.
Other changes in this PR include:
StringCache
for cachingStringName
instances during initialization- New
init
API which is easier to use and closer to what Godot provides- remove
ExtensionLayer
,InitHandle
- remove
Still missing:
- Classes which are loaded after init-levels (currently only
ThemeDB
) - Tests for
Editor
init-level (our binaries don't boot the editor, so that init-level is skipped in CI) - Testing of
ExtensionLibrary::min_level()
- Turns out the lower layer callbacks are still called even if
Scene
is specified as a minimum. What is its use then? - Is that even correct? If not, is the new API still suitable (previous API could infer minimum based on registered layer)?
- Turns out the lower layer callbacks are still called even if
After everything's been done, the codegen crate needs a heavy refactoring. Contributors planning to change that area are recommended to wait a bit.