boxing + unboxing a VT is not a no-op ?

Remi Forax forax at univ-mlv.fr
Thu Jul 12 18:43:26 UTC 2018


Hi guys,
it seems that operations likes VT -> Object -> VT are not optimized has being a no-op.

with IntBox being a value type that wrap an int,
this code is slow (not very slow, i.e not interpreter slow) 

   public int valuelist_intbox_innervalue_inlined_reduce() {
    __ByValue class Adder implements BiFunction<IntBox, IntBox, IntBox> {
      private final boolean nonEmpty;
      
      Adder() {
        nonEmpty = false;
        throw new AssertionError();
      }
      
      public IntBox apply(IntBox acc, IntBox element) {
        return acc.add(element);
      }
    }
    BiFunction<IntBox, IntBox, IntBox> mapper = __MakeDefault Adder();
    var sum = IntBox.zero();
    int size = valueList.size();
    for(int i = 0; i < size; i++) {
      sum = mapper.apply(sum, valueList.get(i));
    }
    return sum.intValue();
  }

while this code is fast (more than 10 times faster)
  public int valuelist_intbox_inlined_reduce() {
    var sum = IntBox.zero();
    int size = valueList.size();
    for(int i = 0; i < size; i++) {
      sum = sum.add(valueList.get(i));
    }
    return sum.intValue();
  } 

the difference is that instead of calling add on an IntBox to do the addition,
i used an anonymous value class which implement a functional interface which used Objects.
(valueList is just an array of IntBox wrapped in a value type).

regards,
Rémi





More information about the valhalla-dev mailing list