D array expansion and non-deterministic re-allocation

Steven Schveighoffer schveiguy at yahoo.com
Wed Nov 25 06:41:28 PST 2009


Simen kjaeraas Wrote:

> Steven Schveighoffer <schveiguy at yahoo.com> wrote:
> 
> >> You are again resorting to implementation. I guess in the current  
> >> implementation it's true that the compiler will indeed insert a call to  
> >> its internal function. But the language spec does not proscribe that.
> >>
> >
> > Sure it does.  It says that setting the length may reallocate the array  
> > to hold enough space.  How do you do that without a function call?
> 
> As long as the compiler is aware of arrays' existence, and knows how
> those functions work, there might indeed be an optimization that turns
> length+=1;length-=1; into a nop. There isn't at the moment, and there
> might never be, but it's a conceivable optimization.

As long as the spec says changing length may expand the array to hold enough space, the optimizer can't, because the optimization would change the side effects of the function.  An optimizer should not change the outcome or side effects of a function.  It's not unheard of for an optimizer to eliminate important operations that it thinks are invalid, but in that case, it's a broken optimizer, I don't see why we would add this case.

Now, I could see a future optimizer changing

a.length += 2;
a.length += 3;

into

a.length += 5;

But I don't see a huge gain.  Why not just write a.length += 5 in the first place?

This point brought up is a non-issue, an optimizer that changes the outcome/side effects of a function is a broken optimizer, end of story.

-Steve



More information about the Digitalmars-d mailing list