diff --git a/contributing/development/core_and_modules/core_types.rst b/contributing/development/core_and_modules/core_types.rst index bcf7d2d6efa..0ad76ff4757 100644 --- a/contributing/development/core_and_modules/core_types.rst +++ b/contributing/development/core_and_modules/core_types.rst @@ -20,20 +20,17 @@ more difficult to read. In general, care is not taken to use the most efficient datatype for a given task unless using large structures or arrays. ``int`` is used through most of the code unless necessary. This is done because nowadays -every device has at least a 32 bits bus and can do such operations in +every device has at least a 32-bit bus and can do such operations in one cycle. It makes code more readable too. -For files or memory sizes, ``size_t`` is used, which is warranted to be -64 bits. +For files or memory sizes, ``size_t`` is used, which is guaranteed to be +64-bit. For Unicode characters, CharType instead of wchar_t is used, because many architectures have 4 bytes long wchar_t, where 2 bytes might be desired. However, by default, this has not been forced and CharType maps directly to wchar_t. -References: -~~~~~~~~~~~ - - `core/typedefs.h `__ Memory model @@ -63,7 +60,7 @@ remain constant. In other words, leave 10-20% of your memory free and perform all small allocations and you are fine. Godot ensures that all objects that can be allocated dynamically are -small (less than a few kb at most). But what happens if an allocation is +small (less than a few kB at most). But what happens if an allocation is too large (like an image or mesh geometry or large array)? In this case Godot has the option to use a dynamic memory pool. This memory needs to be locked to be accessed, and if an allocation runs out of memory, the @@ -85,20 +82,21 @@ For C-style allocation, Godot provides a few macros: memrealloc() memfree() -These are equivalent to the usual malloc, realloc, free of the standard C -library. +These are equivalent to the usual ``malloc()``, ``realloc()``, and ``free()`` +of the C standard library. For C++-style allocation, special macros are provided: .. code-block:: none - memnew( Class / Class(args) ) - memdelete( instance ) + memnew(Class / Class(args)) + memdelete(instance) - memnew_arr( Class , amount ) - memdelete_arr( pointer to array ) + memnew_arr(Class, amount) + memdelete_arr(pointer_to_array) -which are equivalent to new, delete, new[] and delete[]. +These are equivalent to ``new``, ``delete``, ``new[]``, and ``delete[]`` +respectively. memnew/memdelete also use a little C++ magic and notify Objects right after they are created, and right before they are deleted. @@ -128,84 +126,105 @@ its storage strategy. Prefer ``Vector<>`` (or ``LocalVector<>``) over ``List<>`` unless you're sure you need it, as cache locality and memory fragmentation tend to be more important with small collections. -References: -~~~~~~~~~~~ - - `core/os/memory.h `__ Containers ---------- -Godot provides also a set of common containers: - -- Vector -- List -- Set -- Map - -They aim to be as minimal as possible, as templates -in C++ are often inlined and make the binary size much fatter, both in -debug symbols and code. List, Set and Map can be iterated using -pointers, like this: - -.. code-block:: cpp - - for(List::Element *E=somelist.front();E;E=E->next()) { - print_line(E->get()); // print the element - } - -The Vector<> class also has a few nice features: - -- It does copy on write, so making copies of it is cheap as long as - they are not modified. -- It supports multi-threading, by using atomic operations on the - reference counter. - -References: -~~~~~~~~~~~ - -- `core/templates/vector.h `__ -- `core/templates/list.h `__ -- `core/templates/set.h `__ -- `core/templates/map.h `__ - -String ------- - -Godot also provides a String class. This class has a huge amount of -features, full Unicode support in all the functions (like case -operations) and utf8 parsing/extracting, as well as helpers for -conversion and visualization. - -References: -~~~~~~~~~~~ - -- `core/string/ustring.h `__ - -StringName ----------- - -StringNames are like a String, but they are unique. Creating a -StringName from a string results in a unique internal pointer for all -equal strings. StringNames are useful for using strings as -identifier, as comparing them is basically comparing a pointer. - -Creation of a StringName (especially a new one) is slow, but comparison -is fast. - -References: -~~~~~~~~~~~ - -- `core/string/string_name.h `__ +Godot provides its own set of containers, which means STL containers like ``std::string`` +and ``std::vector`` are generally not used in the codebase. + +A 📜 icon denotes the type is part of :ref:`Variant `. This +means it can be used as a parameter or return value of a method exposed to the +scripting API. + ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| Godot datatype | Closest C++ STL datatype | Comment | ++=======================+==========================+=======================================================================================+ +| |string| 📜 | ``std::string`` | **Use this as the "default" string type.** ``String`` uses UTF-32 encoding | +| | | to improve performance thanks to its fixed character size. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |vector| | ``std::vector`` | **Use this as the "default" vector type.** Uses copy-on-write (COW) semantics. | +| | | This means it's generally slower but can be copied around almost for free. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |hash_set| | ``std::unordered_set`` | **Use this as the "default" set type.** | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |hash_map| | ``std::unordered_map`` | **Use this as the "default" map type.** Preserves insertion order. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |string_name| 📜 | ``std::string`` | Uses string interning for fast comparisons. Use this for static strings that are | +| | | referenced frequently and used in multiple locations in the engine. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |local_vector| | ``std::vector`` | Closer to ``std::vector`` in semantics. In most situations, ``Vector`` should be | +| | | preferred. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |array| 📜 | ``std::vector`` | Values can be of any Variant type. No static typing is imposed. | +| | | Uses shared reference counting, similar to ``std::shared_ptr``. | +| | | Uses Vector internally. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |typed_array| 📜 | ``std::vector`` | Subclass of ``Array`` but with static typing for its elements. | +| | | Not to be confused with ``Packed*Array``, which is internally a ``Vector``. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |packed_array| 📜 | ``std::vector`` | Alias of ``Vector``, e.g. ``PackedColorArray = Vector``. | +| | | Only a limited list of packed array types are available | +| | | (use ``TypedArray`` otherwise). | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |list| | ``std::list`` | Linked list type. Generally slower than other array/vector types. Prefer using | +| | | other types in new code, unless using ``List`` avoids the need for type conversions. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |fixed_vector| | ``std::array`` | Vector with a fixed capacity (more similar to ``boost::container::static_vector``). | +| | | This container type is more efficient than other vector-like types because it makes | +| | | no heap allocations. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |span| | ``std::span`` | Represents read-only access to a contiguous array without needing to copy any data. | +| | | See `pull request description `__ | +| | | for details. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |rb_set| | ``std::set`` | Uses a `red-black tree `__ | +| | | for faster access. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |v_set| | ``std::flat_set`` | Uses copy-on-write (COW) semantics. | +| | | This means it's generally slower but can be copied around almost for free. | +| | | The performance benefits of ``VSet`` aren't established, so prefer using other types. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |a_hash_map| | ``std::unordered_map`` | Array-based implementation of a hash map. Does not preserve insertion order. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |rb_map| | ``std::map`` | Uses a `red-black tree `__ | +| | | for faster access. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |dictionary| 📜 | ``std::unordered_map`` | Keys and values can be of any Variant type. No static typing is imposed. | +| | | Uses shared reference counting, similar to ``std::shared_ptr``. | +| | | Preserves insertion order. Uses ``HashMap`` internally. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |typed_dictionary| 📜 | ``std::unordered_map`` | Subclass of ``Dictionary`` but with static typing for its keys and values. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ +| |pair| | ``std::pair`` | Stores a single key-value pair. | ++-----------------------+--------------------------+---------------------------------------------------------------------------------------+ + +.. |string| replace:: `String `__ +.. |vector| replace:: `Vector `__ +.. |hash_set| replace:: `HashSet `__ +.. |hash_map| replace:: `HashMap `__ +.. |string_name| replace:: `StringName `__ +.. |local_vector| replace:: `LocalVector `__ +.. |array| replace:: `Array `__ +.. |typed_array| replace:: `TypedArray `__ +.. |packed_array| replace:: `Packed*Array `__ +.. |list| replace:: `List `__ +.. |fixed_vector| replace:: `FixedVector `__ +.. |span| replace:: `Span `__ +.. |rb_set| replace:: `RBSet `__ +.. |v_set| replace:: `VSet `__ +.. |a_hash_map| replace:: `AHashMap `__ +.. |rb_map| replace:: `RBMap `__ +.. |dictionary| replace:: `Dictionary `__ +.. |typed_dictionary| replace:: `TypedDictionary `__ +.. |pair| replace:: `Pair `__ Math types ---------- -There are several linear math types available in the core/math -directory. - -References: -~~~~~~~~~~~ +There are several linear math types available in the ``core/math`` +directory: - `core/math `__ @@ -213,22 +232,16 @@ NodePath -------- This is a special datatype used for storing paths in a scene tree and -referencing them fast. - -References: -~~~~~~~~~~~ +referencing them in an optimized manner: - `core/string/node_path.h `__ RID --- -RIDs are resource IDs. Servers use these to reference data stored in +RIDs are *Resource IDs*. Servers use these to reference data stored in them. RIDs are opaque, meaning that the data they reference can't be accessed directly. RIDs are unique, even for different types of -referenced data. - -References: -~~~~~~~~~~~ +referenced data: - `core/templates/rid.h `__ diff --git a/contributing/development/cpp_usage_guidelines.rst b/contributing/development/cpp_usage_guidelines.rst index 1f96ecc35ff..06967e1c47e 100644 --- a/contributing/development/cpp_usage_guidelines.rst +++ b/contributing/development/cpp_usage_guidelines.rst @@ -55,75 +55,13 @@ as Godot provides its own data types (among other things). See :ref:`doc_faq_why_not_stl` for more information. This means that pull requests should **not** use ``std::string``, -``std::vector`` and the like. Instead, use Godot's datatypes as described below. +``std::vector`` and the like. Instead, use Godot's datatypes as described in +the :ref:`doc_core_types` documentation. + A 📜 icon denotes the type is part of :ref:`Variant `. This means it can be used as a parameter or return value of a method exposed to the scripting API. -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| Godot datatype | Closest C++ STL datatype | Comment | -+========================+==========================+=======================================================================================+ -| ``String`` 📜 | ``std::string`` | **Use this as the "default" string type.** ``String`` uses UTF-32 encoding | -| | | to improve performance thanks to its fixed character size. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``StringName`` 📜 | ``std::string`` | Uses string interning for fast comparisons. Use this for static strings that are | -| | | referenced frequently and used in multiple locations in the engine. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``Vector`` | ``std::vector`` | **Use this as the "default" vector type.** Uses copy-on-write (COW) semantics. | -| | | This means it's generally slower but can be copied around almost for free. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``LocalVector`` | ``std::vector`` | Closer to ``std::vector`` in semantics. In most situations, ``Vector`` should be | -| | | preferred. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``Array`` 📜 | ``std::vector`` | Values can be of any Variant type. No static typing is imposed. | -| | | Uses shared reference counting, similar to ``std::shared_ptr``. | -| | | Uses Vector internally. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``TypedArray`` 📜 | ``std::vector`` | Subclass of ``Array`` but with static typing for its elements. | -| | | Not to be confused with ``Packed*Array``, which is internally a ``Vector``. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``Packed*Array`` 📜 | ``std::vector`` | Alias of ``Vector``, e.g. ``PackedColorArray = Vector``. | -| | | Only a limited list of packed array types are available | -| | | (use ``TypedArray`` otherwise). | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``List`` | ``std::list`` | Linked list type. Generally slower than other array/vector types. Prefer using | -| | | other types in new code, unless using ``List`` avoids the need for type conversions. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``FixedVector`` | ``std::array`` | Vector with a fixed capacity (more similar to ``boost::container::static_vector``). | -| | | This container type is more efficient than other vector-like types because it makes | -| | | no heap allocations. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``Span`` | ``std::span`` | Represents read-only access to a contiguous array without needing to copy any data. | -| | | See `pull request description `__ | -| | | for details. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``HashSet`` | ``std::unordered_set`` | **Use this as the "default" set type.** | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``RBSet`` | ``std::set`` | Uses a `red-black tree `__ | -| | | for faster access. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``VSet`` | ``std::flat_set`` | Uses copy-on-write (COW) semantics. | -| | | This means it's generally slower but can be copied around almost for free. | -| | | The performance benefits of ``VSet`` aren't established, so prefer using other types. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``HashMap`` | ``std::unordered_map`` | **Use this as the "default" map type.** Preserves insertion order. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``AHashMap`` | ``std::unordered_map`` | Array-based implementation of a hash map. Does not preserve insertion order. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``OAHashMap`` | ``std::unordered_map`` | Does not preserve insertion order. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``RBMap`` | ``std::map`` | Uses a `red-black tree `__ | -| | | for faster access. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``Dictionary`` 📜 | ``std::unordered_map`` | Keys and values can be of any Variant type. No static typing is imposed. | -| | | Uses shared reference counting, similar to ``std::shared_ptr``. | -| | | Preserves insertion order. Uses ``HashMap`` internally. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``TypedDictionary`` 📜 | ``std::unordered_map`` | Subclass of ``Dictionary`` but with static typing for its keys and values. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ -| ``Pair`` | ``std::pair`` | Stores a single key-value pair. | -+------------------------+--------------------------+---------------------------------------------------------------------------------------+ - ``auto`` keyword ~~~~~~~~~~~~~~~~