Take the 2-minute tour ×

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


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

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.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
Oops. This is so strange. Btw, I can get rid of Short and use Int everywhere and pack 2 Ints to Int instead of packing 2 Bytes to Short, this is dirty, but ok for this particular application, since the ranges of valuaes are anyway limited. Now I'll try it and see if there will be an improvement. –  Sarge Borsch Mar 11 '13 at 14:27
I have switched to Ints and now there is no boxing. –  Sarge Borsch Mar 11 '13 at 15:11
add comment

Your Answer


