cgobytepool

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 8 Imported by: 2

README

cgobytepool

MIT License GoDoc Go Report Card Releases

Shared byte pool implementation between C and Go(cgo)
unsigned char* in C / []byte in Go (convertible using unsafe.Slice or reflect.SliceHeader)
this pool shares using cgo.Handle

How to use

Need to declare extern in C and declare export in Go

/*
#include <stdlib.h>

extern void *bytepool_get(void *context, size_t size);
extern void bytepool_put(void *context, void *data, size_t size);
extern void bytepool_free(void *context);

static void ExampleCgo(void *ctx) {
  unsigned char *data = (unsigned char*) bytepool_get(ctx, 100);
  do_something(data);
  bytepool_put(ctx, data, 100);
  bytepool_free(ctx);
}
*/
import "C"

import (
	"unsafe"

	"github.com/octu0/cgobytepool"
)

//export bytepool_get
func bytepool_get(ctx unsafe.Pointer, size C.size_t) unsafe.Pointer {
	return cgobytepool.HandlePoolGet(ctx, int(size))
}

//export bytepool_put
func bytepool_put(ctx unsafe.Pointer, data unsafe.Pointer, size C.size_t) {
	cgobytepool.HandlePoolPut(ctx, data, int(size))
}

//export bytepool_free
func bytepool_free(ctx unsafe.Pointer) {
	cgobytepool.HandlePoolFree(ctx)
}

func ExampleGo(p cgobytepool.Pool) {
	ptr := p.Get(100)
	defer p.Put(ptr, 100)

	data := unsafe.Slice((*byte)(ptr), 100)
	println(len(data)) // => 100
	println(cap(data)) // => 100

	doSomething(data)
}

func main() {
	p := cgobytepool.NewPool(
		cgobytepool.DefaultMemoryAlignmentFunc,
		cgobytepool.WithPoolSize(1000, 16*1024),
		cgobytepool.WithPoolSize(1000, 4*1024),
		cgobytepool.WithPoolSize(1000, 512),
	)

	ExampleGo(p)

	h := cgobytepool.CgoHandle(p)
	C.ExampleCgo(unsafe.Pointer(&h))
}

Benchmark

goos: darwin
goarch: amd64
pkg: github.com/octu0/cgobytepool/benchmark
cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
BenchmarkCgoBytePool
BenchmarkCgoBytePool/cgohandle
BenchmarkCgoBytePool/cgohandle-8         	              218674	        4764 ns/op	   21209 B/op	      10 allocs/op
BenchmarkCgoBytePool/cgohandle_reflect
BenchmarkCgoBytePool/cgohandle_reflect-8 	              572455	        2039 ns/op	     198 B/op	       6 allocs/op
BenchmarkCgoBytePool/cgohandle_array
BenchmarkCgoBytePool/cgohandle_array-8   	              594552	        2082 ns/op	     198 B/op	       6 allocs/op
BenchmarkCgoBytePool/cgohandle_unsafeslice
BenchmarkCgoBytePool/cgohandle_unsafeslice-8         	  580590	        2040 ns/op	     198 B/op	       6 allocs/op
BenchmarkCgoBytePool/malloc
BenchmarkCgoBytePool/malloc-8                        	  433221	        3001 ns/op	   21008 B/op	       4 allocs/op
BenchmarkCgoBytePool/malloc_reflect
BenchmarkCgoBytePool/malloc_reflect-8                	10844132	       115.3 ns/op	      16 B/op	       1 allocs/op
BenchmarkCgoBytePool/malloc_reflect2
BenchmarkCgoBytePool/malloc_reflect2-8               	 9369612	       129.3 ns/op	      16 B/op	       1 allocs/op
BenchmarkCgoBytePool/malloc_unsafeslice
BenchmarkCgoBytePool/malloc_unsafeslice-8            	 8921526	       133.3 ns/op	      16 B/op	       1 allocs/op
BenchmarkCgoBytePool/malloc_unsafeslice2
BenchmarkCgoBytePool/malloc_unsafeslice2-8           	 8935269	       127.8 ns/op	      16 B/op	       1 allocs/op
BenchmarkCgoBytePool/go/malloc
BenchmarkCgoBytePool/go/malloc-8                     	 3910248	       334.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkCgoBytePool/go/cgo
BenchmarkCgoBytePool/go/cgo-8                        	  943117	        1166 ns/op	      96 B/op	       3 allocs/op
BenchmarkCgoBytePool/bp
BenchmarkCgoBytePool/bp-8                            	 3708741	       270.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkCgoBytePool/cbytes
BenchmarkCgoBytePool/cbytes-8                        	  231247	        5876 ns/op	   21064 B/op	       6 allocs/op
BenchmarkCgoBytePool/cgobytepool_cbytes
BenchmarkCgoBytePool/cgobytepool_cbytes-8            	 1795348	       659.6 ns/op	     144 B/op	       3 allocs/op
PASS

License

MIT, see LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	AppName string = "cgobytepool"
	Version string = "1.0.3"
)

Variables

This section is empty.

Functions

func CgoHandle

func CgoHandle(p Pool) cgo.Handle

func GoBytes added in v1.0.1

func GoBytes(data unsafe.Pointer, size int) []byte

func HandlePoolFree

func HandlePoolFree(ctx unsafe.Pointer)

func HandlePoolGet

func HandlePoolGet(ctx unsafe.Pointer, size int) unsafe.Pointer

func HandlePoolPut

func HandlePoolPut(ctx unsafe.Pointer, data unsafe.Pointer, size int)

func ReflectGoBytes added in v1.0.1

func ReflectGoBytes(data unsafe.Pointer, sizeLen, sizeCap int) []byte

func UnsafeGoBytes added in v1.0.1

func UnsafeGoBytes(data unsafe.Pointer, size int) []byte

Types

type CgoBytePool

type CgoBytePool struct {
	// contains filtered or unexported fields
}

func NewPool

func NewPool(alignFunc MemoryAligmentFunc, poolFuncs ...WithPoolFunc) *CgoBytePool

func (*CgoBytePool) AllocBytes

func (p *CgoBytePool) AllocBytes() int64

func (*CgoBytePool) Close

func (p *CgoBytePool) Close()

func (*CgoBytePool) Get

func (p *CgoBytePool) Get(size int) unsafe.Pointer

func (*CgoBytePool) Put

func (p *CgoBytePool) Put(b unsafe.Pointer, size int)

func (*CgoBytePool) Stats

func (p *CgoBytePool) Stats() PoolStats

func (*CgoBytePool) TotalAllocBytes

func (p *CgoBytePool) TotalAllocBytes() int64

type DoneFunc added in v1.0.1

type DoneFunc func()

func CBytes added in v1.0.1

func CBytes(p Pool, data []byte) (unsafe.Pointer, DoneFunc)

type MemoryAligmentFunc

type MemoryAligmentFunc func(int) int
var (
	DefaultMemoryAlignmentFunc MemoryAligmentFunc = func(n int) int {
		return ((n + defaultMemoryAlignmentSize) >> 3) << 3
	}
)

type Pool

type Pool interface {
	Get(int) unsafe.Pointer
	Put(unsafe.Pointer, int)
	Close()

	Stats() PoolStats
}

type PoolStats

type PoolStats struct {
	Allocs []struct {
		ID   int
		Size int64
		Len  int
		Cap  int
	}
	Fallback struct {
		ID   int
		Size int64
	}
}

type WithPoolFunc

type WithPoolFunc func(MemoryAligmentFunc) *cmallocPool

func WithPoolSize

func WithPoolSize(poolSize, bufferSize int) WithPoolFunc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL