Merge branch 'arrays-alloc-optimise'
Reduce the number of memory allocations in wxArrayString and wxVector.
This commit is contained in:
@@ -315,10 +315,8 @@ public:
|
|||||||
//
|
//
|
||||||
// NB: casts to size_type are needed to suppress warnings about
|
// NB: casts to size_type are needed to suppress warnings about
|
||||||
// mixing enumeral and non-enumeral type in conditional expression
|
// mixing enumeral and non-enumeral type in conditional expression
|
||||||
const size_type increment = m_size > 0
|
const size_type increment = m_size > ALLOC_INITIAL_SIZE
|
||||||
? m_size < ALLOC_MAX_SIZE
|
? m_size
|
||||||
? m_size
|
|
||||||
: (size_type)ALLOC_MAX_SIZE
|
|
||||||
: (size_type)ALLOC_INITIAL_SIZE;
|
: (size_type)ALLOC_INITIAL_SIZE;
|
||||||
if ( m_capacity + increment > n )
|
if ( m_capacity + increment > n )
|
||||||
n = m_capacity + increment;
|
n = m_capacity + increment;
|
||||||
@@ -491,7 +489,6 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static const size_type ALLOC_INITIAL_SIZE = 16;
|
static const size_type ALLOC_INITIAL_SIZE = 16;
|
||||||
static const size_type ALLOC_MAX_SIZE = 4096;
|
|
||||||
|
|
||||||
void Copy(const wxVector& vb)
|
void Copy(const wxVector& vb)
|
||||||
{
|
{
|
||||||
|
@@ -36,8 +36,7 @@ wxArrayString::wxArrayString(size_t sz, const char** a)
|
|||||||
#if !wxUSE_STD_CONTAINERS
|
#if !wxUSE_STD_CONTAINERS
|
||||||
Init(false);
|
Init(false);
|
||||||
#endif
|
#endif
|
||||||
for (size_t i=0; i < sz; i++)
|
assign(a, a + sz);
|
||||||
Add(a[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxArrayString::wxArrayString(size_t sz, const wchar_t** a)
|
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
|
#if !wxUSE_STD_CONTAINERS
|
||||||
Init(false);
|
Init(false);
|
||||||
#endif
|
#endif
|
||||||
for (size_t i=0; i < sz; i++)
|
assign(a, a + sz);
|
||||||
Add(a[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxArrayString::wxArrayString(size_t sz, const wxString* a)
|
wxArrayString::wxArrayString(size_t sz, const wxString* a)
|
||||||
@@ -54,15 +52,11 @@ wxArrayString::wxArrayString(size_t sz, const wxString* a)
|
|||||||
#if !wxUSE_STD_CONTAINERS
|
#if !wxUSE_STD_CONTAINERS
|
||||||
Init(false);
|
Init(false);
|
||||||
#endif
|
#endif
|
||||||
for (size_t i=0; i < sz; i++)
|
assign(a, a + sz);
|
||||||
Add(a[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !wxUSE_STD_CONTAINERS
|
#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
|
#ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
|
||||||
#define ARRAY_DEFAULT_INITIAL_SIZE (16)
|
#define ARRAY_DEFAULT_INITIAL_SIZE (16)
|
||||||
#endif
|
#endif
|
||||||
@@ -142,11 +136,8 @@ wxString *wxArrayString::Grow(size_t nIncrement)
|
|||||||
else {
|
else {
|
||||||
// otherwise when it's called for the first time, nIncrement would be 0
|
// otherwise when it's called for the first time, nIncrement would be 0
|
||||||
// and the array would never be expanded
|
// and the array would never be expanded
|
||||||
// add 50% but not too much
|
|
||||||
size_t ndefIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE
|
size_t ndefIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE
|
||||||
? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1;
|
? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize;
|
||||||
if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT )
|
|
||||||
ndefIncrement = ARRAY_MAXSIZE_INCREMENT;
|
|
||||||
if ( nIncrement < ndefIncrement )
|
if ( nIncrement < ndefIncrement )
|
||||||
nIncrement = ndefIncrement;
|
nIncrement = ndefIncrement;
|
||||||
m_nSize += nIncrement;
|
m_nSize += nIncrement;
|
||||||
|
Reference in New Issue
Block a user