Skip to content

Commit 23dcc32

Browse files
committed
Move C++ container type information from C++ usage guidelines to Core types
- Update Core types for Godot 4.4.
1 parent 6b8faf3 commit 23dcc32

File tree

2 files changed

+107
-143
lines changed

2 files changed

+107
-143
lines changed

contributing/development/core_and_modules/core_types.rst

Lines changed: 104 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ more difficult to read.
2020
In general, care is not taken to use the most efficient datatype for a
2121
given task unless using large structures or arrays. ``int`` is used
2222
through most of the code unless necessary. This is done because nowadays
23-
every device has at least a 32 bits bus and can do such operations in
23+
every device has at least a 32-bits bus and can do such operations in
2424
one cycle. It makes code more readable too.
2525

26-
For files or memory sizes, ``size_t`` is used, which is warranted to be
27-
64 bits.
26+
For files or memory sizes, ``size_t`` is used, which is guaranteed to be
27+
64-bit.
2828

2929
For Unicode characters, CharType instead of wchar_t is used, because
3030
many architectures have 4 bytes long wchar_t, where 2 bytes might be
@@ -63,7 +63,7 @@ remain constant. In other words, leave 10-20% of your memory free
6363
and perform all small allocations and you are fine.
6464

6565
Godot ensures that all objects that can be allocated dynamically are
66-
small (less than a few kb at most). But what happens if an allocation is
66+
small (less than a few KB at most). But what happens if an allocation is
6767
too large (like an image or mesh geometry or large array)? In this case
6868
Godot has the option to use a dynamic memory pool. This memory needs to
6969
be locked to be accessed, and if an allocation runs out of memory, the
@@ -85,20 +85,21 @@ For C-style allocation, Godot provides a few macros:
8585
memrealloc()
8686
memfree()
8787
88-
These are equivalent to the usual malloc, realloc, free of the standard C
89-
library.
88+
These are equivalent to the usual ``malloc()``, ``realloc()``, and ``free()``
89+
of the C standard library.
9090

9191
For C++-style allocation, special macros are provided:
9292

9393
.. code-block:: none
9494
95-
memnew( Class / Class(args) )
96-
memdelete( instance )
95+
memnew(Class / Class(args))
96+
memdelete(instance)
9797
98-
memnew_arr( Class , amount )
99-
memdelete_arr( pointer to array )
98+
memnew_arr(Class, amount)
99+
memdelete_arr(pointer_to_array)
100100
101-
which are equivalent to new, delete, new[] and delete[].
101+
These are equivalent to ``new``, ``delete``, ``new[]`` and ``delete[]``
102+
respectively.
102103

103104
memnew/memdelete also use a little C++ magic and notify Objects right
104105
after they are created, and right before they are deleted.
@@ -128,107 +129,132 @@ its storage strategy. Prefer ``Vector<>`` (or ``LocalVector<>``) over
128129
``List<>`` unless you're sure you need it, as cache locality and memory
129130
fragmentation tend to be more important with small collections.
130131

131-
References:
132-
~~~~~~~~~~~
132+
References
133+
~~~~~~~~~~
133134

134135
- `core/os/memory.h <https://github.com/godotengine/godot/blob/master/core/os/memory.h>`__
135136

136137
Containers
137138
----------
138139

