/* pool is pool of tmp obj 临时对象的存储,每次GC都可能会回收。 */ package objectpool import ( "math/big" "sync" ) // BytePool is pool for []byte var BytePool = sync.Pool{ New: func() interface{} { b := make([]byte, 0, 1024) return &b }, } var SmallBytePool = sync.Pool{ New: func() interface{} { b := make([]byte, 0, 256) return &b }, } var LargeBytePool = sync.Pool{ New: func() interface{} { b := make([]byte, 0, 4096) return &b }, } // GetBytes wraps the pool, for clear data func GetBytes() []byte { b := BytePool.Get().(*[]byte) return (*b)[:0] } func PutBytes(b []byte) { BytePool.Put(&b) } func GetSmallBytes() []byte { b := SmallBytePool.Get().(*[]byte) return (*b)[:0] } func PutSmallBytes(b []byte) { BytePool.Put(&b) } func GetLargeBytes() []byte { b := LargeBytePool.Get().(*[]byte) return (*b)[:0] } func PutLargeBytes(b []byte) { BytePool.Put(&b) } // BigIntPool is pool for Big.Int var BigIntPool = sync.Pool{ New: func() interface{} { b := new(big.Int) return b }, } func GetBigInt() *big.Int { b := BigIntPool.Get().(*big.Int) b.SetInt64(0) return b } func PutBigInt(b *big.Int) { BigIntPool.Put(b) }