Skip to content

Commit 149a4b4

Browse files
committed
Merge pull request #107868 from lawnjelly/quick_ancestry4
Provide quick access to `Object` ancestry
2 parents 326b221 + 14a8145 commit 149a4b4

18 files changed

+86
-9
lines changed

core/io/resource.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,9 @@ void Resource::_bind_methods() {
758758
}
759759

760760
Resource::Resource() :
761-
remapped_list(this) {}
761+
remapped_list(this) {
762+
_define_ancestry(AncestralClass::RESOURCE);
763+
}
762764

763765
Resource::~Resource() {
764766
if (unlikely(path_cache.is_empty())) {

core/object/object.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2228,9 +2228,20 @@ void Object::reset_internal_extension(ObjectGDExtension *p_extension) {
22282228
#endif
22292229

22302230
void Object::_construct_object(bool p_reference) {
2231-
type_is_reference = p_reference;
2231+
_block_signals = false;
2232+
_can_translate = true;
2233+
_emitting = false;
2234+
2235+
// ObjectDB::add_instance relies on AncestralClass::REF_COUNTED
2236+
// being already set in the case of references.
2237+
_ancestry = p_reference ? (uint32_t)AncestralClass::REF_COUNTED : 0;
2238+
22322239
_instance_id = ObjectDB::add_instance(this);
22332240

2241+
#ifdef TOOLS_ENABLED
2242+
_edited = false;
2243+
#endif
2244+
22342245
#ifdef DEBUG_ENABLED
22352246
_lock_index.init(1);
22362247
#endif

core/object/object.h

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,29 @@ class Object {
582582
CONNECT_INHERITED = 32, // Used in editor builds.
583583
};
584584

585+
// Store on each object a bitfield to quickly test whether it is derived from some "key" classes
586+
// that are commonly tested in performance sensitive code.
587+
// Ensure unsigned to bitpack.
588+
enum class AncestralClass : unsigned int {
589+
REF_COUNTED = 1 << 0,
590+
NODE = 1 << 1,
591+
RESOURCE = 1 << 2,
592+
SCRIPT = 1 << 3,
593+
594+
CANVAS_ITEM = 1 << 4,
595+
CONTROL = 1 << 5,
596+
NODE_2D = 1 << 6,
597+
COLLISION_OBJECT_2D = 1 << 7,
598+
AREA_2D = 1 << 8,
599+
600+
NODE_3D = 1 << 9,
601+
VISUAL_INSTANCE_3D = 1 << 10,
602+
GEOMETRY_INSTANCE_3D = 1 << 11,
603+
COLLISION_OBJECT_3D = 1 << 12,
604+
PHYSICS_BODY_3D = 1 << 13,
605+
MESH_INSTANCE_3D = 1 << 14,
606+
};
607+
585608
struct Connection {
586609
::Signal signal;
587610
Callable callable;
@@ -623,16 +646,19 @@ class Object {
623646
#ifdef DEBUG_ENABLED
624647
SafeRefCount _lock_index;
625648
#endif // DEBUG_ENABLED
626-
bool _block_signals = false;
627649
int _predelete_ok = 0;
628650
ObjectID _instance_id;
629651
bool _predelete();
630652
void _initialize();
631653
void _postinitialize();
632-
bool _can_translate = true;
633-
bool _emitting = false;
654+
655+
uint32_t _ancestry : 15;
656+
657+
bool _block_signals : 1;
658+
bool _can_translate : 1;
659+
bool _emitting : 1;
634660
#ifdef TOOLS_ENABLED
635-
bool _edited = false;
661+
bool _edited : 1;
636662
uint32_t _edited_version = 0;
637663
HashSet<String> editor_section_folding;
638664
#endif
@@ -658,7 +684,6 @@ class Object {
658684
_FORCE_INLINE_ void _construct_object(bool p_reference);
659685

660686
friend class RefCounted;
661-
bool type_is_reference = false;
662687

663688
BinaryMutex _instance_binding_mutex;
664689
struct InstanceBinding {
@@ -764,6 +789,7 @@ class Object {
764789
static void _get_property_list_from_classdb(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator);
765790

766791
bool _disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force = false);
792+
void _define_ancestry(AncestralClass p_class) { _ancestry |= (uint32_t)p_class; }
767793

768794
virtual bool _uses_signal_mutex() const;
769795

@@ -838,6 +864,8 @@ class Object {
838864
}
839865
virtual bool is_class_ptr(void *p_ptr) const { return get_class_ptr_static() == p_ptr; }
840866

867+
bool has_ancestry(AncestralClass p_class) const { return _ancestry & (uint32_t)p_class; }
868+
841869
const StringName &get_class_name() const;
842870

843871
StringName get_class_name_for_extension(const GDExtension *p_library) const;
@@ -996,7 +1024,7 @@ class Object {
9961024

9971025
void clear_internal_resource_paths();
9981026

999-
_ALWAYS_INLINE_ bool is_ref_counted() const { return type_is_reference; }
1027+
_ALWAYS_INLINE_ bool is_ref_counted() const { return has_ancestry(AncestralClass::REF_COUNTED); }
10001028

10011029
void cancel_free();
10021030

core/object/ref_counted.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ bool RefCounted::unreference() {
9595

9696
RefCounted::RefCounted() :
9797
Object(true) {
98+
_define_ancestry(AncestralClass::REF_COUNTED);
9899
refcount.init();
99100
refcount_init.init();
100101
}

core/object/script_language.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ class Script : public Resource {
197197

198198
virtual const Variant get_rpc_config() const = 0;
199199

200-
Script() {}
200+
Script() {
201+
_define_ancestry(AncestralClass::SCRIPT);
202+
}
201203
};
202204

203205
class ScriptLanguage : public Object {

scene/2d/node_2d.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,7 @@ void Node2D::_bind_methods() {
510510
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "global_skew", PROPERTY_HINT_NONE, "radians_as_degrees", PROPERTY_USAGE_NONE), "set_global_skew", "get_global_skew");
511511
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "global_transform", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NONE), "set_global_transform", "get_global_transform");
512512
}
513+
514+
Node2D::Node2D() {
515+
_define_ancestry(AncestralClass::NODE_2D);
516+
}

scene/2d/node_2d.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,6 @@ class Node2D : public CanvasItem {
115115
Transform2D get_relative_transform_to_parent(const Node *p_parent) const;
116116

117117
Transform2D get_transform() const override;
118+
119+
Node2D();
118120
};

scene/2d/physics/area_2d.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,8 @@ void Area2D::_bind_methods() {
678678

679679
Area2D::Area2D() :
680680
CollisionObject2D(PhysicsServer2D::get_singleton()->area_create(), true) {
681+
_define_ancestry(AncestralClass::AREA_2D);
682+
681683
set_gravity(980);
682684
set_gravity_direction(Vector2(0, 1));
683685
set_monitoring(true);

scene/2d/physics/collision_object_2d.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,8 @@ void CollisionObject2D::_bind_methods() {
655655
}
656656

657657
CollisionObject2D::CollisionObject2D(RID p_rid, bool p_area) {
658+
_define_ancestry(AncestralClass::COLLISION_OBJECT_2D);
659+
658660
rid = p_rid;
659661
area = p_area;
660662
pickable = true;
@@ -672,6 +674,7 @@ CollisionObject2D::CollisionObject2D(RID p_rid, bool p_area) {
672674
}
673675

674676
CollisionObject2D::CollisionObject2D() {
677+
_define_ancestry(AncestralClass::COLLISION_OBJECT_2D);
675678
//owner=
676679

677680
set_notify_transform(true);

scene/3d/mesh_instance_3d.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ void MeshInstance3D::_bind_methods() {
925925
}
926926

927927
MeshInstance3D::MeshInstance3D() {
928+
_define_ancestry(AncestralClass::MESH_INSTANCE_3D);
928929
}
929930

930931
MeshInstance3D::~MeshInstance3D() {

0 commit comments

Comments
 (0)