Provide quick access to Object
ancestry
#107868
Open
+86
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Forward port of #107462
See that PR for full details.
TLDR this internal functionality should speed up things like object casting by approx 2x in most cases.
Introduction
Although we have increased
Object::cast_to
performance significantly with #103708 (4.x) and #104825 (3.x) we discussed at the time that for some high performance bottleneck scenarios we may want an even faster way of determining whether anObject
is one of the key main types.At the time I trialed using a bitfield to store this info and it worked well, and is likely to be one of the fastest methods, and discussed this with @Ivorforce .
While it involves storing (and retrieving) data from the
Object / Node
(thus cache effects), it avoids overheads with a virtual function approach, and the virtual function requires reading thevtable
from the object, so there is a read in all cases.Benchmarks
2000 node children
release
ancestry 175, virtual 297, cast_to 344
debug
ancestry 2719, virtual 2804, cast_to 3386
Usage
Although ancestry can be used directly, the plan is (as with 3.x):
Object::cast_to
(no change to existing code).Object::_is_class
toObject::derives_from
for some other use cases (already done in 3.x in [3.x] Optimize hotspots withObject::derives_from
#107881), so that ancestry can then be hidden and only used internally.Notes
cast_to
to useAncestry
for the specific cases covered, so it will invisibly be used everywhere (see [3.x] OptimizeObject::cast_to
with ancestral classes when possible #107844 for 3.x version).type_is_reference
flag onObject
, as that is no longer necessary now we haveAncestralClass::REF_COUNTED
.Object
constructor, which seems to be in order to accommodateRefCounted
. I worked out that this seems to becauseadd_instance()
requires being able to check whether the object is a reference during the call. In the long term we should probably see if calling that can be done later in theRefCounted
constructor, so theObject
constructor can be kept simple.