Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

1011 B
Raw Blame
package mem

import (
	"sync"
)

var pools []*sync.Pool

const (
	min_size = 1024
)

func bitCount(size int) (count int) {
	for ; size > min_size; count++ {
		size = (size + 1) >> 1
	}
	return
}

func init() {
	// 1KB ~ 256MB
	pools = make([]*sync.Pool, bitCount(1024*1024*256))
	for i := 0; i < len(pools); i++ {
		slotSize := 1024 << i
		pools[i] = &sync.Pool{
			New: func() interface{} {
				buffer := make([]byte, slotSize)
				return &buffer
			},
		}
	}
}

func getSlotPool(size int) (*sync.Pool, bool) {
	index := bitCount(size)
	if index >= len(pools) {
		return nil, false
	}
	return pools[index], true
}

func AllocationSize(size int) int {
	if _, found := getSlotPool(size); found {
		return min_size << bitCount(size)
	}
	return size
}

func Allocate(size int) []byte {
	if pool, found := getSlotPool(size); found {
		slab := *pool.Get().(*[]byte)
		return slab[:size]
	}
	return make([]byte, size)
}

func Free(buf []byte) {
	if pool, found := getSlotPool(cap(buf)); found {
		pool.Put(&buf)
	}
}