BZDEV: Possible inefficiency in array class...

From: Christopher Horvath (encino@ilm.com)
Date: Fri Dec 03 1999 - 20:11:59 EST


Howdy folks.

        In reading the documentation for the array class, I came across the
following statement in section 2.4.1: Indexing:

"This version of indexing is available for arrays of rank one through
eleven. If the array object isn't const, the return type of operator()
is a reference, if the array object is const, the return type is a
value."

I don't have the code in front of me, but the above sentence suggests
this structure:

template<class T>
T &operator()( int index );

template<class T>
T operator()( int index ) const;

If this is how the library is implemented, I would argue that for const
arrays, you could potentially be doing a lot of unnecessary data copying
for arrays who's data type is large. For example, suppose the data type
was something like this:

struct blah
{
    float val[50];
};

The code would resolve to this:

blah &operator()( int index );
blah operator()( int index ) const;

Consequently, whenever a const array is indexed, 50 floats will be
copied. An alternative would be this:

template <class T>
T &operator()( int index );

template <class T>
const T &operator()( int index ) const;

In this case, only the address gets copied when referencing a const or
non-const array, and the compiler will protect against destructive use
of the const T &. Is there a reason why this is not done?

Chris

--------------------- blitz-dev list --------------------------------
* To subscribe/unsubscribe: use the handy web form at
http://oonumerics.org/blitz/lists.html



This archive was generated by hypermail 2b29 : Wed Feb 20 2002 - 04:30:11 EST