Pair
Pair is a Go package that provides a low memory key/value object that takes up one allocation. It's useful for in-memory key/value stores and data structures where memory space is a concern.
Data structure
The allocation is a single packed block of bytes with the following format:
ValueSize uint32 |
KeySize uint32 |
Value []byte |
Key []byte |
Using
To start using Pair, install Go and run go get
:
$ go get -u github.com/tidwall/pair
Create a new Pair:
item := pair.New([]byte("user:2054:name"), []byte("Alice Tripplehorn"))
Access the Pair data:
item.Key() []byte // returns the key portion of the pair.
item.Value() []byte // returns the value portion of the pair.
item.Size() int // returns the exact in-memory size of the item.
item.Zero() bool // returns true if the pair is unallocated.
Unsafe pointer access:
item.Pointer() unsafe.Pointer // returns the base pointer
pair.FromPointer(ptr unsafe.Pointer) Pair // returns a Pair with provided base pointer
Josh Baker @tidwall
License
Pair source code is available under the MIT License.