Question

Sorry for asking second time about specialization, but I haven't good understanding of what the heck is going on yet...
So, I have one project (Gomoku game with AI), and I decided to use my own simple and dirty @specialized ad-hoc collections in the hot part of it, because I must store primitive types without boxing. The problem is that this doesn't really help, because in jvisualvm's Sampler I clearly see

scala.runtime.BoxesRunTime.boxToShort()

eating up thousands of ms when the optimal move search starts running.

The project: https://github.com/magicgoose/Gomoku
The file with the poor "collections": https://github.com/magicgoose/Gomoku/blob/master/src/magicgoose/gomoku/ai/SpecializedCollections.scala
The method, which causes boxing (one of them, I think):

trait Indexed[@specialized T] extends Enumerable[T] {
  @inline def length: Int
  @inline def apply(i: Int): T
// ...
  @inline final def findIndex(fun: T => Boolean) = {
    @tailrec def find(i: Int): Int = {
      if (i < length) {
        if (fun(this(i))) i
        else find(i + 1)
      } else -1
    }
    find(0)
  }
}

I have seen another project (debox: https://github.com/non/debox), which tries to accomplish the similar thing (data collections without primitive boxing), but I don't really understand how it is done there.

Was it helpful?

Solution

This has an easy answer: Function1 is not specialized on Short arguments, only Int, Long, Float, and Double. So when you call fun you need to box on the way in.

Either use your own function class--sadly lacking the convenient shorthand!--or make sure you are not using Short => Boolean but rather Int => Boolean (and the types know it). Note that when I said it was easy, I meant only easy to explain the problem: neither solution is all that easy to implement, but at the moment this is what's necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top