http://d.puremagic.com/issues/show_bug.cgi?id=5546
Summary: Assigning and initializing structs from functions make more copies than necessary Product: D Version: D2 Platform: x86_64 OS/Version: Linux Status: NEW Severity: enhancement Priority: P2 Component: DMD AssignedTo: nob...@puremagic.com ReportedBy: akb...@gmail.com --- Comment #0 from akb...@gmail.com 2011-02-07 23:31:56 PST --- When initializing a struct from a function that returns by value, more copies (calling the post blit and destructors) are called than are necessary. For example, see the attached source file. The output for TestCopy.d is currently: Creating temp Copying temp Deleting temp Creating copy Copying copy Deleting copy Forwarding copy Copying copy Deleting copy Returning global Copying global Deleting global Deleting copy Deleting temp Ideally, the output should look like this: Creating temp Creating copy Forwarding copy Returning global Copying global Deleting global Deleting copy Deleting temp When a struct is being initialized by the return value of a function, apart from the memory being blitted over, no post blit or destructor should need to be called, since semantically it's equivalent to directly initializing the struct in the called function. This can be achieved by always returning a local object and not destructing the local object being returned. In the case of globalFunc(), which is returning a non-local object, a temporary would be made before returning from globalFunc(). When assigning the returning value of a function to a struct that's already initialized, additional optimizations can be made if no custom assignment operator exists. For example: Test testVal; testVal = function(); This will create the copy for the return value of function(), post blit testVal, destroy the previous value of testVal, then destroy the return value of function(). If Test or any of its members have an overridden assignment operator, they must be called. However, in the case where there is no custom assignment operator, the post blit of testVal and destruction of the return value of function() can be omitted, since you are semantically moving the value from the return value to testVal. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------