Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Briefly document Vector<> variations #10382

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

esainane
Copy link
Contributor

@esainane esainane commented Dec 7, 2024

This continues #10329.

Packed*Array aliases seem universally preferred where available, so a link to the list of types seems appropriate.

LocalVector is used sparingly, so mentioning the intent and rough tradeoff involved seems right for an overview.

Bugsquad edit: closes #6259.

`Packed*Array` aliases seem universally preferred where available, so
a link to the list of types seems appropriate.

`LocalVector` is used sparingly, so mentioning the intent and rough
tradeoff involved seems right for an overview.
@esainane
Copy link
Contributor Author

esainane commented Dec 7, 2024

Please do check my understanding is correct - this is based off my reading of the source, and commentary from #10329, but there may be nuances which I'm missing.

Copy link
Contributor

@tetrapod00 tetrapod00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving only for style, and for not seeing anything obviously wrong from my casual understanding of the Godot codebase.

It's good to go as long as it is correct, IMO.

@@ -103,7 +103,18 @@ which are equivalent to new, delete, new[] and delete[].
memnew/memdelete also use a little C++ magic and notify Objects right
after they are created, and right before they are deleted.

For dynamic memory, use Vector<>.
For dynamic memory, use Godot's ``Vector<>`` or one of its variations.
Godot's ``Vector<>`` behaves much like an STL ``std::vector<>``, but is simpler,
Copy link
Contributor

@Ivorforce Ivorforce Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vector is not fully thread safe. CoW copies of Vector can safely access the same data from different threads, but several threads cannot access the same Vector instance safely.


The ``Packed*Array`` :ref:`types <doc_gdscript_packed_arrays>` are aliases for
specific ``Vector<*>`` types (e.g., ``PackedByteArray``, ``PackedInt32Array``)
that are accessible via GDScript. Prefer using the ``Packed*Array`` aliases
Copy link
Contributor

@Ivorforce Ivorforce Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't seen this be done very much in core, is using Packed aliases actually preferred?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core might be the exception, as the aliases are defined in Variant. Virtually everything outside of core will include Variant by default, so in those instances I've generally seen the aliases used more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal interpretation is to use Packed*Array for functions exposed to scripts, and Vector<> for other occasions.

that are accessible via GDScript. Prefer using the ``Packed*Array`` aliases
when available.

``LocalVector<>`` is a non-COW version, with less overhead. It is intended for
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LocalVector is much more like std::vector than Vector. Maybe it should be covered first, with the intro sentence of comparing to STL std::vector?

@@ -103,7 +103,18 @@ which are equivalent to new, delete, new[] and delete[].
memnew/memdelete also use a little C++ magic and notify Objects right
after they are created, and right before they are deleted.

For dynamic memory, use Vector<>.
For dynamic memory, use Godot's ``Vector<>`` or one of its variations.
Copy link
Contributor

@Ivorforce Ivorforce Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LocalVector is not a variation of Vector. Rather, they're two unrelated types with similar interfaces, and both have a buffer as its storage strategy. I'd rather phrase it like "Use one of the sequence types" or something like that. Although List has a very different storage strategy than both Vector and LocalVector, it should probably be mentioned here too (though I would hesitate to recommend to actually use it since it's a linked list).

For dynamic memory, use Godot's ``Vector<>`` or one of its variations.
Godot's ``Vector<>`` behaves much like an STL ``std::vector<>``, but is simpler,
thread safe, and uses Copy-On-Write semantics.
It can be safely passed via public API.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only if it has a Packed alias, I think :)

@AThousandShips AThousandShips added enhancement area:contributing Issues and PRs related to the Contributing/Development section of the documentation labels Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:contributing Issues and PRs related to the Contributing/Development section of the documentation enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pool Vector Dead Link on 4.0
6 participants