jolestar / go-commons-pool

a generic object pool for golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Plan to support for slices' reuse ?

ruinanchen opened this issue · comments

Hey, superman! Has a plan to support for slices' reuse.I think slice is different from struct.

@ruinanchen how about this example?

func TestSliceExample(t *testing.T) {
pool := NewObjectPoolWithDefaultConfig(NewPooledObjectFactorySimple(
    func() (interface{}, error) {
        slice := make([]int,10)
        return slice, nil
    }))
obj, _ := pool.BorrowObject()
slice,ok := obj.([]int)
assert.True(t, ok)
slice[0] = 0
pool.ReturnObject(obj)
}

Thanks for your reply!

I think the example is too simple. Slice is a extensible type.
As far as I know, slice's append generates a new slice if cap is not sufficient.
So i think should classify slices according to theirs capacity. We borrow a slice with parameter that an expected capacity ,then ObjectPool returns a slice has a no less capacity and most nearest. In this way, may avoid a new slice's generating in most case.

Please forgive my poor English.

Borrow by capacity will broken the pool api, and in special case, slice cap is fixed or has max cap limit, so i think just use max cap limit to create fix cap slice is a simple way.

Yeah, you are right. If no consider of precision of memory usage, create fixed cap slice is a good way.