Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Object::get_class_name #75797

Merged
merged 1 commit into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,15 @@ bool Object::_predelete() {
_predelete_ok = 1;
notification(NOTIFICATION_PREDELETE, true);
if (_predelete_ok) {
_class_ptr = nullptr; //must restore so destructors can access class ptr correctly
_class_name_ptr = nullptr; // Must restore, so constructors/destructors have proper class name access at each stage.
}
return _predelete_ok;
}

void Object::_postinitialize() {
_class_ptr = _get_class_namev();
_class_name_ptr = _get_class_namev(); // Set the direct pointer, which is much faster to obtain, but can only happen after postinitialize.
_initialize_classv();
_class_name_ptr = nullptr; // May have been called from a constructor.
notification(NOTIFICATION_POSTINITIALIZE);
}

Expand Down
30 changes: 15 additions & 15 deletions core/object/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ public: \
#define GDCLASS(m_class, m_inherits) \
private: \
void operator=(const m_class &p_rval) {} \
mutable StringName _class_name; \
friend class ::ClassDB; \
\
public: \
Expand All @@ -388,13 +387,11 @@ public:
return String(#m_class); \
} \
virtual const StringName *_get_class_namev() const override { \
if (_get_extension()) { \
return &_get_extension()->class_name; \
} \
if (!_class_name) { \
_class_name = get_class_static(); \
static StringName _class_name_static; \
if (unlikely(!_class_name_static)) { \
StringName::assign_static_unique_class_name(&_class_name_static, #m_class); \
} \
return &_class_name; \
return &_class_name_static; \
} \
static _FORCE_INLINE_ void *get_class_ptr_static() { \
static int ptr; \
Expand Down Expand Up @@ -614,8 +611,7 @@ class Object {
Variant script; // Reference does not exist yet, store it in a Variant.
HashMap<StringName, Variant> metadata;
HashMap<StringName, Variant *> metadata_properties;
mutable StringName _class_name;
mutable const StringName *_class_ptr = nullptr;
mutable const StringName *_class_name_ptr = nullptr;

void _add_user_signal(const String &p_name, const Array &p_args = Array());
bool _has_user_signal(const StringName &p_name) const;
Expand Down Expand Up @@ -714,10 +710,11 @@ class Object {
Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);

virtual const StringName *_get_class_namev() const {
if (!_class_name) {
_class_name = get_class_static();
static StringName _class_name_static;
if (unlikely(!_class_name_static)) {
StringName::assign_static_unique_class_name(&_class_name_static, "Object");
}
return &_class_name;
return &_class_name_static;
}

Vector<StringName> _get_meta_list_bind() const;
Expand Down Expand Up @@ -788,13 +785,16 @@ class Object {

_FORCE_INLINE_ const StringName &get_class_name() const {
if (_extension) {
// Can't put inside the unlikely as constructor can run it
return _extension->class_name;
}
if (!_class_ptr) {

if (unlikely(!_class_name_ptr)) {
// While class is initializing / deinitializing, constructors and destructurs
// need access to the proper class at the proper stage.
return *_get_class_namev();
} else {
return *_class_ptr;
}
return *_class_name_ptr;
}

/* IAPI */
Expand Down
8 changes: 8 additions & 0 deletions core/string/string_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ StringName::StringName(const StringName &p_name) {
}
}

void StringName::assign_static_unique_class_name(StringName *ptr, const char *p_name) {
mutex.lock();
if (*ptr == StringName()) {
*ptr = StringName(p_name, true);
}
mutex.unlock();
}

StringName::StringName(const char *p_name, bool p_static) {
_data = nullptr;

Expand Down
2 changes: 2 additions & 0 deletions core/string/string_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ class StringName {
StringName(const String &p_name, bool p_static = false);
StringName(const StaticCString &p_static_string, bool p_static = false);
StringName() {}

static void assign_static_unique_class_name(StringName *ptr, const char *p_name);
_FORCE_INLINE_ ~StringName() {
if (likely(configured) && _data) { //only free if configured
unref();
Expand Down