Issue 9976 - Needlessly large instantiation depth in std.typetuple algorithms
Summary: Needlessly large instantiation depth in std.typetuple algorithms
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: bootcamp, performance
Depends on:
Blocks:
 
Reported: 2013-04-21 22:25 UTC by khurshid
Modified: 2024-12-01 16:17 UTC (History)
4 users (show)

See Also:


Attachments
test staticMap algorithm (1.57 KB, text/x-dsrc)
2013-04-21 22:25 UTC, khurshid
Details

Note You need to log in before you can comment on or make changes to this issue.
Description khurshid 2013-04-21 22:25:01 UTC
Created attachment 1210 [details]
test staticMap algorithm

More compile time algorithms (as, staticIndexOf, staticMap, EraseAll,
NoDuplicates, etc) - implemented as recursion. But this implementation need
deep recursion. So I had test  staticMap with more 500 arguments. It's not
compiled with normal configuration of dmd . After that I rewrite own staticMap2
with little change:

template staticMap2(alias F, T...)
{
   static if (T.length == 0)
   {
       alias TypeTuple!() staticMap2;
   } else static if (T.length == 1)
   {
       alias TypeTuple!(F!(T[0])) staticMap2;
   } else {
      alias TypeTuple!( staticMap2!(F, T[0..$/2]), staticMap2!(F,T[$/2..$]))
staticMap2 ;
}


And this variant compiled more than 32768 arguments.

For small arguments both version compiled almost the same time.

Which opinion is on this matter?


-------------------------------
test program is attachment.
---------------------------------
Comment 2 github-bugzilla 2013-04-22 10:18:48 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/43d1196c0701103f3e666ec4282055d7ed407be9
fix Issue 9976 - more compile time algorithms implementation used deep recursion

Reduce instantiation depth in staticMap, allSatisfy, anySatisfy, and Filter.

https://github.com/D-Programming-Language/phobos/commit/cf3a3ee0a5cc7f7ed3f9e6ced31ea5d004f8df14
Merge pull request #1271 from 9rnsr/fix9976

Issue 9976 - more compile time algorithms implementation used deep recursion
Comment 3 David Nadlinger 2013-04-22 10:24:03 UTC
I hope you didn't work on a pull request as well; Kenji was fast(er), as usual.

If you have additional suggestions, feel free to reopen the issue or post a new one.
Comment 4 khurshid 2013-04-23 00:27:06 UTC
(In reply to comment #3)
> I hope you didn't work on a pull request as well; Kenji was fast(er), as usual.
> 
> If you have additional suggestions, feel free to reopen the issue or post a new
> one.

Yes, I am first time here.
Comment 5 khurshid 2013-04-23 00:36:32 UTC
And this :))

template Reverse(TList...)
{
    static if (TList.length <= 1 )
		alias TList Reverse;
    else 
        alias TypeTuple!( TList[$-1], Reverse!(TList[1..$-1]), TList[0]) Reverse;
    
}

:)))
Comment 6 khurshid 2013-04-23 01:05:52 UTC
(In reply to comment #5)
> And this :))
> 
> template Reverse(TList...)
> {
>     static if (TList.length <= 1 )
>         alias TList Reverse;
>     else 
>         alias TypeTuple!( TList[$-1], Reverse!(TList[1..$-1]), TList[0])
> Reverse;
> 
> }
> 
> :)))

or 

template Reverse (TList...)
{
	static if (TList.length <= 1 )
	{
		alias Reverse  = TList;
	} else {
		alias Reverse  = TypeTuple!( Reverse !(TList[$/2..$]) , Reverse!(TList[0..$/2]));
	}
}
Comment 7 Kenji Hara 2013-04-23 22:42:50 UTC
(In reply to comment #5)
> And this :))
> 
> template Reverse(TList...)
[snip]

https://github.com/D-Programming-Language/phobos/pull/1274
Comment 8 khurshid 2013-04-23 23:18:33 UTC
(In reply to comment #7)
> (In reply to comment #5)
> > And this :))
> > 
> > template Reverse(TList...)
> [snip]
> 
> https://github.com/D-Programming-Language/phobos/pull/1274

Thanks, Kenji Hara.
Comment 9 khurshid 2013-04-24 01:18:55 UTC
I'm not sure, but, is this right?

template MostDerived(T, TList...)
{
    static if (TList.length == 0)
    {
       alias MostDerived = T;
    }
    else static if (TList.length == 1 )
    {
         static if (is (TList[0] : T) )
         { 
             alias  MostDerived = TList[0];
         }
         else
         {
             alias MostDerived = T;
         }
    }
    else
    {
       alias Half = MostDerived!(T, TList[0..$/2]);
       alias MostDerived = MostDerived!(Half, TList[$/2..$]);
    }
}
Comment 10 github-bugzilla 2013-05-31 18:55:57 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/d3df489171890ccdd73708a3edfa047efa01c4d2
fix Issue 9976 - Needlessly large instantiation depth in std.typetuple algorithms

https://github.com/D-Programming-Language/phobos/commit/69f5377acd873301fd9f7cd742e9229050bb4342
Merge pull request #1274 from 9rnsr/fix9976

Apply ddoc-ed unittest feature in std.typetuple, and fix issue 9976
Comment 11 Andrej Mitrovic 2013-05-31 18:57:43 UTC
(In reply to comment #9)
> I'm not sure, but, is this right?
> 
> template MostDerived(T, TList...)

Is this the last one left? Someone should make a pull for it.
Comment 12 khurshid 2013-06-14 06:30:59 UTC
(In reply to comment #11)
> (In reply to comment #9)
> > I'm not sure, but, is this right?
> > 
> > template MostDerived(T, TList...)
> 
> Is this the last one left? Someone should make a pull for it.

Not only this, I think that all following algorithms can easily change, too:
1. DerivedToFront
2. MostDerived
3. ReplaceAll        which used GenericReplaseAll
4. Replace           which used GenericReplace
5. NoDuplicates     
6. EraseAll          which used GenericEraseAll
7. Erase             which used GenericErase
8. staticIndexOf     which used genericIndexOf

Need only little think :-)
Comment 13 Andrej Mitrovic 2013-06-14 08:32:01 UTC
(In reply to comment #12)
> 8. staticIndexOf     which used genericIndexOf

I've reported about this before:
http://forum.dlang.org/thread/mailman.1418.1346015010.31962.digitalmars-d@puremagic.com

http://forum.dlang.org/thread/CAJ85NXA3F5se9hv3iA80hyQbwXBuP-ffon6ay=AXG+E11sicdw@mail.gmail.com

A speedup would be very welcome.
Comment 14 khurshid 2013-06-14 11:22:03 UTC
(In reply to comment #13)
> (In reply to comment #12)
> > 8. staticIndexOf     which used genericIndexOf
> 
> I've reported about this before:
> http://forum.dlang.org/thread/mailman.1418.1346015010.31962.digitalmars-d@puremagic.com
> 
> http://forum.dlang.org/thread/CAJ85NXA3F5se9hv3iA80hyQbwXBuP-ffon6ay=AXG+E11sicdw@mail.gmail.com
> 
> A speedup would be very welcome.

I've reported also, today :-). 

http://forum.dlang.org/thread/xwzivolcokzulonslkbq@forum.dlang.org

I think that need implement all linear algorithms   with "bisection" principle  as above , instead of "head/tail" principle.
Comment 15 dlangBugzillaToGithub 2024-12-01 16:17:24 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/9604

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB