minor optimizations
This commit is contained in:
parent
2b12b5c970
commit
0fbc29e482
@ -5,6 +5,17 @@
|
||||
#include<cstdlib>
|
||||
#include<cassert>
|
||||
|
||||
#ifndef TL_NOINLINE
|
||||
#define TL_NOINLINE __attribute__((noinline))
|
||||
#endif /* TL_NOINLINE */
|
||||
|
||||
#ifndef TL_LIKELY
|
||||
#define TL_LIKELY(x) __builtin_expect(!!(x), 1)
|
||||
#endif /* TL_LIKELY */
|
||||
#ifndef TL_UNLIKELY
|
||||
#define TL_UNLIKELY(x) __builtin_expect(!!(x), 0)
|
||||
#endif /* TL_UNLIKELY */
|
||||
|
||||
class TurboList {
|
||||
int *old;
|
||||
int *nex;
|
||||
@ -14,6 +25,19 @@ class TurboList {
|
||||
|
||||
uint32_t capacity;
|
||||
uint32_t count;
|
||||
|
||||
TL_NOINLINE void grow_and_insert(int elem) noexcept {
|
||||
// assert(mid == 0);
|
||||
if(old) free(old);
|
||||
old = nex;
|
||||
mid = end;
|
||||
capacity *= 2;
|
||||
nex = (int *) malloc(this->capacity * sizeof(int));
|
||||
|
||||
// Will go into the INSERT code path here
|
||||
insert(elem);
|
||||
}
|
||||
|
||||
public:
|
||||
inline TurboList(uint32_t initial_cap = 16) noexcept :
|
||||
old(nullptr),
|
||||
@ -35,7 +59,7 @@ public:
|
||||
}
|
||||
|
||||
inline void insert(int elem) noexcept {
|
||||
if(count < capacity) {
|
||||
if(TL_LIKELY(count < capacity)) {
|
||||
// INSERT
|
||||
if(mid > 0) {
|
||||
nex[mid - 1] = old[mid - 1];
|
||||
@ -45,15 +69,7 @@ public:
|
||||
++count;
|
||||
} else {
|
||||
// GROW
|
||||
assert(mid == 0);
|
||||
if(old) free(old);
|
||||
old = nex;
|
||||
mid = end;
|
||||
capacity *= 2;
|
||||
nex = (int *) malloc(this->capacity * sizeof(int));
|
||||
|
||||
// Will go into the INSERT code path here
|
||||
insert(elem);
|
||||
grow_and_insert(elem);
|
||||
}
|
||||
}
|
||||
|
||||
|
18
main.cpp
18
main.cpp
@ -23,7 +23,15 @@ static inline void printt(const char *prefix, size_t before, size_t after) noexc
|
||||
}
|
||||
|
||||
int main() {
|
||||
TurboList list(4);
|
||||
std::vector<int> vec;
|
||||
auto before_vector = ms_now();
|
||||
for(int i = 0; i < N; ++i) {
|
||||
vec.push_back(i);
|
||||
}
|
||||
auto after_vector = ms_now();
|
||||
printt("vector", before_vector, after_vector);
|
||||
|
||||
TurboList list;
|
||||
|
||||
auto before_TurboList = ms_now();
|
||||
for(int i = 0; i < N; ++i) {
|
||||
@ -38,13 +46,5 @@ int main() {
|
||||
}
|
||||
#endif /* PRINT_DBG */
|
||||
|
||||
std::vector<int> vec;
|
||||
auto before_vector = ms_now();
|
||||
for(int i = 0; i < N; ++i) {
|
||||
vec.push_back(i);
|
||||
}
|
||||
auto after_vector = ms_now();
|
||||
printt("vector", before_vector, after_vector);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user