densh / scala-offheap

Experimental type-safe off-heap memory for Scala.
BSD 3-Clause "New" or "Revised" License
532 stars 38 forks source link

Introduces embedded fields #34

Closed densh closed 9 years ago

densh commented 9 years ago

This pull requests introduces a new feature to scala-offheap: embedded fields. Rather than storing a reference to another data types, embedded fields let one to completely embed data of one class into another. For example:

@data class Point(x: Int, y: Int)
@data class Segment(@embed start: Point, @embed end: Point)

Here Segment is 16-byte structure that contains two points inside of its data layout. Allocation of Segment like:

Segment(Point(10, 20), Point(30, 40))

Will perform exactly one allocation with corresponding point data written directly into segment-allocated memory. If points are allocated separately the data will be copied over from previous allocations:

val p1 = Point(10, 20)
val p2 = Point(30, 40)
Segment(p1, p2) 

Reading an embedded field is just an arithmetic computation of inner pointer, it doesn't actually perform any memory accesses (thus no pointer chasing.)