Reified Lambda Functions

Howard Lovatt howard.lovatt at iee.org
Fri Jan 8 06:06:20 PST 2010


If you stick to primitives, e.g. double in this example:

  static double reduce(final double[] ds, double initial, final
_Callable_double_double_double reducer) {
    for (final double d : ds) { initial = reducer.callPrimitive(initial, d);
}
    return initial;
  }

  static <T> T reduce(final T[] ts, T initial, final _Callable_2<? extends
T, ? super T, ? super T> reducer) {
    for (final T t : ts) { initial = reducer.call(initial, t); }
    return initial;
  }

  static <T> void time(final _Callable_0<T> c) {
    final long start = System.currentTimeMillis();
    final T result = c.call();
    final long finish = System.currentTimeMillis();
    System.out.println(c + " took " + (finish - start) + " ms and returned "
+ result);
  }

  static void timeTest() {
    final int size = 1024 * 1024;
    final double[] ds = new double[size];
    final Double[] ts = new Double[size];
    for (int i = 0; i < size; i++) { ts[i] = ds[i] = i; }
    final _Callable_double_double_double sumDs = new
_Callable_double_double_double() {
      @Override public double callPrimitive(final double a1, final double
a2) { return a1 + a2; }
    };
    time(new _Callable_double() {
      @Override public double callPrimitive() { return reduce(ds, 0, sumDs);
}
      @Override public String toString() { return "double[] test"; }
    });
    final _Callable_Dble_Dble_Dble sumTs = new _Callable_Dble_Dble_Dble() {
// Compiler bug! Should be Double not Dble
      @Override public Double call(final Double a1, final Double a2) {
return a1 + a2; }
    };
    time(new _Callable_Dble() { // Compiler bug! Should be Double not Dble
      @Override public Double call() { return reduce(ts, 0.0, sumTs); } //
note 0.0 - annoying inference bug!
      @Override public String toString() { return "Double[] test"; }
    });
  }

You don't get any boxing or unboxing with my suggested implementation and
hence you get speed.

 -- Howard.

2010/1/7 Neal Gafter <neal at gafter.com>

> On Thu, Jan 7, 2010 at 8:43 AM, Howard Lovatt <howard.lovatt at gmail.com>
> wrote:
> > There are two primary motivations:
> >
> > 1. To get the performance up, particularly for primitives. If you don't
> then
> > you might as well just have short innner class syntax and use generics.
> The
> > ParallelArray API won't fly without fast primitive support for example.
>
> Between the boxing/unboxing overhead required by your approach, and
> the casts that you say are inserted whenever generic APIs (such as
> ParallelArray) are used, it is hard to see how this proposal can do
> anything to improve performance compared to any of the alternatives
> that have been discussed.
>
> Cheers,
> Neal
>



-- 
 -- Howard.


More information about the lambda-dev mailing list