diff --git a/TurboList.hpp b/TurboList.hpp index f221e6b..9975724 100644 --- a/TurboList.hpp +++ b/TurboList.hpp @@ -5,6 +5,17 @@ #include #include +#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); } } diff --git a/main.cpp b/main.cpp index 92d0aec..35bec68 100644 --- a/main.cpp +++ b/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 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 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; }