Variants/case classes/algebraic data types/sums/oh my!

Vitaly Davidovich vitalyd at gmail.com
Sun Jun 12 18:28:45 UTC 2016


How would I use this to, e.g., propagate the length of an instance array as
a constant when I'm creating an instance of a class where I know statically
the size I want? Assume the CFG terminates before the array length is
requested (i.e. the constant used to instantiate the object cannot be
propagated itself to its use).

On Sunday, June 12, 2016, <forax at univ-mlv.fr> wrote:

>
>
> ------------------------------
>
> *De: *"Vitaly Davidovich" <vitalyd at gmail.com
> <javascript:_e(%7B%7D,'cvml','vitalyd at gmail.com');>>
> *À: *"John Rose" <john.r.rose at oracle.com
> <javascript:_e(%7B%7D,'cvml','john.r.rose at oracle.com');>>
> *Cc: *"Rémi Forax" <forax at univ-mlv.fr
> <javascript:_e(%7B%7D,'cvml','forax at univ-mlv.fr');>>,
> valhalla-dev at openjdk.java.net
> <javascript:_e(%7B%7D,'cvml','valhalla-dev at openjdk.java.net');>
> *Envoyé: *Dimanche 12 Juin 2016 19:30:21
> *Objet: *Re: Variants/case classes/algebraic data types/sums/oh my!
>
>
>
> On Saturday, June 11, 2016, John Rose <john.r.rose at oracle.com
> <javascript:_e(%7B%7D,'cvml','john.r.rose at oracle.com');>> wrote:
>
>> On Jun 11, 2016, at 5:12 PM, forax at univ-mlv.fr
>> <http://JAVASCRIPT-BLOCKED;> wrote:
>> >
>> >
>> > De: "John Rose" <john.r.rose at oracle.com <http://JAVASCRIPT-BLOCKED;>>
>> > À: "Rémi Forax" <forax at univ-mlv.fr <http://JAVASCRIPT-BLOCKED;>>
>> > Cc: "org openjdk" <org.openjdk at io7m.com <http://JAVASCRIPT-BLOCKED;>>,
>> valhalla-dev at openjdk.java.net <http://JAVASCRIPT-BLOCKED;>
>> > Envoyé: Dimanche 12 Juin 2016 01:02:52
>> > Objet: Re: Variants/case classes/algebraic data types/sums/oh my!
>> > Hi John,
>> >
>> >>> On Jun 11, 2016, at 4:18 AM, Remi Forax <forax at univ-mlv.fr
>> <http://JAVASCRIPT-BLOCKED;>> wrote:
>> >>>
>> >>> because K is reified, there will be two methods eval() at runtime,
>> one specialized for K = 0 and an other for K = 1,
>> >>> which means that the switch will be constant fold (the real 'switch'
>> is done when selecting the specialization).
>> >>>
>> >> That is an interesting use case for non-type class template parameters!
>> >>
>> > Here is another one,
>> > class ArrayList<any E> {
>> >   final E[] array;
>> >   int size;
>> >
>> >   public <Consumer<? super E> U> void forEach(U consumer) {
>> >      for(int i = 0; i < size; i++) {
>> >        consumer.accept(array[i]);
>> >      }
>> >   }
>> > }
>>
>> I think there are two simple ways to interpret your suggestion.  One is
>> to split and specialize forEach by concrete consumer *type*, and one is to
>> specialize by consumer *instance*.
>>
>>   public <__SpecializePerType U extends Consumer<? super E>> void
>> forEach(U consumer) {
>>      for(int i = 0; i < size; i++) {
>>        consumer.accept(array[i]);
>>      }
>>   }
>>   public <__SpecializePerInstanceOf U extends Consumer<? super E>> void
>> forEach(U consumer) {
>>      for(int i = 0; i < size; i++) {
>>        consumer.accept(array[i]);
>>      }
>>   }
>>
>>
>> > In the snippet above, forEach is specialized by the value of the lambda
>> taken as argument, which is another way to say that he code of forEach and
>> the code of the lambda should be inlined together.
>>
>> Yes; function parameters to templates are an important organizing feature
>> for C++ STL algorithms.  The sort method is also a good candidate for such
>> treatment.  C++ expresses per-type specialization using type parameters and
>> per-instance specialization using non-type parameters.
>
> Goes without saying, but non-type parameters are very useful to propagate
> constants, possibly quite "far" with sufficient inlining, and open up more
> optimization opportunities.  I'd love something similar in Java.
>
>
> It remember me that you can 'constantify' everything you want in Java
> https://gist.github.com/anonymous/7cabe890a28547dad0401dc7883cc4a7
>
> Rémi
>
>

-- 
Sent from my phone


More information about the valhalla-dev mailing list