Given a large byte array (consider as memory), Gravity helps in writing, reading and freeing of data onto the provided memory in a thread-safe way.
The main purpose of Gravity is to help save data off heap. Providing []byte
from memory mapped region to Gravity will allow it to save data out of heap.
The example uses https://github.com/glycerine/offheap to get byte slice of a memory mapped region
// create an offheap byte slice
allocation := offheap.Malloc(1000, "")
defer allocation.Free()
// initialize gravity with offheap byte slice
g, err := gravity.NewGravity(allocation.Mem)
// writes hello off heap
_, _ := g.Write([]byte("hello"))
Long story short, when writing data to the memory, free spaces with higher gravity is preferred. This tends to help merge freespaces to form bigger freespaces. Gravity of free space is defined as
size(fsi) * size(fsi+1) / distance2(fsi, fsi+1)
where size(fsi)
is the size of i-th free space
- Try writing an in-memory cache on top of gravity.