Skip to content

Commit 2ebc97b

Browse files
committed
Add FixedVector template.
This is a high performance `Vector`-like object that can be used if the maximum number of objects is small and known, and the objects are needed only temporarily.
1 parent 7490787 commit 2ebc97b

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

core/templates/fixed_vector.h

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
// A high speed Vector of fixed capacity.
34+
// Especially useful if you need to create an array on the stack, to
35+
// prevent dynamic allocations (especially in bottleneck code).
36+
37+
// Choose CAPACITY such that it is enough for all elements that could be added through all branches.
38+
39+
// Set force_trivial to true to gain a slight performance improvement on resize calls
40+
// if the default value of the type can be ignored.
41+
template <class T, uint32_t CAPACITY, bool force_trivial = false>
42+
class FixedVector {
43+
constexpr static uint32_t UNIT_SIZE = sizeof(T);
44+
45+
static_assert(!force_trivial || std::is_trivially_destructible_v<T>, "force_trivial may only be true if the type is trivially destructible.");
46+
47+
uint32_t _size = 0;
48+
uint8_t _data[CAPACITY * UNIT_SIZE];
49+
50+
public:
51+
_FORCE_INLINE_ constexpr FixedVector() = default;
52+
constexpr FixedVector(std::initializer_list<T> p_init) {
53+
for (const T &element : p_init) {
54+
memnew_placement(ptr() + _size++, T(element));
55+
}
56+
}
57+
58+
template <uint32_t p_capacity, bool p_force_trivial = false>
59+
constexpr FixedVector(const FixedVector<T, p_capacity, p_force_trivial> &p_from) {
60+
for (const T &element : p_from) {
61+
memnew_placement(ptr() + _size++, T(element));
62+
}
63+
}
64+
65+
template <uint32_t p_capacity, bool p_force_trivial = false>
66+
constexpr FixedVector(FixedVector<T, p_capacity, p_force_trivial> &&p_from) {
67+
for (T &element : p_from) {
68+
memnew_placement(ptr() + _size++, T(std::move(element)));
69+
}
70+
}
71+
72+
_FORCE_INLINE_ constexpr T *ptr() { return (T *)(_data); }
73+
_FORCE_INLINE_ constexpr const T *ptr() const { return (const T *)(_data); }
74+
75+
_FORCE_INLINE_ constexpr operator Span<T>() const { return Span<T>(ptr(), size()); }
76+
_FORCE_INLINE_ constexpr Span<T> span() const { return operator Span<T>(); }
77+
78+
_FORCE_INLINE_ constexpr uint32_t size() const { return _size; }
79+
_FORCE_INLINE_ constexpr bool is_empty() const { return !_size; }
80+
_FORCE_INLINE_ constexpr bool is_full() const { return _size == CAPACITY; }
81+
_FORCE_INLINE_ constexpr uint32_t capacity() const { return CAPACITY; }
82+
83+
_FORCE_INLINE_ constexpr void clear() { resize(0); }
84+
85+
template <bool p_ensure_zero = false>
86+
constexpr Error resize(uint32_t p_size) {
87+
if (!force_trivial && p_size > _size) {
88+
ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY);
89+
memnew_arr_placement<p_ensure_zero>(ptr() + _size, p_size - _size);
90+
} else if (p_size < _size) {
91+
if constexpr (!std::is_trivially_destructible_v<T>) {
92+
for (uint32_t i = p_size; i < _size; i++) {
93+
ptr()[i].~T();
94+
}
95+
}
96+
}
97+
98+
_size = p_size;
99+
}
100+
101+
constexpr void push_back(const T &p_val) {
102+
ERR_FAIL_COND(_size >= CAPACITY);
103+
memnew_placement(ptr() + _size, T(p_val));
104+
_size++;
105+
}
106+
107+
constexpr void pop_back() {
108+
ERR_FAIL_COND(_size == 0);
109+
_size--;
110+
ptr()[_size].~T();
111+
}
112+
113+
// NOTE: Subscripts sanity check the bounds to avoid undefined behavior.
114+
// This is slower than direct buffer access and can prevent autovectorization.
115+
// If the bounds are known, use ptr() subscript instead.
116+
constexpr const T &operator[](uint32_t p_index) const {
117+
CRASH_COND(p_index >= _size);
118+
return ptr()[p_index];
119+
}
120+
121+
constexpr T &operator[](uint32_t p_index) {
122+
CRASH_COND(p_index >= _size);
123+
return ptr()[p_index];
124+
}
125+
126+
_FORCE_INLINE_ constexpr T *begin() { return ptr(); }
127+
_FORCE_INLINE_ constexpr T *end() { return ptr() + _size; }
128+
129+
_FORCE_INLINE_ constexpr const T *begin() const { return ptr(); }
130+
_FORCE_INLINE_ constexpr const T *end() const { return ptr() + _size; }
131+
};
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
72+
} //namespace TestFixedVector

tests/test_main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
#include "tests/core/string/test_translation_server.h"
9595
#include "tests/core/templates/test_a_hash_map.h"
9696
#include "tests/core/templates/test_command_queue.h"
97+
#include "tests/core/templates/test_fixed_vector.h"
9798
#include "tests/core/templates/test_hash_map.h"
9899
#include "tests/core/templates/test_hash_set.h"
99100
#include "tests/core/templates/test_list.h"

0 commit comments

Comments
 (0)