opIndexMember
claptrap
clap at trap.com
Sun Dec 27 17:01:47 UTC 2020
On Sunday, 27 December 2020 at 15:02:16 UTC, Dennis wrote:
> On Sunday, 27 December 2020 at 14:46:43 UTC, claptrap wrote:
>> So you can do stuff like this....
>
> What's wrong with this?
> ```
> struct VecArray
> {
> float[] _x;
> float[] _y;
> float[] _z;
>
> Vector opIndex(size_t idx) {
> return Vector(_x[idx], _y[idx], _z[idx]);
> }
> }
> ```
>
> Performance in debug builds?
It wont work with ref returns, and performance in release builds.
I had some instances where the return type was being fully
constructed even though only one member was accessed, but I dont
remember exactly what code caused that.
godbolt with LDC -03...
===============================================
import std;
struct Vector { float x,y,z; }
struct VecArray
{
float[] _x;
float[] _y;
float[] _z;
Vector opIndex(size_t idx) {
return Vector(_x[idx], _y[idx], _z[idx]);
}
}
float doSomething1(ref VecArray array)
{
return array[0].x * 2.0;
}
float doSomething2(ref VecArray array)
{
return array._x[0] * 2.0;
}
==============================================
doSomthing1:
float example.doSomething1(ref example.VecArray):
push rax
cmp qword ptr [rdi], 0
je .LBB6_4
cmp qword ptr [rdi + 16], 0
je .LBB6_4
cmp qword ptr [rdi + 32], 0
je .LBB6_4
mov rax, qword ptr [rdi + 8]
movss xmm0, dword ptr [rax]
addss xmm0, xmm0
pop rax
ret
.LBB6_4:
lea rsi, [rip + .L.str.1]
mov edi, 11
mov edx, 12
call _d_arraybounds at PLT
===============================================
doSomthing2:
float example.doSomething2(ref example.VecArray):
push rax
cmp qword ptr [rdi], 0
je .LBB7_2
mov rax, qword ptr [rdi + 8]
movss xmm0, dword ptr [rax]
addss xmm0, xmm0
pop rax
ret
.LBB7_2:
lea rsi, [rip + .L.str.1]
mov edi, 11
mov edx, 23
call _d_arraybounds at PLT
More information about the Digitalmars-d
mailing list