forked from graphitemaster/vector_benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.cpp
174 lines (164 loc) · 5.56 KB
/
array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stddef.h> // size_t
#include <stdlib.h> // realloc, free
#include <stdio.h> // printf
#include <string.h> // memset
#include <time.h> // clock_t, clock
#include <utility> // std::move, std::exchange, std::forward
#include <type_traits> // std::is_trivially_{copyable,constructible,destructible}_v
#include <vector> // std::vector
#if defined(_MSC_VER)
#define force_inline __force_inline
#else
#define force_inline __attribute__((always_inline)) inline
#endif
// custom placement-new so it can be forced inlined
struct placement_new {};
force_inline constexpr void* operator new(size_t, void* ptr, placement_new) noexcept { return ptr; }
template<typename T>
struct Array {
using value_type = T;
constexpr Array() = default;
~Array() { clear(); free(m_data); }
Array(Array&& array)
: m_data{std::exchange(array.m_data, nullptr)}
, m_size{std::exchange(array.m_size, 0)}
, m_capacity{std::exchange(array.m_capacity, 0)}
{}
template<typename... Ts>
force_inline bool emplace_back(Ts&&... args) noexcept {
if (!ensure(size() + 1)) return false;
new (m_data + m_size, placement_new{}) T{std::forward<Ts>(args)...};
m_size++;
return true;
}
force_inline bool push_back(T&& value) noexcept {
if (!ensure(size() + 1)) return false;
new (m_data + m_size, placement_new{}) T{std::move(value)};
m_size++;
return true;
}
force_inline bool push_back(const T& value) noexcept {
if (!ensure(size() + 1)) return false;
new (m_data + m_size, placement_new{}) T{value};
m_size++;
return true;
}
force_inline void clear() noexcept {
// Rely on unsigned underflow to walk in reverse order for calling destructors
if constexpr (!std::is_trivially_destructible_v<T>) {
if (m_size) for (size_t i = m_size - 1; i < m_size; i--) m_data[i].~T();
}
m_size = 0;
}
bool resize(size_t size) noexcept {
if (size <= m_size) {
if constexpr(!std::is_trivially_destructible_v<T>) {
if (m_size) for (size_t i = m_size - 1; i > size; i--) m_data[i].~T();
}
} else {
if (!ensure(size)) return false;
if constexpr (std::is_trivially_constructible_v<T>) {
memset(m_data + m_size, 0, (size - m_size) * sizeof(T));
} else {
for (size_t i = m_size; i < size; i++) new (m_data + i, placement_new{}) T;
}
}
m_size = size;
return true;
}
force_inline T* begin() noexcept { return m_data; }
force_inline const T* begin() const noexcept { return m_data; }
force_inline T* end() noexcept { return m_data + m_size; }
force_inline const T* end() const noexcept { return m_data + m_size; }
force_inline const T& operator[](size_t index) const noexcept { return m_data[index]; }
force_inline T& operator[](size_t index) noexcept { return m_data[index]; }
force_inline bool empty() const noexcept { return m_size == 0; }
force_inline size_t size() const noexcept { return m_size; }
force_inline size_t capacity() const noexcept { return m_capacity; }
force_inline const T* data() const noexcept { return m_data; }
force_inline T* data() noexcept{ return m_data; }
private:
bool ensure(size_t size) noexcept {
if (size <= m_capacity) return true;
size_t new_capacity = m_capacity;
while (new_capacity < size) new_capacity = ((new_capacity + 1) * 3) / 2;
if (sizeof(T) > (size_t)-1/new_capacity) return false; // sizeof(T) * new_capacity overflow.
void *data = nullptr;
if constexpr (std::is_trivially_copyable_v<T>) {
data = realloc(m_data, sizeof(T) * new_capacity);
} else {
data = malloc(sizeof(T) * new_capacity);
}
if (!data) return false; // out of memory
if constexpr (!std::is_trivially_copyable_v<T>) {
if (m_size) {
auto store = reinterpret_cast<T*>(data);
for (auto item = begin(), last = end(); item != last; ++item) {
*store++ = std::move(*item);
item->~T();
}
}
free(m_data);
}
m_data = reinterpret_cast<T*>(data);
m_capacity = new_capacity;
return true;
}
T* m_data = nullptr;
size_t m_size = 0;
size_t m_capacity = 0;
};
// Simple benchmark timer.
struct Timer {
clock_t t0, t1;
void start() noexcept { t0 = clock(); }
void stop() noexcept { t1 = clock(); }
double elapsed() const noexcept {
return (double)(t1 - t0) / CLOCKS_PER_SEC;
}
};
// Some types to test the Array implementation with.
struct NonTrivial {
NonTrivial() {}
NonTrivial(NonTrivial&&) {}
~NonTrivial() {}
NonTrivial(const NonTrivial&) {}
NonTrivial& operator=(NonTrivial&&) { return *this; }
NonTrivial& operator=(const NonTrivial&) { return *this; }
char buffer[128] = {0};
};
struct Trivial { float x = 0.0f, y = 1.0f, z = 2.0f; };
using POD = size_t;
static constexpr const auto ITERATIONS = 5'000'000;
template<typename T>
T test() noexcept {
srand(0xdeadbeef); // constant seed to be fair.
T array;
Timer timer;
timer.start();
typename T::value_type to_copy{};
for (size_t i = 0; i < ITERATIONS; i++) {
array.push_back(to_copy);
typename T::value_type to_move{};
array.push_back(std::move(to_move));
array.emplace_back(); // to_forward
}
timer.stop();
printf("%f\t", timer.elapsed());
return array;
}
#ifndef OPTION
#define OPTION ""
#endif
int main() {
printf("\"array %s\" ", OPTION);
auto x0 = test<Array<POD>>();
auto x1 = test<Array<Trivial>>();
auto x2 = test<Array<NonTrivial>>();
printf("\n");
printf("\"vector %s\" ", OPTION);
auto y0 = test<std::vector<POD>>();
auto y1 = test<std::vector<Trivial>>();
auto y2 = test<std::vector<NonTrivial>>();
printf("\n");
}