http://d.puremagic.com/issues/show_bug.cgi?id=9732
Summary: Do not call opAssign() for the first assignment to
member in the constructor
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Keywords: performance
Severity: enhancement
Priority: P3
Component: DMD
AssignedTo: [email protected]
ReportedBy: [email protected]
--- Comment #0 from Ali Cehreli <[email protected]> 2013-03-15 17:16:46 PDT ---
This is about struct members of both structs and classes.
It is a common idiom in constructors to assign rvalues to members:
this(int i)
{
this.inner = Inner(i); // <-- assign from rvalue
}
If 'Inner' above is a struct that has an rvalue opAssign() defined, currently
dmd compiles a call to that opAssign(). However, it is safe to elide that call
for the first such assignment in the constructor. Rather, the compiler can blit
the rvalue on top of the .init state of the member.
The following program demonstrates the two calls to opAssign() that could be
elided:
import std.stdio;
struct Inner
{
int i;
void opAssign(Inner rhs)
{
writeln("rvalue opAssign called");
}
}
struct OuterStruct
{
Inner inner;
this(int i)
{
writeln("Assigning to OuterStruct.inner");
this.inner = Inner(i);
}
}
class OuterClass
{
Inner inner;
this(int i)
{
writeln("Assigning to OuterClass.inner");
this.inner = Inner(i);
}
}
void main()
{
writeln("\nConstructing main.inner");
auto inner = Inner(42);
writeln("\nConstructing main.outer_struct");
auto outer_struct = OuterStruct(43);
writeln("\nConstructing main.outer_class");
auto outer_class = new OuterClass(44);
}
The output:
Constructing main.inner
Constructing main.outer_struct
Assigning to OuterStruct.inner
rvalue opAssign called // <-- could be elided
Constructing main.outer_class
Assigning to OuterClass.inner
rvalue opAssign called // <-- could be elided
Note that if dmd merely blitted the rvalue, the two lines "rvalue opAssign
called" would not be printed.
Thank you,
Ali
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------