From 44e647e72b5e019e245fbaa66e40c68bdde6c939 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Tue, 10 Mar 2015 16:53:47 +0000 Subject: [PATCH] add reallocate() function and use it in bit_vector and vector containers give a speedup of 1-4% Signed-off-by: Nuno Lopes --- src/util/bit_vector.cpp | 10 ++++----- src/util/bit_vector.h | 4 +--- src/util/memory_manager.cpp | 42 +++++++++++++++++++++++++++++++++++++ src/util/memory_manager.h | 1 + src/util/vector.h | 12 +++-------- 5 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/util/bit_vector.cpp b/src/util/bit_vector.cpp index 1490b9628..c41e4574c 100644 --- a/src/util/bit_vector.cpp +++ b/src/util/bit_vector.cpp @@ -25,13 +25,11 @@ Revision History: #define MK_MASK(_num_bits_) ((1U << _num_bits_) - 1) void bit_vector::expand_to(unsigned new_capacity) { - unsigned * new_data = alloc_svect(unsigned, new_capacity); - memset(new_data, 0, new_capacity * sizeof(unsigned)); - if (m_capacity > 0) { - memcpy(new_data, m_data, m_capacity * sizeof(unsigned)); - dealloc_svect(m_data); + if (m_data) { + m_data = (unsigned*)memory::reallocate(m_data, new_capacity * sizeof(unsigned)); + } else { + m_data = alloc_svect(unsigned, new_capacity); } - m_data = new_data; m_capacity = new_capacity; } diff --git a/src/util/bit_vector.h b/src/util/bit_vector.h index 0ccdeae9e..6738ba0aa 100644 --- a/src/util/bit_vector.h +++ b/src/util/bit_vector.h @@ -87,9 +87,7 @@ public: } ~bit_vector() { - if (m_data) { - dealloc_svect(m_data); - } + dealloc_svect(m_data); } void reset() { diff --git a/src/util/memory_manager.cpp b/src/util/memory_manager.cpp index 354a11aeb..e911ff505 100644 --- a/src/util/memory_manager.cpp +++ b/src/util/memory_manager.cpp @@ -252,6 +252,24 @@ void * memory::allocate(size_t s) { return static_cast(r) + 1; // we return a pointer to the location after the extra field } +void* memory::reallocate(void *p, size_t s) { + size_t *sz_p = reinterpret_cast(p)-1; + size_t sz = *sz_p; + void *real_p = reinterpret_cast(sz_p); + s = s + sizeof(size_t); // we allocate an extra field! + + g_memory_thread_alloc_size += s - sz; + if (g_memory_thread_alloc_size > SYNCH_THRESHOLD) { + synchronize_counters(true); + } + + void *r = realloc(real_p, s); + if (r == 0) + throw_out_of_memory(); + *(static_cast(r)) = s; + return static_cast(r) + 1; // we return a pointer to the location after the extra field +} + #else // ================================== // ================================== @@ -292,5 +310,29 @@ void * memory::allocate(size_t s) { *(static_cast(r)) = s; return static_cast(r) + 1; // we return a pointer to the location after the extra field } + +void* memory::reallocate(void *p, size_t s) { + size_t * sz_p = reinterpret_cast(p) - 1; + size_t sz = *sz_p; + void * real_p = reinterpret_cast(sz_p); + s = s + sizeof(size_t); // we allocate an extra field! + bool out_of_mem = false; + #pragma omp critical (z3_memory_manager) + { + g_memory_alloc_size += s - sz; + if (g_memory_alloc_size > g_memory_max_used_size) + g_memory_max_used_size = g_memory_alloc_size; + if (g_memory_max_size != 0 && g_memory_alloc_size > g_memory_max_size) + out_of_mem = true; + } + if (out_of_mem) + throw_out_of_memory(); + + void *r = realloc(real_p, s); + if (r == 0) + throw_out_of_memory(); + *(static_cast(r)) = s; + return static_cast(r) + 1; // we return a pointer to the location after the extra field +} #endif diff --git a/src/util/memory_manager.h b/src/util/memory_manager.h index 49472ef28..bd912828e 100644 --- a/src/util/memory_manager.h +++ b/src/util/memory_manager.h @@ -58,6 +58,7 @@ public: static void display_i_max_usage(std::ostream& os); static void deallocate(void* p); static ALLOC_ATTR void* allocate(size_t s); + static ALLOC_ATTR void* reallocate(void *p, size_t s); #if _DEBUG static void deallocate(char const* file, int line, void* p); static ALLOC_ATTR void* allocate(char const* file, int line, char const* obj, size_t s); diff --git a/src/util/vector.h b/src/util/vector.h index 9370a4eed..dc08a63b3 100644 --- a/src/util/vector.h +++ b/src/util/vector.h @@ -71,18 +71,12 @@ class vector { SZ old_capacity_T = sizeof(T) * old_capacity + sizeof(SZ) * 2; SZ new_capacity = (3 * old_capacity + 1) >> 1; SZ new_capacity_T = sizeof(T) * new_capacity + sizeof(SZ) * 2; - SZ size = reinterpret_cast(m_data)[SIZE_IDX]; if (new_capacity <= old_capacity || new_capacity_T <= old_capacity_T) { throw default_exception("Overflow encountered when expanding vector"); } - SZ * mem = reinterpret_cast(memory::allocate(new_capacity_T)); - *mem = new_capacity; - mem ++; - *mem = size; - mem++; - memcpy(mem, m_data, size * sizeof(T)); - free_memory(); - m_data = reinterpret_cast(mem); + SZ *mem = (SZ*)memory::reallocate(reinterpret_cast(m_data)-2, new_capacity_T); + *mem = new_capacity; + m_data = reinterpret_cast(mem + 2); } }