On 28/01/2011 20:25, Simen kjaeraas wrote:
Bruno Medeiros <[email protected]> wrote:
On 27/01/2011 21:05, Simen kjaeraas wrote:
Bruno Medeiros <[email protected]> wrote:
string[] func(string arg) pure {
string elem2 = "blah".idup;
return [ arg, elem2 ];
}
The compiler *cannot* know (well, looking at the signature only of
course) how to properly deepdup the result from the first return
value, so as to give the exact same result as if func was called again.
Could you please elucidate, as I am unsure of your reasoning for saying
the compiler cannot know how to deepdup the result.
string str = "blah";
string[] var1 = func(str);
string[] var2 = func(str);
How can the compiler optimize the second call to func, the one that is
assigned to var2, such that he deepdups var1 instead of calling func
again? Which code would be generated?
The compiler can't do that because of all the transitive data of var1,
the compiler doesn't know which of it was newly allocated by func, and
which of it was reused from func's parameters or some other global
inputs.
But for immutable data (like the contents of the elements of a string[]),
that doesn't matter, does it?
Maybe it won't matter for the *contents of the elements* of the string
array, but the whole result value has to be /the same/ as if the
optimization was not applied. Otherwise the optimization is invalid,
even if for most uses of the result value it would not make a difference
for the program.
--
Bruno Medeiros - Software Engineer