suggestion: slight optimization for UT_String


Subject: suggestion: slight optimization for UT_String
From: Joaquín Cuenca Abela (cuenca@celium.net)
Date: Sun Mar 18 2001 - 20:21:18 CST


What are your feelings about changing current growing algorithm of
UT_StringBuf from a:

n + 4

to

max(old * 1.5, n)
(ie, switch to a exponential growing algorithm).

Here are the Mike algos, and my suggested changes (3-4 lines, I think...
:)
suppose g_nGrowExtraBytes = 4 and g_nGrowFactor = 1.5.

#if 0 // Mike growing functions
void UT_Stringbuf::grow_nocopy(size_t n)
{
        ++n; // allow for zero termination
        if (n > capacity()) {
                const size_t nCurSize = size();
                if (m_psz) {
                        delete[] m_psz;
                }
                n += g_nGrowExtraBytes;
                m_psz = new char_type[n];
                m_pEnd = m_psz + nCurSize;
                m_size = n;
        }
}

void UT_Stringbuf::grow_copy(size_t n)
{
        ++n; // allow for zero termination
        if (n > capacity()) {
                n += g_nGrowExtraBytes;
                char_type* p = new char_type[n];
                const size_t nCurSize = size();
                if (m_psz) {
                        memcpy(p, m_psz, size() + 1);
                        delete[] m_psz;
                }
                m_psz = p;
                m_pEnd = m_psz + nCurSize;
                m_size = n;
        }
}

#else // JCA growing functions

void UT_Stringbuf::grow_nocopy(size_t n)
{
        ++n; // allow for zero termination
        if (n > capacity()) {
                const size_t nCurSize = size();
                if (m_psz) {
                        delete[] m_psz;
                }
                n = max(nCurSize * g_nGrowFactor, n);
                m_psz = new char_type[n];
                m_pEnd = m_psz + nCurSize;
                m_size = n;
        }
}

void UT_Stringbuf::grow_copy(size_t n)
{
        ++n; // allow for zero termination
        if (n > capacity()) {
                const size_t nCurSize = size();
                n = max(static_cast<size_t> (nCurSize * g_nGrowFactor), n);
                char_type* p = new char_type[n];
                if (m_psz) {
                        memcpy(p, m_psz, size() + 1);
                        delete[] m_psz;
                }
                m_psz = p;
                m_pEnd = m_psz + nCurSize;
                m_size = n;
        }
}
#endif

Comments?

Cheers,

--
Joaquín Cuenca Abela
cuenca@celium.net



This archive was generated by hypermail 2b25 : Sun Mar 18 2001 - 20:21:27 CST