Arrays, overlapping, etc

Regan Heath regan at netmail.co.nz
Tue Jul 10 01:05:58 PDT 2007


Vladimir Panteleev wrote:
> On Mon, 09 Jul 2007 20:15:19 +0300, Regan Heath <regan at netmail.co.nz> wrote:
> 
>> Vladimir Panteleev wrote:
>>> The main reason for overlapping copy being forbidden is because
>>> forbidding it makes normal array operations much faster - the
>>> compiler doesn't have to check for overlaps each time an array
>>> (slice) is copied.
>> Isn't it already checking?  I mean, it has to be in order to give the
>> exception "overlapping array copy", right?
> 
> It only does that in debug versions. If you compile a release build, stuff will just break.

Ahh, of course.  Thanks for clarification.

>>> I made a suggestion some time ago to make the
>>> compiler allow overlapping copy when it's obvious that you're doing
>>> it (copying a slice of one array onto itself). No one has yet
>>> discussed implementing it as a separate language feature, though.
>> I dont want a seperate language feature.  I just want it to work.
> 
> Don't think it's possible to do without the abovementioned performance penalty with all array slice copies.

Yes, I see what you mean now.

How much of a penalty is it really.  The code reads:

     else if (cast(byte *)to + to.length * size <= cast(byte *)from ||
	cast(byte *)from + from.length * size <= cast(byte *)to)
     {
	memcpy(cast(byte *)to, cast(byte *)from, to.length * size);
     }
     else
     {
	throw new Error("overlapping array copy");
     }

Assuming the debug code is in phobos\internal\arraycat.d.  (I didn't 
release this was debug only code... where is the release version of the 
code?)

Regan



More information about the Digitalmars-d mailing list