Skip to content

godot-cpp: PtrToArg::convert() creates unnecessary copies #1932

@NoctemCat

Description

@NoctemCat

Godot version

4.6.stable

godot-cpp version

master

System information

linux

Issue description

The code for PtrToArg is pretty old and can be updated to not make copies

#define MAKE_PTRARG(m_type)                                            \
	template <>                                                        \
	struct PtrToArg<m_type> {                                          \
		_FORCE_INLINE_ static m_type convert(const void *p_ptr) {      \
			return *reinterpret_cast<const m_type *>(p_ptr);           \
		}                                                              \
	};                                                                 \
	template <>                                                        \
	struct PtrToArg<const m_type &> {                                  \
		_FORCE_INLINE_ static m_type convert(const void *p_ptr) {      \
			return *reinterpret_cast<const m_type *>(p_ptr);           \
		}                                                              \
	}

Stumbled upon this while thinking about why we needed internal getter godotengine/godot#99201 , while it does looks like internal getter has its uses the engine itself had a similar issue godotengine/godot#80074 and merged pr that prevented copying, so I figure godot-cpp can also stop copying arguments it got by a pointer where it doesn't need to make copies.

Also the engine itself is using even newer PtrToArgDirect and PtrToArgByReference where it doesn't make copies for arguments that can be passed by reference.

Tbh, it's just an enhancement, as godot-cpp will work correctly even without this, but in marshalling less copies is always better

Steps to reproduce

Bind method with const T & argument

Minimal reproduction project

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis is an enhancement on the current functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions