merging map/filter/reduce/... in D
Ivan Kazmenko via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jan 28 23:46:13 PST 2016
On Friday, 29 January 2016 at 07:17:04 UTC, glathoud wrote:
> I have the impression that function implementations are not
> merged:
>
> return fun0(fun1(a));
>
> For example, fun1(a) outputs a temporary array, which is then
> used as input for fun0. Merging the implementations of fun0 and
> fun1 would eliminate the need for a temporary array.
If fun1(a) indeed eagerly returns a temporary array, you are
right. Still, if the author of fun1 cares to be generic enough,
it usually returns a lazy range: a struct with front, empty and
popFront. Whenever fun0 requests the next element of the range,
fun1 calculates it and gives it away (returns front and calls
popFront).
Note that all this - which function calls which other function -
is known at compile time. To merge the actual code of fun0 and
popFront of fun1's return value is then the job for the optimizer
pass, it's basically inlining.
Note that a temporary array can theoretically also be optimized
out by an advanced optimizer.
Ivan Kazmenko.
More information about the Digitalmars-d-learn
mailing list