139-
Godot provides also a set of common containers:
140-
141-
- Vector
142-
- List
143-
- Set
144-
- Map
145-
146-
They aim to be as minimal as possible, as templates
147-
in C++ are often inlined and make the binary size much fatter, both in
148-
debug symbols and code. List, Set and Map can be iterated using
149-
pointers, like this:
150-
151-
.. code-block:: cpp
152-
153-
for(List<int>::Element *E=somelist.front();E;E=E->next()) {
154-
print_line(E->get()); // print the element
155-
}
156-
157-
The Vector<> class also has a few nice features:
158-
159-
- It does copy on write, so making copies of it is cheap as long as
160-
they are not modified.
161-
- It supports multi-threading, by using atomic operations on the
162-
reference counter.
163-
164-
References:
165-
~~~~~~~~~~~
140+
Godot provides its own set of containers, which means STL containers like ``std::string``
141+
and ``std::vector`` are genearally not used in the codebase.
142+
143+
A 📜 icon denotes the type is part of :ref:`Variant <doc_variant_class>`. This
144+
means it can be used as a parameter or return value of a method exposed to the
145+
scripting API.
146+
147+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
148+
| Godot datatype | Closest C++ STL datatype | Comment |
149+
+========================+==========================+=======================================================================================+
150+
| ``String`` 📜 | ``std::string`` | **Use this as the "default" string type.** ``String`` uses UTF-32 encoding |
151+
| | | to improve performance thanks to its fixed character size. |
152+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
153+
| ``StringName`` 📜 | ``std::string`` | Uses string interning for fast comparisons. Use this for static strings that are |
154+
| | | referenced frequently and used in multiple locations in the engine. |
155+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
156+
| ``Vector`` | ``std::vector`` | **Use this as the "default" vector type.** Uses copy-on-write (COW) semantics. |
157+
| | | This means it's generally slower but can be copied around almost for free. |
158+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
159+
| ``LocalVector`` | ``std::vector`` | Closer to ``std::vector`` in semantics. In most situations, ``Vector`` should be |
160+
| | | preferred. |
161+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
162+
| ``Array`` 📜 | ``std::vector`` | Values can be of any Variant type. No static typing is imposed. |
163+
| | | Uses shared reference counting, similar to ``std::shared_ptr``. |
164+
| | | Uses Vector<Variant> internally. |
165+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
166+
| ``TypedArray`` 📜 | ``std::vector`` | Subclass of ``Array`` but with static typing for its elements. |
167+
| | | Not to be confused with ``Packed*Array``, which is internally a ``Vector``. |
168+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
169+
| ``Packed*Array`` 📜 | ``std::vector`` | Alias of ``Vector``, e.g. ``PackedColorArray = Vector<Color>``. |
170+
| | | Only a limited list of packed array types are available |
171+
| | | (use ``TypedArray`` otherwise). |
172+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
173+
| ``List`` | ``std::list`` | Linked list type. Generally slower than other array/vector types. Prefer using |
174+
| | | other types in new code, unless using ``List`` avoids the need for type conversions. |
175+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
176+
| ``FixedVector`` | ``std::array`` | Vector with a fixed capacity (more similar to ``boost::container::static_vector``). |
177+
| | | This container type is more efficient than other vector-like types because it makes |
178+
| | | no heap allocations. |
179+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
180+
| ``Span`` | ``std::span`` | Represents read-only access to a contiguous array without needing to copy any data. |
181+
| | | See `pull request description <https://github.com/godotengine/godot/pull/100293>`__ |
182+
| | | for details. |
183+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
184+
| ``HashSet`` | ``std::unordered_set`` | **Use this as the "default" set type.** |
185+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
186+
| ``RBSet`` | ``std::set`` | Uses a `red-black tree <https://en.wikipedia.org/wiki/Red-black_tree>`__ |
187+
| | | for faster access. |
188+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
189+
| ``VSet`` | ``std::flat_set`` | Uses copy-on-write (COW) semantics. |
190+
| | | This means it's generally slower but can be copied around almost for free. |
191+
| | | The performance benefits of ``VSet`` aren't established, so prefer using other types. |
192+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
193+
| ``HashMap`` | ``std::unordered_map`` | **Use this as the "default" map type.** Preserves insertion order. |
194+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
195+
| ``AHashMap`` | ``std::unordered_map`` | Array-based implementation of a hash map. Does not preserve insertion order. |
196+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
197+
| ``OAHashMap`` | ``std::unordered_map`` | Does not preserve insertion order. |
198+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
199+
| ``RBMap`` | ``std::map`` | Uses a `red-black tree <https://en.wikipedia.org/wiki/Red-black_tree>`__ |
200+
| | | for faster access. |
201+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
202+
| ``Dictionary`` 📜 | ``std::unordered_map`` | Keys and values can be of any Variant type. No static typing is imposed. |
203+
| | | Uses shared reference counting, similar to ``std::shared_ptr``. |
204+
| | | Preserves insertion order. Uses ``HashMap<Variant>`` internally. |
205+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
206+
| ``TypedDictionary`` 📜 | ``std::unordered_map`` | Subclass of ``Dictionary`` but with static typing for its keys and values. |
207+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
208+
| ``Pair`` | ``std::pair`` | Stores a single key-value pair. |
209+
+------------------------+--------------------------+---------------------------------------------------------------------------------------+
210+
211+
References
212+
~~~~~~~~~~
166213

