Skip to content

Commit da83380

Browse files
committed
optimize: manual destroy
1 parent f9204e9 commit da83380

File tree

4 files changed

+12
-19
lines changed

4 files changed

+12
-19
lines changed

item.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ func (b *Item[T]) destroybystat(stat status) {
9797

9898
// ManualDestroy item and put it back to pool.
9999
//
100-
// Calling this method without setting pool.SetManualDestroy(true)
101-
// can probably cause panic.
100+
// Calling this method only when you're sure that
101+
// no one will use it, or it will cause a panic.
102102
func (b *Item[T]) ManualDestroy() {
103+
runtime.SetFinalizer(b, nil)
103104
b.destroybystat(status(atomic.SwapUintptr(
104105
(*uintptr)(&b.stat), uintptr(destroyedstatus),
105106
)))
@@ -110,9 +111,6 @@ func (b *Item[T]) ManualDestroy() {
110111
// Only can call once.
111112
func (b *Item[T]) setautodestroy() *Item[T] {
112113
runtime.SetFinalizer(b, func(item *Item[T]) {
113-
if item.stat.hasdestroyed() {
114-
panic("unexpected hasdestroyed")
115-
}
116114
// no one is using, no concurrency issue.
117115
item.destroybystat(item.stat)
118116
})

pbuf/buffer_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func testBuffer(buf *OBuffer, t *testing.T) {
5353
}
5454
})
5555

56+
bufcp.ManualDestroy()
57+
5658
runtime.GC()
5759
runtime.Gosched()
5860
runtime.GC()

pbuf/bytes.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,8 @@ func (b UserBytes[USRDAT]) SliceTo(to int) UserBytes[USRDAT] {
119119
func (b UserBytes[USRDAT]) Slice(from, to int) UserBytes[USRDAT] {
120120
return UserBytes[USRDAT]{buf: b.buf, a: b.a + from, b: b.a + to}
121121
}
122+
123+
// ManualDestroy please refer to Item.ManualDestroy().
124+
func (b UserBytes[USRDAT]) ManualDestroy() {
125+
b.buf.ManualDestroy()
126+
}

pool.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ type Pool[T any] struct {
1414
pooler Pooler[T]
1515
pool sync.Pool
1616
noputbak bool
17-
manudstr bool
1817
}
1918

2019
// NewPool make a new pool from custom pooler.
@@ -34,12 +33,6 @@ func (pool *Pool[T]) SetNoPutBack(on bool) {
3433
pool.noputbak = on
3534
}
3635

37-
// SetManualDestroy mark that user must manually
38-
// run Item.Destroy().
39-
func (pool *Pool[T]) SetManualDestroy(on bool) {
40-
pool.manudstr = on
41-
}
42-
4336
func (pool *Pool[T]) incin() {
4437
atomic.AddInt32(&pool.countin, 1)
4538
}
@@ -63,16 +56,11 @@ func (pool *Pool[T]) newempty() *Item[T] {
6356
}
6457
item.stat = status(0)
6558
pool.incout()
66-
if !pool.manudstr {
67-
item.setautodestroy()
68-
}
69-
return item
59+
return item.setautodestroy()
7060
}
7161

7262
func (pool *Pool[T]) put(item *Item[T]) {
73-
if !pool.manudstr {
74-
runtime.SetFinalizer(item, nil)
75-
}
63+
runtime.SetFinalizer(item, nil)
7664

7765
item.cfg = nil
7866

0 commit comments

Comments
 (0)