Re: suggestion: slight optimization for UT_String


Subject: Re: suggestion: slight optimization for UT_String
From: Joaquín Cuenca Abela (cuenca@celium.net)
Date: Mon Mar 19 2001 - 04:10:16 CST


Mike Nordell wrote:
>
> Joaquín Cuenca Abela wrote:
> > 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).
>
> Has tests shown that we do that much appending? If so, I'm all for it
> (conceptually), but not implementation-wise.
> 1. The "n" in g_nGrowFactor says "I'm an integral type" why you can't stuff
> 1.5 into it.

ops, good catch

> 2. Floating point operations should in this case (IMHO) be avoided (simple
> to fix, but since you provided an alternative implementation :-) ).

why? I choose 1.5 because I read that Andrew Koenig proved that usually
the best strategie is to grow exponentially with a factor of 1.5 (sorry,
I have no links to the article). I don't think that the overhead of the
floating point conversion will be signifiant (but I have no figures, and
the experience shows that programmers are bad profilers :)...

So I vote for just s/g_nGrowFactor/g_rGrowFactor/g;

> 3. You might consider not using "max" for at least two reasons. C++ "max"
> uses templates which are forbidden in AW, and M$ implementation doesn't even
> give you access to it using its standard name. Either MyMax from ut_types.h
> (though that would include loads of unneccessary stuff that at least I'd
> dislike) or simply create your own local MyMax macro in ut_stringbuf.cpp.

I didn't write std::max, but just max (I was not planning to use STL
max)
In my tests I added a simple

#ifndef max
#define max(a, b) ((a) > (b) ? (a) : (b))
#endif

at the top of the file. I did not include it in my email because I just
posted the relevant snipset of code.

> While at it, could you remove the "replace" member function declaration
> (it's never defined) and remove the redundant "if (m_psz)" around "delete[]
> m_psz" in the grow_xxx functions?

I thought that we were still using the if (x) delete[] x; in order to
support legacy compilers (?). If it's no more true, we should replace
the DELETEP, DELETEPV and REPLACEP decls...
I will remove the redundant if (x) delete[] x; from the clear method,
too, but I'm a bit reluctant to remove the test from DELETEP & al. (can
everybody please check that his compiler does the right thing here?).

Cheers,

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



This archive was generated by hypermail 2b25 : Mon Mar 19 2001 - 04:10:42 CST