April 05, 2006 Re: Array indexing and slicing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Georg Wrede | >> i just want to say, IMHO, all changes in index/slicing syntax will change compiler only. not generated binary.
>
>penalty. Second, if we are talking about dynamic arrays, then it does make a difference whether we allow indexing from the end of the array.
but "indexing from the end". if we express it in current syntax, then will no difference. we should compare equivalent code. at least codes with equivalent results of it execution.
|
April 05, 2006 Re: Array indexing and slicing | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | novice2 wrote:
>>> i just want to say, IMHO, all changes in index/slicing syntax will change
>>> compiler only. not generated binary.
>> Actually I don't think that can be true. That would only be the case if the
>> indexes were defined at compilation time. Assuming positive indexes only, the
>> following code...
>
> but programmer defines algo - sequence of some operations.
> we should compare equal code for currnet syntax and new syntax.
> equal code - with equal results.
>
> just imaginary example:
>
> task: get 3rd item from end in array;
> new synatx: b = a[-3];
> old syntax: b = a[length-3];
>
> only now we can compare, wich actions will be performed in runtime,
> and guess, will be it different or not.
"old" syntax is actually
b = a[$-3];
But that is not a good example, because the index is a constant. A better example would be:
int idx=someFunc();
b=a[idx];
It currently compiles to something like
int idx=someFunc();
debug {
if (idx<0 || idx>=a.length)
throw ...;
}
b=a[idx];
If you use -release, the debug {} section is eliminated and there is no cost in speed. On the other hand, with the new version, the result would be something like
int idx=someFunc();
if (idx<0) {
idx+=a.length;
}
debug {
if (idx<0 || idx>=a.length)
throw ...;
}
b=a[idx];
The problem is that in the general case, the compiler cannot determine whether idx will be negative or not, and cannot optimize away the first if() statement, meaning every array access will be notably slower, even if you don't use negative indices at all.
Is that inefficiency worth the change? I don't think it is...
xs0
|
April 05, 2006 Re: Array indexing and slicing | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | In article <e106tf$1l5v$1@digitaldaemon.com>, novice2 says... >but "indexing from the end". if we express it in current syntax, then will no difference. we should compare equivalent code. at least codes with equivalent results of it execution. See xs0's reponse. |
Copyright © 1999-2021 by the D Language Foundation