DMD 0.177 release

BCS BCS at pathlink.com
Mon Dec 11 10:54:16 PST 2006


Walter Bright wrote:
> Tom S wrote:
>> The Foo instance returned from static opCall is copied, thus the 'ctor 
>> hack' doesn't have real access to the object it's constructing, not to 
>> mention the overhead of copying the struct to another place on stack...
> 
> 
> It's time to put the recurring efficiency argument to bed. Consider this 
> D code:
[...]

In many cases, it may be vary good. However consider the case where 
there is more than one return statement using different variables. You 
now need a much better optimizer to get this optimized down to "almost 
nothing". Furthermore, it doesn't reflect what is actually being done.

struct S
{
	static S err;
	int k, l;

	static S opCall(int i, int j)
	{
		S ret;
		ret.k=i;
		ret.l=j;

		if(ret.test)
			return ret;
		else
			ret err;
	}

	bool test(){...}
}


With constructors, it is not only simpler code, but looks like what is 
happening.

struct S
{
	static S err;
	int k, l;

	this(int i, int j)
	{
		k=i;
		l=j;

		if(!ret.test) this = err;
	}

	bool test(){...}
}



More information about the Digitalmars-d-announce mailing list