From 9e1ea5d14fb1835eb1ad1e45c8e8f02c9072ca57 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Sat, 9 Apr 2022 11:22:03 +0200 Subject: Optimizing push_back() cvec/cdeq and some minor stuff. --- benchmarks/misc/sso_bench2.cpp | 8 ++++---- include/stc/cdeq.h | 10 ++++++---- include/stc/cstr.h | 2 +- include/stc/cvec.h | 25 +++++++++++++++---------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/benchmarks/misc/sso_bench2.cpp b/benchmarks/misc/sso_bench2.cpp index 7d3bf5ca..64ef6b12 100644 --- a/benchmarks/misc/sso_bench2.cpp +++ b/benchmarks/misc/sso_bench2.cpp @@ -2,7 +2,7 @@ #include #include #include -//#define STC_USE_SSO 1 +#define STC_USE_SSO 1 #define i_val_str #include @@ -18,7 +18,7 @@ static inline uint64_t romutrio(void) { s[0] = 15241094284759029579u * zp; s[1] = yp - xp; s[1] = ROTL_(s[1], 12); s[2] = zp - yp; s[2] = ROTL_(s[2], 44); - return xp; + return xp; } static void sromutrio(uint64_t seed) { @@ -31,7 +31,7 @@ static void sromutrio(uint64_t seed) { static const char CHARS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=+-"; -static const int BENCHMARK_SIZE = 10000000; +static const int BENCHMARK_SIZE = 5000000; static const int MAX_STRING_LENGTH = 20; using time_point = std::chrono::high_resolution_clock::time_point; @@ -63,7 +63,7 @@ void benchmark(L& vec, const int length, R addRandomString) { if (length == 0) for (int i = 0; i < BENCHMARK_SIZE; i++) addRandomString(vec, (i*13 & 31) + 1); - else + else for (int i = 0; i < BENCHMARK_SIZE; i++) addRandomString(vec, length); diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index e9df8898..c0b6b05c 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -298,10 +298,12 @@ _cx_memb(_reserve)(_cx_self* self, const size_t n) { STC_DEF _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value) { - struct cdeq_rep* rep = cdeq_rep_(self); - if (_cdeq_nfront(self) + rep->size == rep->cap) - _cx_memb(_expand_right_half_)(self, rep->size, 1); - _cx_value *v = self->data + cdeq_rep_(self)->size++; + struct cdeq_rep* r = cdeq_rep_(self); + if (_cdeq_nfront(self) + r->size == r->cap) { + _cx_memb(_expand_right_half_)(self, r->size, 1); + r = cdeq_rep_(self); + } + _cx_value *v = self->data + r->size++; *v = value; return v; } diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 189fc467..1e93a0ef 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -185,7 +185,7 @@ cstr_reserve(cstr* self, const size_t cap) { cstr_priv* p = _cstr_p(self); const size_t oldcap = p->cap; if (cap > oldcap) { - p = (cstr_priv*) c_realloc(oldcap ? p : NULL, _cstr_opt_mem(cap)); + p = (cstr_priv*) c_realloc((oldcap != 0) & (p != &_cstr_nullrep) ? p : NULL, _cstr_opt_mem(cap)); self->str = p->chr; if (oldcap == 0) self->str[p->size = 0] = '\0'; p->cap = _cstr_opt_cap(cap); diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 34e830ba..e3a08136 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -299,22 +299,27 @@ _cx_memb(_resize)(_cx_self* self, const size_t len, i_val null) { STC_DEF _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value) { - const size_t len = cvec_rep_(self)->size; - if (len == _cx_memb(_capacity)(*self)) - _cx_memb(_reserve)(self, (len*3 >> 1) + 4); - _cx_value *v = self->data + cvec_rep_(self)->size++; + struct cvec_rep *r = cvec_rep_(self); + if (r->size == r->cap) { + _cx_memb(_reserve)(self, (r->size*3 >> 1) + 4); + r = cvec_rep_(self); + } + _cx_value *v = self->data + r->size++; *v = value; return v; } static _cx_value* _cx_memb(_insert_space_)(_cx_self* self, _cx_value* pos, const size_t len) { - const size_t idx = pos - self->data, size = cvec_rep_(self)->size; - if (len == 0) return pos; - if (size + len > _cx_memb(_capacity)(*self)) - _cx_memb(_reserve)(self, (size*3 >> 1) + len), + const size_t idx = pos - self->data; + struct cvec_rep* r = cvec_rep_(self); + if (!len) return pos; + if (r->size + len > r->cap) { + _cx_memb(_reserve)(self, (r->size*3 >> 1) + len); + r = cvec_rep_(self); pos = self->data + idx; - cvec_rep_(self)->size += len; - memmove(pos + len, pos, (size - idx) * sizeof(i_val)); + } + memmove(pos + len, pos, (r->size - idx) * sizeof *pos); + r->size += len; return pos; } -- cgit v1.2.3