Avoid initializing a struct field.
jerro
a at a.com
Sat May 4 12:00:17 PDT 2013
> What code have you used to test it, and what is the result you
> have seen?
I was using this code:
extern(C) void foo(Foo* r)
{
Foo tmp;
*r = tmp;
}
And here is the generated assembly (when using "= void"):
push %rbp
mov %rsp,%rbp
sub $0xc0,%rsp
mov %rdi,-0x10(%rbp)
movabs $0x65a360,%rsi
lea -0xc0(%rbp),%rdi
mov $0x15,%ecx
rep movsq %ds:(%rsi),%es:(%rdi)
movsb %ds:(%rsi),%es:(%rdi)
movsb %ds:(%rsi),%es:(%rdi)
movsb %ds:(%rsi),%es:(%rdi)
movsb %ds:(%rsi),%es:(%rdi)
lea -0xc0(%rbp),%rsi
mov -0x10(%rbp),%rdi
mov $0x15,%cl
rep movsq %ds:(%rsi),%es:(%rdi)
movsb %ds:(%rsi),%es:(%rdi)
movsb %ds:(%rsi),%es:(%rdi)
movsb %ds:(%rsi),%es:(%rdi)
movsb %ds:(%rsi),%es:(%rdi)
leaveq
retq
The problem here is that the code copies the entire object from
some static address instead of just the fields that need to be
initialized, and that can be a performance problem. The same is
true for your void version.
I tried using float[42] instead of int[42] and found out that
buffer actually isn't initialized to its default initializer if I
use "= void" (the numbers were all 0 instead of NaN), but the
performance cost is still there.
More information about the Digitalmars-d-learn
mailing list