167214
- `core/templates/vector.h <https://github.com/godotengine/godot/blob/master/core/templates/vector.h>`__
215+
- `core/templates/local_vector.h <https://github.com/godotengine/godot/blob/master/core/templates/local_vector.h>`__
168216
- `core/templates/list.h <https://github.com/godotengine/godot/blob/master/core/templates/list.h>`__
169-
- `core/templates/set.h <https://github.com/godotengine/godot/blob/master/core/templates/hash_set.h>`__
170-
- `core/templates/map.h <https://github.com/godotengine/godot/blob/master/core/templates/hash_map.h>`__
171-
172-
String
173-
------
174-
175-
Godot also provides a String class. This class has a huge amount of
176-
features, full Unicode support in all the functions (like case
177-
operations) and utf8 parsing/extracting, as well as helpers for
178-
conversion and visualization.
179-
180-
References:
181-
~~~~~~~~~~~
182-
217+
- `core/templates/hash_set.h <https://github.com/godotengine/godot/blob/master/core/templates/hash_set.h>`__
218+
- `core/templates/rb_set.h <https://github.com/godotengine/godot/blob/master/core/templates/rb_set.h>`__
219+
- `core/templates/span.h <https://github.com/godotengine/godot/blob/master/core/templates/span.h>`__
220+
- `core/templates/hash_map.h <https://github.com/godotengine/godot/blob/master/core/templates/hash_map.h>`__
221+
- `core/templates/rb_map.h <https://github.com/godotengine/godot/blob/master/core/templates/rb_map.h>`__
222+
- `core/templates/a_hash_map.h <https://github.com/godotengine/godot/blob/master/core/templates/a_hash_map.h>`__
223+
- `core/templates/oa_hash_map.h <https://github.com/godotengine/godot/blob/master/core/templates/oa_hash_map.h>`__
183224
- `core/string/ustring.h <https://github.com/godotengine/godot/blob/master/core/string/ustring.h>`__
184-
185-
StringName
186-
----------
187-
188-
StringNames are like a String, but they are unique. Creating a
189-
StringName from a string results in a unique internal pointer for all
190-
equal strings. StringNames are useful for using strings as
191-
identifier, as comparing them is basically comparing a pointer.
192-
193-
Creation of a StringName (especially a new one) is slow, but comparison
194-
is fast.
195-
196-
References:
197-
~~~~~~~~~~~
198-
199225
- `core/string/string_name.h <https://github.com/godotengine/godot/blob/master/core/string/string_name.h>`__
200226

201227
Math types
202228
----------
203229

204-
There are several linear math types available in the core/math
230+
There are several linear math types available in the ``core/math``
205231
directory.
206232

207-
References:
208-
~~~~~~~~~~~
233+
References
234+
~~~~~~~~~~
209235

210236
- `core/math <https://github.com/godotengine/godot/tree/master/core/math>`__
211237

212238
NodePath
213239
--------
214240

215241
This is a special datatype used for storing paths in a scene tree and
216-
referencing them fast.
242+
referencing them in an optimized manner.
217243

218-
References:
219-
~~~~~~~~~~~
244+
References
245+
~~~~~~~~~~
220246

221247
- `core/string/node_path.h <https://github.com/godotengine/godot/blob/master/core/string/node_path.h>`__
222248

223249
RID
224250
---
225251

226-
RIDs are resource IDs. Servers use these to reference data stored in
252+
RIDs are *resource IDs*. Servers use these to reference data stored in
227253
them. RIDs are opaque, meaning that the data they reference can't be
228254
accessed directly. RIDs are unique, even for different types of
229255
referenced data.
230256

231-
References:
232-
~~~~~~~~~~~
257+
References
258+
~~~~~~~~~~
233259

234260
- `core/templates/rid.h <https://github.com/godotengine/godot/blob/master/core/templates/rid.h>`__

0 commit comments

Comments
 (0)