Skip to content

Commit

Permalink
Merge pull request #75797 from reduz/optimize-object-get-class-name
Browse files Browse the repository at this point in the history
Optimize Object::get_class_name
  • Loading branch information
akien-mga authored Apr 8, 2023
2 parents 61630d4 + 8950943 commit cfab3d2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
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

0 comments on commit cfab3d2

Please sign in to comment.