Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: common/sync/bufferpool/buffer_pool.go

Issue 2596633002: Add bufferpool, a sync.Pool-backed pool of buffers (Closed)
Patch Set: Wrap each Buffer on Get. Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | common/sync/bufferpool/buffer_pool_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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().(*bytes.Buffer)
37 if !ok {
38 buf = &bytes.Buffer{}
39 }
40
41 return &Buffer{
42 Buffer: buf,
43 p: p,
44 }
45 }
46
47 // Buffer is a bytes.Buffer that is bound to a pool. It should not be used
48 // directly, but rather obtained through calling Get on a P instance.
49 type Buffer struct {
50 *bytes.Buffer
51
52 p *P
53 }
54
55 // Clone clones the contents of the buffer's Bytes, returning an indepdent
56 // duplicate []byte.
57 func (b *Buffer) Clone() []byte {
58 return append([]byte(nil), b.Bytes()...)
59 }
60
61 // Release returns this Buffer to its pool.
62 //
63 // After calling Release, no accesses may be made to b or its internal data.
64 // If its data is to be retained, it must be cloned prior to Release (see
65 // Clone).
66 func (b *Buffer) Release() {
67 p := b.p
68 if p == nil {
69 panic("double Release")
70 }
71
72 b.p = nil
73 b.Reset()
74 p.pool.Put(b.Buffer)
75 }
OLDNEW
« no previous file with comments | « no previous file | common/sync/bufferpool/buffer_pool_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698