Skip to content

Latest commit

 

History

History
36 lines (23 loc) · 1.32 KB

README.md

File metadata and controls

36 lines (23 loc) · 1.32 KB

Gravity

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.

Why would I need this?

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"))

Why the name Gravity?

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

Read this post for more info

Future Ideas

  1. Try writing an in-memory cache on top of gravity.