Quickly packed arrays of structures in Scala

I'm learning what it takes to turn an existing mixed Python / C ++ mixed number base into a mixed Scala / C ++ (ideally basically Scala eventually). I expect that the biggest problem will be the many arrays of structures. For example, in C ++ we have types such as

Array<Vector<double,3>> # analogous to double [][3] Array<Frame<Vector<double,3>>> # a bunch of translation,quaternion pairs 

They can be converted back and forth between Python and C ++ without copying thanks to Numpy.

In the JVM, since unboxed arrays can have only a few types, the only way I can imagine is to create (1) a type in a Scala box for each structure, for example Vector<double,3> and (2) a typed thin wrapper around Array[Double] , which knows what structure it should be and creates / consumes single packets alone as needed.

Are there existing libraries that do such a thing, or implement any alternatives for packed arrays of structures? Does anyone have any experience with what performance symbols will be, and can existing compilers and JVMs optimize fields in at least a non-polymorphic, sealed case?

Please note that packaging and nice input are not optional: without packaging, I will quickly explode the memory, and if all I have is Array [Double] A system like C ++ (unfortunately) wins.

+4
source share
1 answer

The question is whether there is anything other than numbers. If this is just a bunch of doubles, you can write a wrapper in Scala, but you should not expect to avoid boxing. Instead, consider writing mutable wrappers:

 trait Vec3 { def x: Double def y: Double def z: Double } class ArrayedVec3(array: Array[Double]) extends Vec3 { private[this] var index = 0 def goto(i: Int) = { index = i*3; this } def x = array(index) def y = array(index+1) def z = array(index+2) } 

You can make ArrayedVec3 implement Iterator by returning as next or other things for cases where you want ease of use rather than efficiency.

But the fact is that if you are ready to independently control the creation and movement of these adapters, you do not need to worry about boxing. You only create a “box” once, and then jump to where you need it.

If you are satisfied with the work within ~ 2x of C ++ and are aiming for single-threaded use, this should do the trick. (He worked for me in the past.)

+3
source

Source: https://habr.com/ru/post/1444037/


All Articles