www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Few questions about staticMap

reply Mitacha <mateusz.mitaszka gmail.com> writes:
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?
Feb 26 2019
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Feb 26 2019
prev sibling parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
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
Feb 27 2019