[omniORB] Sequence length operation

jnye@micro-optics.com jnye@micro-optics.com
Tue, 12 Jun 2001 10:20:29 -0300


Is it just me, or is the length operation incredibly inefficient when it
comes to reallocation in the _CORBA_Pseudo_Unbounded_Sequence class? Here is
the code:

inline void length(_CORBA_ULong length)
{
    if (length > pd_max)
    {
***        ElemT* newbuf = new ElemT[length]; ***
        if( !newbuf )  _CORBA_new_operator_return_null();
        for( unsigned long i = 0; i < pd_len; i++ )
	newbuf[i] = pd_buf[i];
***        pd_max = length; ***
        if( pd_rel && pd_buf )  delete[] pd_buf;
        else                    pd_rel = 1;
        pd_buf = newbuf;
    }
    pd_len = length;
}

See the lines with *** surrounding them. Should this class not follow what
most STL vectors do and double their capacity each time the requested length
exceeds the maximum? Here is an example use:

IDL:
typedef sequence<string> StringSeq;


C++:
StringSeq_var seq = new StringSeq(100);
std::string line;
std::ifstream ifs(....);
size_t count = 0;
while (std::getline(ifs, line))
{
    seq->length(seq->length() + 1);
    seq[count] = line.c_str(); // c_str returns const so copy is made.
    count++
}

In the above example, if the number of lines exceeds 100, a reallocation an
copy of the entire sequence's contents will occur on each iteration of the
loop. Pretty expensive and unnecessary.

Is there anyone looking into optimizing the implementations of the base omni
classes?

Here is an implementation I propose:
inline void length(_CORBA_ULong length)
{
    if (length > pd_max)
    {
***        _CORBA_ULong pd_max_tmp = pd_max * 2; ***
***        ElemT* newbuf = new ElemT[pd_max_tmp]; ***
        if( !newbuf )  _CORBA_new_operator_return_null();
        for( unsigned long i = 0; i < pd_len; i++ )
	newbuf[i] = pd_buf[i];
***        pd_max = pd_max_tmp; ***
        if( pd_rel && pd_buf )  delete[] pd_buf;
        else                    pd_rel = 1;
        pd_buf = newbuf;
    }
    pd_len = length;
}

Just a suggestion...

Cheers,
Jason.