type variables
Schrom, Brian T
Brian.Schrom at pnnl.gov
Sun Aug 2 15:51:40 UTC 2020
On Sun, Aug 02, 2020 at 03:04:07PM +0000, Paul Backus via Digitalmars-d wrote:
>
> In fact, the naive recursive version of iota is actually even simpler:
>
> int[] iota(int start, int finish)
> {
> if (start > finish)
> return [];
> else
> return [start] ~ iota(start + 1, finish);
> }
>
> Again, the problem with this function is not complexity or readability, but
> performance. It performs many unnecessary memory allocations, and consumes
> (finish - start) stack frames instead of 1.
I'm disappointed that the code as written above appears to indeed
generate N stack frames from my looking with compiler explorer.
However, basically the same version, to me, seems to TCO, thus 1 stack
frame, with optimization turned on with ldc.
void iotaTCO( ref int[] array, int start, int finish)
{
if (start > finish)
return;
else
{
array ~= start;
iotaTCO(array, start + 1, finish);
}
}
I think I'm missing the essence of what is different between these two
functions and why TCO isn't applicable to the first.
More information about the Digitalmars-d
mailing list