From 876090aeea80d55093b9f10044c02f2d9d61795e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 18 Nov 2017 17:32:58 +0100 Subject: [PATCH 1/3] Remove maximal reallocation size in wxVector This dramatically pessimizes performance for large vector sizes and doesn't actually save that much memory because all intermediate allocations are still being used by the process, so follow stdlibc++ example and just double the allocated buffer size without limit. --- include/wx/vector.h | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/include/wx/vector.h b/include/wx/vector.h index 686dc0a587..f07faddfca 100644 --- a/include/wx/vector.h +++ b/include/wx/vector.h @@ -315,10 +315,8 @@ public: // // NB: casts to size_type are needed to suppress warnings about // mixing enumeral and non-enumeral type in conditional expression - const size_type increment = m_size > 0 - ? m_size < ALLOC_MAX_SIZE - ? m_size - : (size_type)ALLOC_MAX_SIZE + const size_type increment = m_size > ALLOC_INITIAL_SIZE + ? m_size : (size_type)ALLOC_INITIAL_SIZE; if ( m_capacity + increment > n ) n = m_capacity + increment; @@ -491,7 +489,6 @@ public: private: static const size_type ALLOC_INITIAL_SIZE = 16; - static const size_type ALLOC_MAX_SIZE = 4096; void Copy(const wxVector& vb) { From d3132b114c094a2925aa0d0909c8acb67f285ebc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 19 Nov 2017 21:37:49 +0100 Subject: [PATCH 2/3] Optimize memory allocation in wxArrayString ctors Call assign() instead of Add() in a loop: this is not only shorter, but also ensures that reserve() is called before starting the loop and all the required memory is allocated at once. --- src/common/arrstr.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/common/arrstr.cpp b/src/common/arrstr.cpp index ccc25f23ab..6b3abce10c 100644 --- a/src/common/arrstr.cpp +++ b/src/common/arrstr.cpp @@ -36,8 +36,7 @@ wxArrayString::wxArrayString(size_t sz, const char** a) #if !wxUSE_STD_CONTAINERS Init(false); #endif - for (size_t i=0; i < sz; i++) - Add(a[i]); + assign(a, a + sz); } wxArrayString::wxArrayString(size_t sz, const wchar_t** a) @@ -45,8 +44,7 @@ wxArrayString::wxArrayString(size_t sz, const wchar_t** a) #if !wxUSE_STD_CONTAINERS Init(false); #endif - for (size_t i=0; i < sz; i++) - Add(a[i]); + assign(a, a + sz); } wxArrayString::wxArrayString(size_t sz, const wxString* a) @@ -54,8 +52,7 @@ wxArrayString::wxArrayString(size_t sz, const wxString* a) #if !wxUSE_STD_CONTAINERS Init(false); #endif - for (size_t i=0; i < sz; i++) - Add(a[i]); + assign(a, a + sz); } #if !wxUSE_STD_CONTAINERS From abce2a9428f62251b92aa67088c93181a7aedc9c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 19 Nov 2017 22:02:26 +0100 Subject: [PATCH 3/3] Use the same growth strategy in wxArrayString as in wxVector Remove maximal reallocation size in wxArrayString too, as it was done for wxVector a commit ago, and increase its size by 100% and not 50% when it needs to grow. There is no real reason to use different growth strategies in the two classes and wxVector one seems to be better. --- src/common/arrstr.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/common/arrstr.cpp b/src/common/arrstr.cpp index 6b3abce10c..cdb9590eaf 100644 --- a/src/common/arrstr.cpp +++ b/src/common/arrstr.cpp @@ -57,9 +57,6 @@ wxArrayString::wxArrayString(size_t sz, const wxString* a) #if !wxUSE_STD_CONTAINERS -// size increment = min(50% of current size, ARRAY_MAXSIZE_INCREMENT) -#define ARRAY_MAXSIZE_INCREMENT 4096 - #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h #define ARRAY_DEFAULT_INITIAL_SIZE (16) #endif @@ -139,11 +136,8 @@ wxString *wxArrayString::Grow(size_t nIncrement) else { // otherwise when it's called for the first time, nIncrement would be 0 // and the array would never be expanded - // add 50% but not too much size_t ndefIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE - ? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; - if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT ) - ndefIncrement = ARRAY_MAXSIZE_INCREMENT; + ? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize; if ( nIncrement < ndefIncrement ) nIncrement = ndefIncrement; m_nSize += nIncrement;