Thread overview
Few questions about staticMap
Feb 26, 2019
Mitacha
Feb 26, 2019
Ali Çehreli
Feb 27, 2019
Simen Kjærås
February 26, 2019
Hi everyone,

I checked, just out of curiosity, what is staticMap's implementation. It's implemented using recursive, this made me think if there is way to use static foreach instead. I came out with following solution: https://run.dlang.io/is/qvgJaw

I checked time it took compiler to compile std and my version for 7 parameters and there was no difference. The only difference I found was number of template instantiations: 1 for my code and 9 for std version.

Are there any benefits to implementing staticMap use recursive template?
February 26, 2019
On 02/26/2019 10:50 AM, Mitacha wrote:

> I checked, just out of curiosity, what is staticMap's implementation.
> It's implemented using recursive, this made me think if there is way to
> use static foreach instead.

I'm pretty sure staticMap was implemented way before 'static foreach'. Perhaps it could use non-static compile-time foreach but I don't know.

Ali

February 27, 2019
On Tuesday, 26 February 2019 at 18:50:39 UTC, Mitacha wrote:
> Hi everyone,
>
> I checked, just out of curiosity, what is staticMap's implementation. It's implemented using recursive, this made me think if there is way to use static foreach instead. I came out with following solution: https://run.dlang.io/is/qvgJaw
>
> I checked time it took compiler to compile std and my version for 7 parameters and there was no difference. The only difference I found was number of template instantiations: 1 for my code and 9 for std version.
>
> Are there any benefits to implementing staticMap use recursive template?

Some testing indicates there's not a whole lot to gain from the unrolled parts of your sMap, so here's a shorter version:

template sMap(alias F, T...) {
    mixin("alias sMap = AliasSeq!(",{
        string result;
        static foreach (i, _; T) {
            result ~= "F!(T["~i.stringof~"]), ";
        }
        return result;
    }(),");");
}

I took the liberty of comparing different implementations and workloads, and can't really see any big difference between sMap and staticMap in the cases tested. There is a very small tendency for sMap to be faster, but the difference from run to run tends to drown this out, so I'm not sure it's an actual difference. For completeness, here's the code I've been using to test:

template add1(int n) {
    enum add1 = n + 1;
}

struct testStruct(int n) {}

unittest {
    enum N = 10000;
    alias a = sMap!(add1, generate!N);
    alias b = staticMap!(add1, generate!N);
    alias c = sMap!(testStruct, generate!N);
    alias d = staticMap!(testStruct, generate!N);
}

template generate(int n) {
    mixin("alias generate = AliasSeq!("~{
        string result;
        static foreach (i; 0..n) {
            result ~= i.stringof~", ";
        }
        return result;
    }()~");");
}

I did find a different interesting tidbit, though: static foreach is significantly slower when used to iterate over a N..M range than over a tuple of the same length. Reported as https://issues.dlang.org/show_bug.cgi?id=19705

--
  Simen