Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 // Package bufferpool implements a pool of bytes.Buffer instances backed by | |
| 6 // a sync.Pool. The goal of using a buffer pool is that, in exchange for some | |
| 7 // locking overhead, the user can avoid iteratively reallocating buffers for | |
| 8 // frequently used purposes. | |
| 9 // | |
| 10 // Ideal usage of bufferpool is with like-purposed buffers in order to encourage | |
| 11 // the pool to contain buffers sized to that specific purpose. If this is | |
| 12 // correctly implemented, the buffers in the pool should generally come into | |
| 13 // existence, grow to the purpose's size need, and then remain there without | |
| 14 // further allocation. | |
| 15 // | |
| 16 // An overly-broad-purposed pool, on the other hand, will have its buffers grow | |
| 17 // to max(purpose...) size and consequently contain more large buffers than | |
| 18 // necessary. | |
| 19 package bufferpool | |
| 20 | |
| 21 import ( | |
| 22 "bytes" | |
| 23 "sync" | |
| 24 ) | |
| 25 | |
| 26 // P is a pool of buffers. The zero value is an initialized but empty pool. | |
| 27 // | |
| 28 // P must be passed around as reference, not value. | |
| 29 type P struct { | |
| 30 pool sync.Pool | |
| 31 } | |
| 32 | |
| 33 // Get returns a Buffer. When the caller is finished with the Buffer, they | |
| 34 // should call Release to return it to its pool. | |
| 35 func (p *P) Get() *Buffer { | |
| 36 buf, ok := p.pool.Get().(*Buffer) | |
| 37 if !ok { | |
| 38 buf = &Buffer{ | |
| 39 Buffer: &bytes.Buffer{}, | |
| 40 p: p, | |
| 41 } | |
| 42 } | |
| 43 return buf | |
| 44 } | |
| 45 | |
| 46 // Buffer is a bytes.Buffer that is bound to a pool. It should not be used | |
| 47 // directly, but rather obtained through calling Get on a P instance. | |
| 48 type Buffer struct { | |
| 49 *bytes.Buffer | |
| 50 | |
| 51 p *P | |
| 52 } | |
| 53 | |
| 54 // Clone clones the contents of the buffer's Bytes, returning an indepdent | |
| 55 // duplicate []byte. | |
| 56 func (b *Buffer) Clone() []byte { | |
| 57 return append([]byte(nil), b.Bytes()...) | |
| 58 } | |
| 59 | |
| 60 // Release returns this Buffer to its pool. | |
| 61 // | |
| 62 // After calling Release, no accesses may be made to b or its internal data. | |
|
Vadim Sh.
2016/12/20 23:11:51
you can ensure this by setting b.p and b.Buffer to
Vadim Sh.
2016/12/20 23:13:51
Hm... For that, sync.Pool must hold referenced to
dnj
2016/12/21 00:54:38
Done.
| |
| 63 // If its data is to be retained, it must be cloned prior to Release (see | |
| 64 // Clone). | |
| 65 func (b *Buffer) Release() { | |
| 66 if b.p != nil { | |
| 67 b.Reset() | |
| 68 b.p.pool.Put(b) | |
| 69 } | |
| 70 } | |
| OLD | NEW |