Skip to content

Commit 1d74b41

Browse files
committed
Provide quick access to Object ancestry
1 parent d7bdc0b commit 1d74b41

18 files changed

+85
-26
lines changed

core/io/resource.cpp

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

754754
Resource::Resource() :
755-
remapped_list(this) {}
755+
remapped_list(this) {
756+
_define_ancestry(AncestralClass::RESOURCE);
757+
}
756758

757759
Resource::~Resource() {
758760
if (unlikely(path_cache.is_empty())) {

core/object/object.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,23 +2203,23 @@ void Object::reset_internal_extension(ObjectGDExtension *p_extension) {
22032203
}
22042204
#endif
22052205

2206-
void Object::_construct_object(bool p_reference) {
2207-
type_is_reference = p_reference;
2206+
Object::Object() {
2207+
_block_signals = false;
2208+
_can_translate = true;
2209+
_emitting = false;
2210+
_ancestry = 0;
2211+
22082212
_instance_id = ObjectDB::add_instance(this);
22092213

2214+
#ifdef TOOLS_ENABLED
2215+
_edited = false;
2216+
#endif
2217+
22102218
#ifdef DEBUG_ENABLED
22112219
_lock_index.init(1);
22122220
#endif
22132221
}
22142222

2215-
Object::Object(bool p_reference) {
2216-
_construct_object(p_reference);
2217-
}
2218-
2219-
Object::Object() {
2220-
_construct_object(false);
2221-
}
2222-
22232223
void Object::detach_from_objectdb() {
22242224
if (_instance_id != ObjectID()) {
22252225
ObjectDB::remove_instance(this);

core/object/object.h

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,29 @@ class Object {
587587
CONNECT_INHERITED = 32, // Used in editor builds.
588588
};
589589

590+
// Store on each object a bitfield to quickly test whether it is derived from some "key" classes
591+
// that are commonly tested in performance sensitive code.
592+
// Ensure unsigned to bitpack.
593+
enum class AncestralClass : unsigned int {
594+
REF_COUNTED = 1 << 0,
595+
NODE = 1 << 1,
596+
RESOURCE = 1 << 2,
597+
SCRIPT = 1 << 3,
598+
599+
CANVAS_ITEM = 1 << 4,
600+
CONTROL = 1 << 5,
601+
NODE_2D = 1 << 6,
602+
COLLISION_OBJECT_2D = 1 << 7,
603+
AREA_2D = 1 << 8,
604+
605+
NODE_3D = 1 << 9,
606+
VISUAL_INSTANCE_3D = 1 << 10,
607+
GEOMETRY_INSTANCE_3D = 1 << 11,
608+
COLLISION_OBJECT_3D = 1 << 12,
609+
PHYSICS_BODY_3D = 1 << 13,
610+
MESH_INSTANCE_3D = 1 << 14,
611+
};
612+
590613
struct Connection {
591614
::Signal signal;
592615
Callable callable;
@@ -628,16 +651,19 @@ class Object {
628651
#ifdef DEBUG_ENABLED
629652
SafeRefCount _lock_index;
630653
#endif // DEBUG_ENABLED
631-
bool _block_signals = false;
632654
int _predelete_ok = 0;
633655
ObjectID _instance_id;
634656
bool _predelete();
635657
void _initialize();
636658
void _postinitialize();
637-
bool _can_translate = true;
638-
bool _emitting = false;
659+
660+
uint32_t _ancestry : 15;
661+
662+
bool _block_signals : 1;
663+
bool _can_translate : 1;
664+
bool _emitting : 1;
639665
#ifdef TOOLS_ENABLED
640-
bool _edited = false;
666+
bool _edited : 1;
641667
uint32_t _edited_version = 0;
642668
HashSet<String> editor_section_folding;
643669
#endif
@@ -660,11 +686,6 @@ class Object {
660686
Variant _get_indexed_bind(const NodePath &p_name) const;
661687
int _get_method_argument_count_bind(const StringName &p_name) const;
662688

663-
_FORCE_INLINE_ void _construct_object(bool p_reference);
664-
665-
friend class RefCounted;
666-
bool type_is_reference = false;
667-
668689
BinaryMutex _instance_binding_mutex;
669690
struct InstanceBinding {
670691
void *binding = nullptr;
@@ -675,8 +696,6 @@ class Object {
675696
InstanceBinding *_instance_bindings = nullptr;
676697
uint32_t _instance_binding_count = 0;
677698

678-
Object(bool p_reference);
679-
680699
protected:
681700
StringName _translation_domain;
682701

@@ -769,6 +788,7 @@ class Object {
769788
static void _get_property_list_from_classdb(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator);
770789

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

773793
virtual bool _uses_signal_mutex() const;
774794

@@ -845,6 +865,8 @@ class Object {
845865
}
846866
virtual bool is_class_ptr(void *p_ptr) const { return get_class_ptr_static() == p_ptr; }
847867

868+
bool has_ancestry(AncestralClass p_class) const { return _ancestry & (uint32_t)p_class; }
869+
848870
const StringName &get_class_name() const;
849871

850872
StringName get_class_name_for_extension(const GDExtension *p_library) const;
@@ -1003,7 +1025,7 @@ class Object {
10031025

10041026
void clear_internal_resource_paths();
10051027

1006-
_ALWAYS_INLINE_ bool is_ref_counted() const { return type_is_reference; }
1028+
_ALWAYS_INLINE_ bool is_ref_counted() const { return has_ancestry(AncestralClass::REF_COUNTED); }
10071029

10081030
void cancel_free();
10091031

core/object/ref_counted.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ bool RefCounted::unreference() {
9393
return die;
9494
}
9595

96-
RefCounted::RefCounted() :
97-
Object(true) {
96+
RefCounted::RefCounted() {
97+
_define_ancestry(AncestralClass::REF_COUNTED);
9898
refcount.init();
9999
refcount_init.init();
100100
}

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() {

scene/3d/node_3d.cpp

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

15301530
Node3D::Node3D() :
15311531
xform_change(this), _client_physics_interpolation_node_3d_list(this) {
1532+
_define_ancestry(AncestralClass::NODE_3D);
1533+
15321534
// Default member initializer for bitfield is a C++20 extension, so:
15331535

15341536
data.top_level = false;

scene/3d/physics/collision_object_3d.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,13 +746,17 @@ PackedStringArray CollisionObject3D::get_configuration_warnings() const {
746746
}
747747

748748
CollisionObject3D::CollisionObject3D() {
749+
_define_ancestry(AncestralClass::COLLISION_OBJECT_3D);
750+
749751
set_notify_transform(true);
750752
//owner=
751753

752754
//set_transform_notify(true);
753755
}
754756

755757
CollisionObject3D::~CollisionObject3D() {
758+
_define_ancestry(AncestralClass::COLLISION_OBJECT_3D);
759+
756760
ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
757761
PhysicsServer3D::get_singleton()->free(rid);
758762
}

scene/3d/physics/physics_body_3d.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ PackedStringArray PhysicsBody3D::get_configuration_warnings() const {
220220
return warnings;
221221
}
222222

223+
PhysicsBody3D::PhysicsBody3D() {
224+
_define_ancestry(AncestralClass::PHYSICS_BODY_3D);
225+
}
226+
223227
///////////////////////////////////////
224228

225229
//so, if you pass 45 as limit, avoid numerical precision errors when angle is 45.

scene/3d/physics/physics_body_3d.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ class PhysicsBody3D : public CollisionObject3D {
6565
TypedArray<PhysicsBody3D> get_collision_exceptions();
6666
void add_collision_exception_with(Node *p_node); //must be physicsbody
6767
void remove_collision_exception_with(Node *p_node);
68+
69+
PhysicsBody3D();
6870
};

scene/3d/visual_instance_3d.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ RID VisualInstance3D::get_base() const {
204204
}
205205

206206
VisualInstance3D::VisualInstance3D() {
207+
_define_ancestry(AncestralClass::VISUAL_INSTANCE_3D);
208+
207209
instance = RenderingServer::get_singleton()->instance_create();
208210
RenderingServer::get_singleton()->instance_attach_object_instance_id(instance, get_instance_id());
209211
_set_notify_transform_when_fti_off(true);
@@ -645,6 +647,7 @@ void GeometryInstance3D::_bind_methods() {
645647
}
646648

647649
GeometryInstance3D::GeometryInstance3D() {
650+
_define_ancestry(AncestralClass::GEOMETRY_INSTANCE_3D);
648651
}
649652

650653
GeometryInstance3D::~GeometryInstance3D() {

scene/gui/control.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4399,6 +4399,8 @@ void Control::_bind_methods() {
43994399
}
44004400

44014401
Control::Control() {
4402+
_define_ancestry(AncestralClass::CONTROL);
4403+
44024404
data.theme_owner = memnew(ThemeOwner(this));
44034405

44044406
set_physics_interpolation_mode(Node::PHYSICS_INTERPOLATION_MODE_OFF);

scene/main/canvas_item.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,8 @@ CanvasItem::TextureRepeat CanvasItem::get_texture_repeat_in_tree() const {
17271727

17281728
CanvasItem::CanvasItem() :
17291729
xform_change(this) {
1730+
_define_ancestry(AncestralClass::CANVAS_ITEM);
1731+
17301732
canvas_item = RenderingServer::get_singleton()->canvas_item_create();
17311733
}
17321734

scene/main/node.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4031,6 +4031,8 @@ String Node::_get_name_num_separator() {
40314031
}
40324032

40334033
Node::Node() {
4034+
_define_ancestry(AncestralClass::NODE);
4035+
40344036
orphan_node_count++;
40354037

40364038
// Default member initializer for bitfield is a C++20 extension, so:

0 commit comments

Comments
 (0)