Put adds a slice to the right bucket in the pool.
(s []byte)
| 78 | |
| 79 | // Put adds a slice to the right bucket in the pool. |
| 80 | func (p *Pool) Put(s []byte) int { |
| 81 | c := cap(s) |
| 82 | |
| 83 | // valid slice? |
| 84 | if (c-p.minBucket)%p.bktSize != 0 { |
| 85 | return -1 |
| 86 | } |
| 87 | bkt := p.bucketFor(c) // -1 puts the slice in the pool below. it will be larger than all requested slices for this bucket |
| 88 | if bkt < 0 { |
| 89 | return -1 |
| 90 | } |
| 91 | if bkt >= len(p.buckets) { |
| 92 | return -1 |
| 93 | } |
| 94 | |
| 95 | // reset length for safety |
| 96 | p.buckets[bkt].Put(s[:0]) // nolint: staticcheck |
| 97 | |
| 98 | return bkt // for testing |
| 99 | } |
| 100 | |
| 101 | func (p *Pool) bucketFor(sz int) int { |
| 102 | if sz <= p.minBucket { |