Merge branch 'arrays-alloc-optimise'

Reduce the number of memory allocations in wxArrayString and wxVector.
This commit is contained in:
Vadim Zeitlin
2017-11-19 23:13:43 +01:00
2 changed files with 6 additions and 18 deletions

View File

@@ -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
const size_type increment = m_size > ALLOC_INITIAL_SIZE
? m_size
: (size_type)ALLOC_MAX_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)
{

View File

@@ -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,15 +52,11 @@ 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
// 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
@@ -142,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;