debox icon indicating copy to clipboard operation
debox copied to clipboard

debox.Map.contains boxes (!)

Open denisrosset opened this issue 10 years ago • 5 comments

debox.Map[Int, AnyRef].contains(key: Int) boxes, I do not know exactly why, but the performance suffers quite a bit:

untitled

This is on Scala 2.11.2. I wonder if it is the inner tail recursive method that causes specialization to fail.

denisrosset avatar Oct 21 '14 11:10 denisrosset

So, this is actually a specialization failure. If you had a debox.Map[Int, Int] you would not see this problem.

Currently it seems like mixed primitive/Object specializations fail. There was a feature awhile ago ("AnyRef specialization") which was designed to solve this exact problem. But it doesn't seem to be working.

I'm looking into this but it's possible that Debox can't fix the problem :/

non avatar Oct 21 '14 14:10 non

Correct. I think I found the culprit: the specialized annotation (which is performed on the value type) without any arguments involves primitives, excluding AnyRef.

See https://github.com/scala/scala/blob/v2.10.4/src/library/scala/specialized.scala#L1

Thus for the pair (Int, AnyRef), the only available variant is (AnyRef, AnyRef). I'm trying to include explicitly AnyRef for the value type and see if it boxes. Will keep you informed.

denisrosset avatar Oct 22 '14 16:10 denisrosset

No, even when specializing on (Int, AnyRef) for both A and B, it still boxes, even when writing Map[Int, AnyRef] and not a subtype of AnyRef.

Removing the specialization on B, the boxing disappears.

I will propose a hackish solution in a PR.

denisrosset avatar Oct 22 '14 17:10 denisrosset

See #20.

denisrosset avatar Oct 22 '14 22:10 denisrosset

I found a solution for that problem, and ended up implementing a new library for fast data structures: https://github.com/denisrosset/metal .

Metal provides data structures that can operate on a mix of primitive/AnyRef types such a Map2[Int, String, Long], with the same functionality of scala.collection.Map[Int, (String, Long)], however without boxing.

However, one drawback of Metal is that it is heavily based towards array-based data structures. For example, linked lists are difficult to implement on its framework.

denisrosset avatar Nov 19 '15 14:11 denisrosset