<div dir="ltr">Would you mind uploading the clang-side of this patch to phabricator? It will make it a little easier for me to perform a review.  See <a href="http://llvm.org/docs/Phabricator.html">http://llvm.org/docs/Phabricator.html</a> for more details.</div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 30, 2015 at 9:13 AM, Louis Dionne via cfe-commits <span dir="ltr"><<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I wrote a patch that adds a `__nth_element` builtin that allows fetching the<br>
n-th type of a parameter pack with very little compile-time overhead. The patch<br>
was inspired by r252036 and r252115 by David Majnemer, which add a similar<br>
`__make_integer_seq` builtin for efficiently creating a `std::integer_sequence`.<br>
At its core, the builtin makes the following specialization<br>
<br>
__nth_element<i, T1, ..., Tn><br>
<br>
equivalent to Ti. Further analysis of the problem this patch is trying to solve<br>
is available on a blog post I just wrote [1]. The post also present benchmarks<br>
showing the compile-time improvement over existing techniques.<br>
<br>
The patch is currently in a very rough state. It is my first time contributing<br>
to Clang, and I encountered some issues that I hacked around just to get a<br>
working solution. If there is interest in this patch, I will gladly improve<br>
it until it is in an acceptable state, but I will most likely need some<br>
pointers to do so. Things that are hacky right now:<br>
<br>
1. I wasn't able to create a non-type template parameter from a builtin type.<br>
   Hence, `__nth_element` must currently be used as<br>
     __nth_element<TypeOfTheIndex, Index, Ts...><br>
   instead of just `__nth_element<Index, Ts...>`, while forcing `Index` to be<br>
   something like `unsigned long`. This way, I was able to copy Majnemer's<br>
   code that creates non-type template parameters. This is probably trivial<br>
   to change, but I couldn't figure it out immediately.<br>
<br>
2. I wasn't sure how to retrieve an actual integer from an `APSInt`, so right<br>
   now I use `getExtValue()`. This is probably wrong, as I didn't really<br>
   understand what I was doing.<br>
<br>
There are certainly other issues that a code review would address, if we make<br>
it that far. Also, the name `__nth_element` is badly chosen, since it seems<br>
to refer to the `nth_element` algorithm of the standard library. We should<br>
settle on something else, but I have no preference.<br>
<br>
I also wrote a corresponding patch for libc++ to use the builtin to implement<br>
`std::tuple_element`. You can find both patches attached to this message. Also,<br>
these patches were initially posted as bug reports [2] and [3], respectively.<br>
<br>
Please let me know if there is any interest for going forward with these patches.<br>
<br>
Regards,<br>
Louis Dionne<br>
<br>
[1]: <a href="http://ldionne.com/2015/11/29/efficient-parameter-pack-indexing" rel="noreferrer" target="_blank">http://ldionne.com/2015/11/29/efficient-parameter-pack-indexing</a><br>
[2]: <a href="https://llvm.org/bugs/show_bug.cgi?id=25670" rel="noreferrer" target="_blank">https://llvm.org/bugs/show_bug.cgi?id=25670</a><br>
[3]: <a href="https://llvm.org/bugs/show_bug.cgi?id=25671" rel="noreferrer" target="_blank">https://llvm.org/bugs/show_bug.cgi?id=25671</a><br>
<br>
<br>_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
<br></blockquote></div><br></div>