Change representation of dynamic arrays?

David Brown dlang at davidb.org
Sat Oct 20 09:40:23 PDT 2007


On Sat, Oct 20, 2007 at 04:44:06PM +0100, Janice Caron wrote:

>The test (array.length == 0) will still work, but since calculating
>.length might involve a divide, it's no longer guaranteed to be
>optimal. (Also, I can imagine collection in which .length might be
>O(N)).

The compiler should never generate an actual division for a compile-time
constant.  For any constant division, there is an equivalent multiplication
that will give the same result.  Most modern architectures do multiply
quickly.

It is more work than strictly necessary to determine if the length is zero.
However, (array.length == 0) is a real easy thing for an optimizer to see.
It certainly doesn't justify adding a special construct to the language for
it.  It would be easier to just fix the optimizer if it misses it.

David



More information about the Digitalmars-d mailing list