Skip to content

Commit 08745d4

Browse files
authored
Merge pull request #10382 from esainane/having-a-cow
Briefly document Vector<> variations
2 parents e66144f + 6db14b4 commit 08745d4

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

contributing/development/core_and_modules/core_types.rst

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,30 @@ which are equivalent to new, delete, new[] and delete[].
103103
memnew/memdelete also use a little C++ magic and notify Objects right
104104
after they are created, and right before they are deleted.
105105

106-
For dynamic memory, use Vector<>.
106+
For dynamic memory, use one of Godot's sequence types such as ``Vector<>``
107+
or ``LocalVector<>``. ``Vector<>`` behaves much like an STL ``std::vector<>``,
108+
but is simpler and uses Copy-On-Write (CoW) semantics. CoW copies of
109+
``Vector<>`` can safely access the same data from different threads, but
110+
several threads cannot access the same ``Vector<>`` instance safely.
111+
It can be safely passed via public API if it has a ``Packed`` alias.
112+
113+
The ``Packed*Array`` :ref:`types <doc_gdscript_packed_arrays>` are aliases
114+
for specific ``Vector<*>`` types (e.g., ``PackedByteArray``,
115+
``PackedInt32Array``) that are accessible via GDScript. Outside of core,
116+
prefer using the ``Packed*Array`` aliases for functions exposed to scripts,
117+
and ``Vector<>`` for other occasions.
118+
119+
``LocalVector<>`` is much more like ``std::vector`` than ``Vector<>``.
120+
It is non-CoW, with less overhead. It is intended for internal use where
121+
the benefits of CoW are not needed. Note that neither ``LocalVector<>``
122+
nor ``Vector<>`` are drop-in replacements for each other. They are two
123+
unrelated types with similar interfaces, both using a buffer as their
124+
storage strategy.
125+
126+
``List<>`` is another Godot sequence type, using a doubly-linked list as
127+
its storage strategy. Prefer ``Vector<>`` (or ``LocalVector<>``) over
128+
``List<>`` unless you're sure you need it, as cache locality and memory
129+
fragmentation tend to be more important with small collections.
107130

108131
References:
109132
~~~~~~~~~~~

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,8 @@ native or user class, or enum. Nested array types (like ``Array[Array[int]]``) a
890890
The only exception was made for the ``Array`` (``Array[Variant]``) type, for user convenience
891891
and compatibility with old code. However, operations on untyped arrays are considered unsafe.
892892

893+
.. _doc_gdscript_packed_arrays:
894+
893895
Packed arrays
894896
^^^^^^^^^^^^^
895897

0 commit comments

Comments
 (0)