Avoiding boxing/unboxing within function

后端 未结 3 1838
[愿得一人]
[愿得一人] 2021-02-01 08:48

For a numeric intensive code I have written a function with the following signature:

def update( f: (Int,Int,Double) => Double ): Unit = {...}
3条回答
  •  心在旅途
    2021-02-01 09:16

    About the anonymous class which extends Function3 (the bytecode of which you show) generated by Scalac - it is not possible to call the overloaded apply with primitive parameters from within the b.update, because that update method takes a Function3, which does not have an apply with primitive parameters.

    From within the Function3 bytecode, the only apply is:

    public abstract java.lang.Object apply(java.lang.Object, java.lang.Object, java.lang.Object);
    

    You could instead use Function2[Long, Double], which is specialized on those types, and encode 2 integers x and y in a long as (x.toLong << 32) + y, and decode them as v & 0xffffffff and (v >> 32) & 0xffffffff.

提交回复
热议问题