Referring to array element by descriptive name
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jan 14 10:38:17 PST 2017
On 01/14/2017 07:11 AM, albert-j wrote:
> Is it possible to refer to an array element by a descriptive name, just
> for code clarity, without performance overhead? E.g.
>
> void aFunction(double[] arr) {
> double importantElement = arr[3];
> ... use importantElement ...
> }
>
> But the above, I suppose, introduces an extra copy operation?
>
I've used nested functions before. Compiled with
-O -inline -boundscheck=off
even dmd produces exact code for the following three access methods:
import std.stdio;
void aFunction(double[] arr) {
ref importantElement() {
return arr[3];
}
writeln("Indexed element : ", arr[3]);
writeln("importantElement: ", importantElement);
double originalIdea = arr[3];
writeln("Original idea : ", originalIdea);
}
void main() {
}
Here are the three calls; comments added by me. The only difference is
RCX vs. RAX for one of the calls:
.text._D6deneme9aFunctionFAdZv segment
assume CS:.text._D6deneme9aFunctionFAdZv
_D6deneme9aFunctionFAdZv:
push RBP
mov RBP,RSP
sub RSP,010h
mov -010h[RBP],RDI
mov -8[RBP],RSI
; arr[3]
mov EDX,offset FLAT:_TMP0 at 32
mov EDI,012h
mov RSI,RDX
mov RAX,-8[RBP]
movsd XMM0,018h[RAX]
call _D3std5stdio18__T7writelnTAyaTdZ7writelnFNfAyadZv at PC32
; importantElement:
mov EDX,offset FLAT:_TMP0 at 32
mov EDI,012h
mov RSI,RDX
mov RCX,-8[RBP]
movsd XMM0,018h[RCX]
call _D3std5stdio18__T7writelnTAyaTdZ7writelnFNfAyadZv at PC32
; originalIdea:
mov EDX,offset FLAT:_TMP0 at 32
mov EDI,012h
mov RSI,RDX
mov RAX,-8[RBP]
movsd XMM0,018h[RAX]
call _D3std5stdio18__T7writelnTAyaTdZ7writelnFNfAyadZv at PC32
mov RSP,RBP
pop RBP
ret
0f1f
add byte ptr [RAX],0
add [RAX],AL
.text._D6deneme9aFunctionFAdZv ends
Ali
More information about the Digitalmars-d-learn
mailing list