minor optimizations
This commit is contained in:
parent
2b12b5c970
commit
0fbc29e482
@ -5,6 +5,17 @@
|
|||||||
#include<cstdlib>
|
#include<cstdlib>
|
||||||
#include<cassert>
|
#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 {
|
class TurboList {
|
||||||
int *old;
|
int *old;
|
||||||
int *nex;
|
int *nex;
|
||||||
@ -14,6 +25,19 @@ class TurboList {
|
|||||||
|
|
||||||
uint32_t capacity;
|
uint32_t capacity;
|
||||||
uint32_t count;
|
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:
|
public:
|
||||||
inline TurboList(uint32_t initial_cap = 16) noexcept :
|
inline TurboList(uint32_t initial_cap = 16) noexcept :
|
||||||
old(nullptr),
|
old(nullptr),
|
||||||
@ -35,7 +59,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline void insert(int elem) noexcept {
|
inline void insert(int elem) noexcept {
|
||||||
if(count < capacity) {
|
if(TL_LIKELY(count < capacity)) {
|
||||||
// INSERT
|
// INSERT
|
||||||
if(mid > 0) {
|
if(mid > 0) {
|
||||||
nex[mid - 1] = old[mid - 1];
|
nex[mid - 1] = old[mid - 1];
|
||||||
@ -45,15 +69,7 @@ public:
|
|||||||
++count;
|
++count;
|
||||||
} else {
|
} else {
|
||||||
// GROW
|
// GROW
|
||||||
assert(mid == 0);
|
grow_and_insert(elem);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
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();
|
auto before_TurboList = ms_now();
|
||||||
for(int i = 0; i < N; ++i) {
|
for(int i = 0; i < N; ++i) {
|
||||||
@ -38,13 +46,5 @@ int main() {
|
|||||||
}
|
}
|
||||||
#endif /* PRINT_DBG */
|
#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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user