gobwas / pool

Go Pooling Helpers
MIT License
113 stars 18 forks source link

Large memory allocation on pools.Put #7

Open ehsannm opened 4 years ago

ehsannm commented 4 years ago

Hi,

I was benchmarking my lib which is using of gobwas/pbytes package extensively, however, during my benchamrks i found that there is a large amount of allocation happens in pool.Put() function. I checked i didn't find anything. That is why i have asking it here maybe you could help me to find out.

github.com/gobwas/pool/pbytes.(*Pool).Put
/Users/ehsan/Dev/ronak/go/rony/vendor/github.com/gobwas/pool/pbytes/pool.go

  Total:    237.51MB   237.51MB (flat, cum) 92.18%
     42            .          .            
     43            .          .           // Put returns given slice to reuse pool. 
     44            .          .           // It does not reuse bytes whose size is not power of two or is out of pool 
     45            .          .           // min/max range. 
     46            .          .           func (p *Pool) Put(bts []byte) { 
     47     237.51MB   237.51MB             p.pool.Put(bts, cap(bts)) 
     48            .          .           } 
     49            .          .            
     50            .          .           // GetCap returns probably reused slice of bytes with at least capacity of n. 
     51            .          .           func (p *Pool) GetCap(c int) []byte { 
     52            .          .             return p.Get(0, c) 
github.com/gobwas/pool/pbytes.(*Pool).Get
/Users/ehsan/Dev/ronak/go/rony/vendor/github.com/gobwas/pool/pbytes/pool.go

  Total:      9.50MB     9.50MB (flat, cum)  3.69%
     35            .          .                 bts := v.([]byte) 
     36            .          .                 bts = bts[:n] 
     37            .          .                 return bts 
     38            .          .             } 
     39            .          .            
     40       9.50MB     9.50MB             return make([]byte, n, x) 
     41            .          .           } 
     42            .          .            
     43            .          .           // Put returns given slice to reuse pool. 
     44            .          .           // It does not reuse bytes whose size is not power of two or is out of pool 
     45            .          .           // min/max range. 
gobwas commented 4 years ago

Hi @ehsannm,

Yes, this is a known issue when using []byte instead of *[]byte within the sync.Pool. I think I can fix this soon, thanks!

ehsannm commented 4 years ago

thanks @gobwas, I am looking forward to the fix. Also I am happy to help if you agreed