www.digitalmars.com         C & C++   DMDScript  

D - setting array length change

I propose that the function _d_arraysetlength in internal.gc.gc
change to *not* short-cut when growing the array from 0 length.
(meaning remove the "if (p.length)" block)

This means we could reserve space for arrays using slicing:

 int[] x;
 x.length = 10000; // some big reserve
 x = x[0..0]; // not x.length = 0;

and then set the length at a later time.

If the current implementation of _d_arraysetlength is unchanged 
then when the length finally is set since the array had 0 
length a new pointer is allocated no matter what the old 
pointer had. I'd like to check the old pointer before allocating.

I also think a helper function to reserve space would be 
helpful since this "set the length to reserve" trick is not 
simple or easy to remember. This helper would look something
like

template reserve(T) {
  void reserve(inout T[] x, size_t len) {
    origlen = x.length;
    x.length = len;
    x = x[0..origlen];
  }
}


-Ben
 
Mar 17 2004