GDExtension: PtrToArg::convert()
creates unnecessary copies
#80074
Labels
Milestone
PtrToArg::convert()
creates unnecessary copies
#80074
Godot version
4.2-dev (branched off 75f9c97)
System information
Windows 10
Issue description
Stumbled upon this while debugging equality between
StringName
instances (for unrelated issue #78580).The GDExtension function pointer for equality calls
OperatorEvaluatorEqual<StringName, StringName>::ptr_evaluate()
.This one checks
PtrToArg<A>::convert(left) == PtrToArg<B>::convert(right)
.The function
PtrToArg::convert()
is defined as follows (substitutedm_type
withStringName
):reinterpret_cast
returns an expression of typeconst StringName*
.Dereferencing that yields
const StringName&
(and notStringName
).The return type of
convert()
however isStringName
(by value).Unless I'm missing something, the C++ compiler cannot apply RVO here because we don't have a value, only a const-reference.
So this calls the copy-constructor
StringName::StringName(const StringName &p_name)
-- I verified this via debugger.This copy is unnecessary for many operations which only have read access, like equality here. Even if
StringName
is cheap to copy, this still comes with refcount inc/dec + locking, when it would mostly need to compare two hashes.Steps to reproduce
Equality-compare two
StringName
objects in a GDExtension binding and step in via debugger. Copy constructor ofStringName
is invoked.Minimal reproduction project
N/A
The text was updated successfully, but these errors were encountered: