<div dir="ltr"><div dir="ltr">On Thu, Apr 23, 2020 at 12:05 AM Steven Schveighoffer via Digitalmars-d <<a href="mailto:digitalmars-d@puremagic.com">digitalmars-d@puremagic.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 4/22/20 9:35 AM, Manu wrote:<br>
> On Wed, Apr 22, 2020 at 11:20 PM Steven Schveighoffer via Digitalmars-d <br>
> <<a href="mailto:digitalmars-d@puremagic.com" target="_blank">digitalmars-d@puremagic.com</a> <mailto:<a href="mailto:digitalmars-d@puremagic.com" target="_blank">digitalmars-d@puremagic.com</a>>> wrote:<br>
> <br>
>     On 4/22/20 8:04 AM, Manu wrote:<br>
>      > We have a compile time problem, and this is basically the cure.<br>
>      > Intuitively, people imagine CTFE is expensive (and it kinda is), but<br>
>      > really, the reason our compile times are bad is<br>
>     template instantiation.<br>
>      ><br>
>      > This DIP single-handedly fixes compile-time issues in programs I've<br>
>      > written by reducing template instantiations by near-100%, in<br>
>     particular,<br>
>      > the expensive ones; recursive instantiations, usually<br>
>     implementing some<br>
>      > form of static map.<br>
>      ><br>
>      > <a href="https://github.com/dlang/DIPs/pull/188" rel="noreferrer" target="_blank">https://github.com/dlang/DIPs/pull/188</a><br>
>      ><br>
>      > This is an RFC on a draft, but I'd like to submit it with a<br>
>     reference<br>
>      > implementation soon.<br>
>      ><br>
>      > Stefan Koch has helped me with a reference implementation, which<br>
>     has so<br>
>      > far gone surprisingly smoothly, and has shown 50x improvement in<br>
>     compile<br>
>      > times in some artificial tests.<br>
> <br>
>     Yes please! Where is the reference implementation? I want to try some<br>
>     things out.<br>
> <br>
> <br>
> The link is in the DIP.<br>
<br>
Oops, it's at the top, I missed that. Thanks.<br>
<br>
> Most tests we've done are working, except template instantiation <br>
> expansion is not yet implemented: ie, Thing!(Tuple, x, y)...<br>
> That's got a lot of implementation baggage attached to it in DMD.<br>
<br>
Ugh, I think I won't be able to test then, most of this stuff works with <br>
lists of types, not values.<br></blockquote><div><br></div><div>Types work now, it just depends what you do with them.</div><div>For instance, `cast(TypeTup)ValueTup` works right now.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
And come to think of it, staticMap works, but I need Filter as well. I <br>
suppose it can help a bit, but I still am going to have tons of <br>
"temporary" templates that are going to clog up the memory.<br></blockquote><div><br></div><div>I'd like to see some of your use cases. I do expect you'll need some shim templates in some cases (ie, filter), but the major difference is that they won't tend to be recursive expansions. Recursive expansions have quadratic cost... without those, your compilation should return to a linear cost world.</div><div><br></div><div>Fold/reduce operations can be proposed similarly to this as a follow up. It might be possible to specify a filter combining a map + fold.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Stefan, where's that implementation of first class types for CTFE-only <br>
functions you promised? ;)</blockquote><div><br></div><div>We've been talking about this, and I actually think this lays some foundation for some of that work.</div><div>Type functions is a pretty big spec challenge. This will solve a huge amount of perf issues, simplify code, and it's a very simple addition relatively speaking.</div><div><br></div><div>Another development that would benefit us hugely would be first-class tuples which Timon (I think?) has been working on for some time.</div><div>That would eliminate the awkward AliasSeq hack, and simplify the implementation. In a first-class tuple world, we may be able to nest tuples, whereas today, nested AliasSeq flatten.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> <br>
> I expect you won't be able to run practical tests on real code yet <br>
> without TemplateInstance expansion.<br>
> The problem is that existing code is factored exclusively into <br>
> template instantiations, so a trivial experiment will apply to existing <br>
> code in that form.<br>
<br>
The trivial experiment to test is to take a list of types with possible <br>
duplicates and remove all duplicates. It doesn't have to be in any order.<br></blockquote><div><br></div><div>Probably not possible with nothing but a map operation. There will be additional logic, but an efficient map should improve the perf substantially.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I'm hoping something like this might work:<br>
<br>
template NewFilter(pred, T...)<br>
{<br>
    template process(U) {<br>
      static if(pred!U) alias process = U;<br>
      else alias process = AliasSeq!();<br>
    }<br>
    alias NewFilter = AliasSeq!(process!(T)...); // do I need the <br>
AliasSeq here?<br>
}<br></blockquote><div><br></div><div>This should work. No, you don't need the AliasSeq.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
template RemoveDuplicates(T...)<br>
{<br>
    template keepIt(U) {<br>
       enum isSame(V) = __traits(isSame, U, V);<br>
       enum keepIt = NewFilter!(isSame, T).length  == 1;<br>
    }<br>
    alias RemoveDuplicates = NewFilter!(keepIt, T);<br>
}<br></blockquote><div><br></div><div>I think efficient implementation here would depend on a static fold, which I plan for a follow-up, and it's a very trivial expansion from this DIP.</div><div>Static reduce would allow `...` as an argument to a BinOp</div><div>Ie; `Tup + ...` would expand `Tup[0] + Tup[1] + Tup[2] + ...`</div><div>You could do `is(FindType

== Tup) || ...`, and it would evaluate true if FindType exists in Tup, with no junk template instantiations!</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Will this work better? You will still have a bunch of NewFilter, <br>
process, keepIt, and isSame instantiations, all with horribly long <br>
symbol names. But of note is there is no recursive instantiation patterns.<br></blockquote><div><br></div><div>Using fold as above, you can eliminate the boilerplate.</div><div>I don't want to propose that here though. It's a relatively trivial expansion from this initial DIP.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
One thing I noticed, in order to use a property on a static map <br>
expansion (i.e. call a function with the resulting sequence, or access <br>
.length), you will need extra parentheses to distinguish the ... token <br>
from the . token.<br></blockquote><div><br></div><div>> 

call a function with the resulting sequence

</div><div><br></div><div>Not sure what you mean exactly?</div><div><br></div><div>Calling a function that receives the sequence as var args:</div><div>  f(TupExpr...)</div><div><div><br></div><div>Calling a function for each element in the sequence?</div><div></div></div><div>  f(TupExpr)...</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Is there an easier/better way to do this with the new feature?<br></blockquote><div><br></div><div>

Can you show an example? Maybe the precedence is wrong?</div></div></div>