From c783d73c8f1899cc75bfb41ae4514c597089232e Mon Sep 17 00:00:00 2001 From: OneOfOne Date: Fri, 17 Nov 2023 19:26:21 +0000 Subject: [PATCH 1/2] feat: bytes pool --- pool.go | 40 ++++++++++++++++++++++++++++++++++++++++ sync.go | 40 ---------------------------------------- 2 files changed, 40 insertions(+), 40 deletions(-) create mode 100644 pool.go delete mode 100644 sync.go diff --git a/pool.go b/pool.go new file mode 100644 index 0000000..271dd99 --- /dev/null +++ b/pool.go @@ -0,0 +1,40 @@ +package genh + +import "sync" + +type BytesPool struct { + m LMap[uint32, *sync.Pool] +} + +func (mp *BytesPool) Get(sz uint32) []byte { + if sz = sz - (sz % 1024); sz < 1024 { // round to the nearest kb + sz = 1024 + } + return *(mp.pool(sz).Get().(*[]byte)) +} + +func (mp *BytesPool) Put(b []byte) uint32 { + if cap(b) < 1024 { + return 0 + } + sz := uint32(cap(b)) + if sz = sz - (sz % 1024); sz < 1024 { // round to the nearest kb + sz = 1024 + } + b = b[:0:sz] + mp.pool(sz).Put(&b) + return sz +} + +func (mp *BytesPool) pool(sz uint32) *sync.Pool { + return mp.m.MustGet(sz, func() *sync.Pool { + p := sync.Pool{ + New: func() any { + b := make([]byte, 0, sz) + return &b + }, + } + return &p + }) + +} diff --git a/sync.go b/sync.go deleted file mode 100644 index f7a9962..0000000 --- a/sync.go +++ /dev/null @@ -1,40 +0,0 @@ -package genh - -import "sync" - -type Once[T any] struct { - v T - err error - once sync.Once -} - -func (o *Once[T]) Do(fn func() (T, error)) (v T, err error) { - o.once.Do(func() { o.v, o.err = fn() }) - return o.v, o.err -} - -type Pool[T any] struct { - New func() *T - Reset func(*T) - - p sync.Pool -} - -func (p *Pool[T]) Get() *T { - v, ok := p.p.Get().(*T) - if !ok { - if p.New != nil { - v = p.New() - } else { - v = new(T) - } - } - return v -} - -func (p *Pool[T]) Put(v *T) { - if p.Reset != nil { - p.Reset(v) - } - p.p.Put(v) -} From 72bb839550a57ce2d7bd477ece7c0f228cbc85f2 Mon Sep 17 00:00:00 2001 From: OneOfOne Date: Thu, 22 Aug 2024 16:38:25 -0500 Subject: [PATCH 2/2] feat(TimedMap): MustGet --- timed.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/timed.go b/timed.go index 52a05e7..00dc2b7 100644 --- a/timed.go +++ b/timed.go @@ -34,6 +34,21 @@ func (tm *TimedMap[K, V]) Set(k K, v V, timeout time.Duration) { tm.m.Set(k, ele) } +func (tm *TimedMap[K, V]) MustGet(k K, vfn func() V, timeout time.Duration) (out V) { + var ok bool + if out, ok = tm.GetOk(k); ok { + return + } + out = vfn() + ele := &tmEle[V]{v: out, ttl: timeout} + ele.la.Store(time.Now().UnixNano()) + if timeout > 0 { + ele.t = time.AfterFunc(timeout, func() { tm.deleteEle(k, ele) }) + } + tm.m.Set(k, ele) + return +} + func (tm *TimedMap[K, V]) SetUpdateFn(k K, vfn func() V, updateEvery time.Duration) { tm.SetUpdateExpireFn(k, vfn, updateEvery, -1) }