对于高并发场景, 频繁创建大对象, 使用sync.Pool优化, 应该有一点帮助吧
const magicNumber = 1 << 62 type Closer[T any] interface { Close() } type Object[T Closer[T]] struct { state int64 value T } func NewObject[T Closer[T]](v T) *Object[T] { return &Object[T]{value: v, state: magicNumber} } func (c *Object[T]) Value() T { return c.value } func (c *Object[T]) Add() { atomic.AddInt64(&c.state, 1) } func (c *Object[T]) Done() { if atomic.AddInt64(&c.state, -1) == 0 { c.value.Close() } } func (c *Object[T]) Release() { if atomic.AddInt64(&c.state, -1*magicNumber) == 0 { c.value.Close() } } 