godotengine / godotengine/godot-cpp
Creating new instances of a custom class also instances NativeScript resource dupes
- Dominant language
- C++
- Stars
- 2.7k
- Forks
- 809
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 8
Description
What this does looks a bit suspicious: https://github.com/GodotNativeTools/godot-cpp/blob/e97e86648355cbe61c6249483c6f2a5d1b41714f/include/core/Godot.hpp#L42
This code runs when you create new instances of a custom class from C++ (from what I searched so far, it doesnt seem to be used by the framework or by Godot, only by user code). Script instances must be created through a `NativeScript` instance, but here that resource is created on the fly every time. The script instance then gets the only one to refer to it, which is how it gets freed later.
Script *instances* should only *refer* to the *same* NativeScript resource (which represents the "class" itself), not create new copies on every instance. This causes extra allocation work and uses more memory.
The problem in making such resources unique is that they are usually obtained by doing `load("the_class.gdns")`, which in turn exists in order to integrate to Godot's resource system. It is difficult to do that right in this function, which explains the current implementation.
What can be done at least, is to cache the resources in an array indexed by a class ID generated at registration, like what's done in the D bindings? It won't ensure unicity but at least it reduces clutter.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.