Skip to content

Commit 304347c

Browse files
committed
Merge pull request #104055 from Ivorforce/fixed-vector
Core: Add `FixedVector` template - a collection that can be used completely on the stack.
2 parents baf9d6e + 1b1ab76 commit 304347c

File tree

3 files changed

+271
-0
lines changed

3 files changed

+271
-0
lines changed

core/templates/fixed_vector.h

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**************************************************************************/
2+
/* fixed_vector.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#pragma once
32+
33+
/**
34+
* A high performance Vector of fixed capacity.
35+
* Especially useful if you need to create an array on the stack, to
36+
* prevent dynamic allocations (especially in bottleneck code).
37+
*
38+
* Choose CAPACITY such that it is enough for all elements that could be added through all branches.
39+
*
40+
*/
41+
template <class T, uint32_t CAPACITY>
42+
class FixedVector {
43+
// This declaration allows us to access other FixedVector's private members.
44+
template <class T_, uint32_t CAPACITY_>
45+
friend class FixedVector;
46+
47+
uint32_t _size = 0;
48+
alignas(T) uint8_t _data[CAPACITY * sizeof(T)];
49+
50+
constexpr static uint32_t DATA_PADDING = MAX(alignof(T), alignof(uint32_t)) - alignof(uint32_t);
51+
52+
public:
53+
_FORCE_INLINE_ constexpr FixedVector() = default;
54+
constexpr FixedVector(std::initializer_list<T> p_init) {
55+
ERR_FAIL_COND(p_init.size() > CAPACITY);
56+
for (const T &element : p_init) {
57+
memnew_placement(ptr() + _size++, T(element));
58+
}
59+
}
60+
61+
template <uint32_t p_capacity>
62+
constexpr FixedVector(const FixedVector<T, p_capacity> &p_from) {
63+
ERR_FAIL_COND(p_from.size() > CAPACITY);
64+
if constexpr (std::is_trivially_copyable_v<T>) {
65+
// Copy size and all provided elements at once.
66+
memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T));
67+
} else {
68+
for (const T &element : p_from) {
69+
memnew_placement(ptr() + _size++, T(element));
70+
}
71+
}
72+
}
73+
74+
template <uint32_t p_capacity>
75+
constexpr FixedVector(FixedVector<T, p_capacity> &&p_from) {
76+
ERR_FAIL_COND(p_from.size() > CAPACITY);
77+
// Copy size and all provided elements at once.
78+
// Note: Assumes trivial relocatability.
79+
memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T));
80+
p_from._size = 0;
81+
}
82+
83+
~FixedVector() {
84+
if constexpr (!std::is_trivially_destructible_v<T>) {
85+
for (uint32_t i = 0; i < _size; i++) {
86+
ptr()[i].~T();
87+
}
88+
}
89+
}
90+
91+
_FORCE_INLINE_ constexpr T *ptr() { return (T *)(_data); }
92+
_FORCE_INLINE_ constexpr const T *ptr() const { return (const T *)(_data); }
93+
94+
_FORCE_INLINE_ constexpr operator Span<T>() const { return Span<T>(ptr(), size()); }
95+
_FORCE_INLINE_ constexpr Span<T> span() const { return operator Span<T>(); }
96+
97+
_FORCE_INLINE_ constexpr uint32_t size() const { return _size; }
98+
_FORCE_INLINE_ constexpr bool is_empty() const { return !_size; }
99+
_FORCE_INLINE_ constexpr bool is_full() const { return _size == CAPACITY; }
100+
_FORCE_INLINE_ constexpr uint32_t capacity() const { return CAPACITY; }
101+
102+
_FORCE_INLINE_ constexpr void clear() { resize_initialized(0); }
103+
104+
/// Changes the size of the vector.
105+
/// If p_size > size(), constructs new elements.
106+
/// If p_size < size(), destructs new elements.
107+
constexpr Error resize_initialized(uint32_t p_size) {
108+
if (p_size > _size) {
109+
ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY);
110+
memnew_arr_placement<true>(ptr() + _size, p_size - _size);
111+
} else if (p_size < _size) {
112+
if constexpr (!std::is_trivially_destructible_v<T>) {
113+
for (uint32_t i = p_size; i < _size; i++) {
114+
ptr()[i].~T();
115+
}
116+
}
117+
}
118+
119+
_size = p_size;
120+
return OK;
121+
}
122+
123+
/// Changes the size of the vector.
124+
/// The initializer of new elements is skipped, making this function faster than resize_initialized.
125+
/// The caller is required to initialize the new values.
126+
constexpr Error resize_uninitialized(uint32_t p_size) {
127+
static_assert(std::is_trivially_destructible_v<T>, "resize_uninitialized is unsafe to call if T is not trivially destructible.");
128+
ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY);
129+
_size = p_size;
130+
return OK;
131+
}
132+
133+
constexpr void push_back(const T &p_val) {
134+
ERR_FAIL_COND(_size >= CAPACITY);
135+
memnew_placement(ptr() + _size, T(p_val));
136+
_size++;
137+
}
138+
139+
constexpr void pop_back() {
140+
ERR_FAIL_COND(_size == 0);
141+
_size--;
142+
ptr()[_size].~T();
143+
}
144+
145+
// NOTE: Subscripts sanity check the bounds to avoid undefined behavior.
146+
// This is slower than direct buffer access and can prevent autovectorization.
147+
// If the bounds are known, use ptr() subscript instead.
148+
constexpr const T &operator[](uint32_t p_index) const {
149+
CRASH_COND(p_index >= _size);
150+
return ptr()[p_index];
151+
}
152+
153+
constexpr T &operator[](uint32_t p_index) {
154+
CRASH_COND(p_index >= _size);
155+
return ptr()[p_index];
156+
}
157+
158+
_FORCE_INLINE_ constexpr T *begin() { return ptr(); }
159+
_FORCE_INLINE_ constexpr T *end() { return ptr() + _size; }
160+
161+
_FORCE_INLINE_ constexpr const T *begin() const { return ptr(); }
162+
_FORCE_INLINE_ constexpr const T *end() const { return ptr() + _size; }
163+
};
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**************************************************************************/
2+
/* test_fixed_vector.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#pragma once
32+
33+
#include "core/templates/fixed_vector.h"
34+
35+
#include "tests/test_macros.h"
36+
37+
namespace TestFixedVector {
38+
39+
TEST_CASE("[FixedVector] Basic Checks") {
40+
FixedVector<uint16_t, 1> vector;
41+
CHECK_EQ(vector.capacity(), 1);
42+
43+
CHECK_EQ(vector.size(), 0);
44+
CHECK(vector.is_empty());
45+
CHECK(!vector.is_full());
46+
47+
vector.push_back(5);
48+
CHECK_EQ(vector.size(), 1);
49+
CHECK_EQ(vector[0], 5);
50+
CHECK_EQ(vector.ptr()[0], 5);
51+
CHECK(!vector.is_empty());
52+
CHECK(vector.is_full());
53+
54+
vector.pop_back();
55+
CHECK_EQ(vector.size(), 0);
56+
CHECK(vector.is_empty());
57+
CHECK(!vector.is_full());
58+
59+
FixedVector<uint16_t, 2> vector1 = { 1, 2 };
60+
CHECK_EQ(vector1.capacity(), 2);
61+
CHECK_EQ(vector1.size(), 2);
62+
CHECK_EQ(vector1[0], 1);
63+
CHECK_EQ(vector1[1], 2);
64+
65+
FixedVector<uint16_t, 3> vector2(vector1);
66+
CHECK_EQ(vector2.capacity(), 3);
67+
CHECK_EQ(vector2.size(), 2);
68+
CHECK_EQ(vector2[0], 1);
69+
CHECK_EQ(vector2[1], 2);
70+
71+
FixedVector<Variant, 3> vector_variant;
72+
CHECK_EQ(vector_variant.size(), 0);
73+
CHECK_EQ(vector_variant.capacity(), 3);
74+
vector_variant.resize_initialized(3);
75+
vector_variant[0] = "Test";
76+
vector_variant[1] = 1;
77+
CHECK_EQ(vector_variant.capacity(), 3);
78+
CHECK_EQ(vector_variant.size(), 3);
79+
CHECK_EQ(vector_variant[0], "Test");
80+
CHECK_EQ(vector_variant[1], Variant(1));
81+
CHECK_EQ(vector_variant[2].get_type(), Variant::NIL);
82+
}
83+
84+
TEST_CASE("[FixedVector] Alignment Checks") {
85+
FixedVector<uint16_t, 4> vector_uint16;
86+
vector_uint16.resize_uninitialized(4);
87+
CHECK((size_t)&vector_uint16[0] % alignof(uint16_t) == 0);
88+
CHECK((size_t)&vector_uint16[1] % alignof(uint16_t) == 0);
89+
CHECK((size_t)&vector_uint16[2] % alignof(uint16_t) == 0);
90+
CHECK((size_t)&vector_uint16[3] % alignof(uint16_t) == 0);
91+
92+
FixedVector<uint32_t, 4> vector_uint32;
93+
vector_uint32.resize_uninitialized(4);
94+
CHECK((size_t)&vector_uint32[0] % alignof(uint32_t) == 0);
95+
CHECK((size_t)&vector_uint32[1] % alignof(uint32_t) == 0);
96+
CHECK((size_t)&vector_uint32[2] % alignof(uint32_t) == 0);
97+
CHECK((size_t)&vector_uint32[3] % alignof(uint32_t) == 0);
98+
99+
FixedVector<uint64_t, 4> vector_uint64;
100+
vector_uint64.resize_uninitialized(4);
101+
CHECK((size_t)&vector_uint64[0] % alignof(uint64_t) == 0);
102+
CHECK((size_t)&vector_uint64[1] % alignof(uint64_t) == 0);
103+
CHECK((size_t)&vector_uint64[2] % alignof(uint64_t) == 0);
104+
CHECK((size_t)&vector_uint64[3] % alignof(uint64_t) == 0);
105+
}
106+
107+
} //namespace TestFixedVector

tests/test_main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
#include "tests/core/string/test_translation_server.h"
9696
#include "tests/core/templates/test_a_hash_map.h"
9797
#include "tests/core/templates/test_command_queue.h"
98+
#include "tests/core/templates/test_fixed_vector.h"
9899
#include "tests/core/templates/test_hash_map.h"
99100
#include "tests/core/templates/test_hash_set.h"
100101
#include "tests/core/templates/test_list.h"

0 commit comments

Comments
 (0)