Skip to content
langera edited this page Nov 2, 2014 · 12 revisions

Slab

Slab is a storage mechanism that can store up to 263-1 (Long.MAX_VALUE) java objects.

It provides the basic Collection operations - add, get, remove, iterator and size.

It also abstracts the actual storage and will work with any implementation of SlabStorage. The project provides several implementations, specifically ones that use either heap or direct memory (offHeap) and sun.misc.Unsafe or ByteBuffer.

The Slab will serialize the Java object to bytes when storing it into memory. The SlabStorage interface limits it to only serializing primitives or arrays of primitives. The other limitation imposed by the Slab is that the size of the object serialized content must be fixed and known in advance. This allows the Slab to manage gaps in the storage and implement compaction.

A Slab will internally allocate chunks of memory and store the objects in those chunks. The size of every chunk is passed in the Slab constructor and the Slab will allocate chunks as needed until the total size of memory reaches Long.MAX_VALUE bytes.

Slab

Chunk size = n (depends on Storage impl. but limited by Slab itself only by size of long (263-1 bytes)).

Maximum Slab size = Minimum (263-1, 231-1 * n)

Creating a slab with a large chunk size will mean that larger chunks of memory will be allocated and reduce the overhead of allocating a new chunk. However, smaller chunk size gives the compaction operation a finer grained memory chunks and allows it to optimize its performance.

Each slab chunk maintains a list of free areas in its memory. This list is maintained as a linked list by storing the address of the next free address in the area reserved for the object data. (this means that minimum object size must be 8 bytes).

The root of the list is stored for every chunk (as the long field freeListIndex). -1 denotes a null.

Slab Chunk

Every insert into a slab will first look at the "free list" to fill in any gaps. The insert searches for gaps from the first chunk to the last. If no gap is found, it will insert the object as the last item on the last chunk.

During compaction, the Slab will work backwards from the last chunk to the first and will attempt to remove all data from that last chunk into gaps in previous chunks. If successful, it will remove the chunk and free its memory.

To allow that, the limitation of knowing the object size in advance was imposed.

Access to the content is done via a Flyweight object that can implement the same interface as the original Java object.

The SlabFlyweight object is a mutable object that will internally hold the address to the data of the instance it represents currently. By implementing the SlabFlyweight interface, you will define:

  • how to de/serialize the data into memory
  • how to de/serialize a null value
  • how to de/serialize a free address in the slab (used by the linked list of free gaps in the slab)

slab flyweight

Clone this wiki locally