mirror of
https://github.com/fumiama/orbyte.git
synced 2026-06-10 21:24:50 +08:00
optimize: manual destroy
This commit is contained in:
8
item.go
8
item.go
@@ -97,9 +97,10 @@ func (b *Item[T]) destroybystat(stat status) {
|
|||||||
|
|
||||||
// ManualDestroy item and put it back to pool.
|
// ManualDestroy item and put it back to pool.
|
||||||
//
|
//
|
||||||
// Calling this method without setting pool.SetManualDestroy(true)
|
// Calling this method only when you're sure that
|
||||||
// can probably cause panic.
|
// no one will use it, or it will cause a panic.
|
||||||
func (b *Item[T]) ManualDestroy() {
|
func (b *Item[T]) ManualDestroy() {
|
||||||
|
runtime.SetFinalizer(b, nil)
|
||||||
b.destroybystat(status(atomic.SwapUintptr(
|
b.destroybystat(status(atomic.SwapUintptr(
|
||||||
(*uintptr)(&b.stat), uintptr(destroyedstatus),
|
(*uintptr)(&b.stat), uintptr(destroyedstatus),
|
||||||
)))
|
)))
|
||||||
@@ -110,9 +111,6 @@ func (b *Item[T]) ManualDestroy() {
|
|||||||
// Only can call once.
|
// Only can call once.
|
||||||
func (b *Item[T]) setautodestroy() *Item[T] {
|
func (b *Item[T]) setautodestroy() *Item[T] {
|
||||||
runtime.SetFinalizer(b, func(item *Item[T]) {
|
runtime.SetFinalizer(b, func(item *Item[T]) {
|
||||||
if item.stat.hasdestroyed() {
|
|
||||||
panic("unexpected hasdestroyed")
|
|
||||||
}
|
|
||||||
// no one is using, no concurrency issue.
|
// no one is using, no concurrency issue.
|
||||||
item.destroybystat(item.stat)
|
item.destroybystat(item.stat)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -53,6 +53,8 @@ func testBuffer(buf *OBuffer, t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
bufcp.ManualDestroy()
|
||||||
|
|
||||||
runtime.GC()
|
runtime.GC()
|
||||||
runtime.Gosched()
|
runtime.Gosched()
|
||||||
runtime.GC()
|
runtime.GC()
|
||||||
|
|||||||
@@ -119,3 +119,8 @@ func (b UserBytes[USRDAT]) SliceTo(to int) UserBytes[USRDAT] {
|
|||||||
func (b UserBytes[USRDAT]) Slice(from, to int) UserBytes[USRDAT] {
|
func (b UserBytes[USRDAT]) Slice(from, to int) UserBytes[USRDAT] {
|
||||||
return UserBytes[USRDAT]{buf: b.buf, a: b.a + from, b: b.a + to}
|
return UserBytes[USRDAT]{buf: b.buf, a: b.a + from, b: b.a + to}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ManualDestroy please refer to Item.ManualDestroy().
|
||||||
|
func (b UserBytes[USRDAT]) ManualDestroy() {
|
||||||
|
b.buf.ManualDestroy()
|
||||||
|
}
|
||||||
|
|||||||
16
pool.go
16
pool.go
@@ -14,7 +14,6 @@ type Pool[T any] struct {
|
|||||||
pooler Pooler[T]
|
pooler Pooler[T]
|
||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
noputbak bool
|
noputbak bool
|
||||||
manudstr bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPool make a new pool from custom pooler.
|
// NewPool make a new pool from custom pooler.
|
||||||
@@ -34,12 +33,6 @@ func (pool *Pool[T]) SetNoPutBack(on bool) {
|
|||||||
pool.noputbak = on
|
pool.noputbak = on
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetManualDestroy mark that user must manually
|
|
||||||
// run Item.Destroy().
|
|
||||||
func (pool *Pool[T]) SetManualDestroy(on bool) {
|
|
||||||
pool.manudstr = on
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pool *Pool[T]) incin() {
|
func (pool *Pool[T]) incin() {
|
||||||
atomic.AddInt32(&pool.countin, 1)
|
atomic.AddInt32(&pool.countin, 1)
|
||||||
}
|
}
|
||||||
@@ -63,16 +56,11 @@ func (pool *Pool[T]) newempty() *Item[T] {
|
|||||||
}
|
}
|
||||||
item.stat = status(0)
|
item.stat = status(0)
|
||||||
pool.incout()
|
pool.incout()
|
||||||
if !pool.manudstr {
|
return item.setautodestroy()
|
||||||
item.setautodestroy()
|
|
||||||
}
|
|
||||||
return item
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pool *Pool[T]) put(item *Item[T]) {
|
func (pool *Pool[T]) put(item *Item[T]) {
|
||||||
if !pool.manudstr {
|
runtime.SetFinalizer(item, nil)
|
||||||
runtime.SetFinalizer(item, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
item.cfg = nil
|
item.cfg = nil
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user