Browse code

pkg/pool: add pools for bufio readers & writers

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)

unclejack authored on 2014/08/13 01:27:19
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,111 @@
0
+// +build go1.3
1
+
2
+// Package pools provides a collection of pools which provide various
3
+// data types with buffers. These can be used to lower the number of
4
+// memory allocations and reuse buffers.
5
+//
6
+// New pools should be added to this package to allow them to be
7
+// shared across packages.
8
+//
9
+// Utility functions which operate on pools should be added to this
10
+// package to allow them to be reused.
11
+package pools
12
+
13
+import (
14
+	"bufio"
15
+	"io"
16
+	"sync"
17
+
18
+	"github.com/docker/docker/pkg/ioutils"
19
+)
20
+
21
+var (
22
+	// Pool which returns bufio.Reader with a 32K buffer
23
+	BufioReader32KPool *BufioReaderPool
24
+	// Pool which returns bufio.Writer with a 32K buffer
25
+	BufioWriter32KPool *BufioWriterPool
26
+)
27
+
28
+const buffer32K = 32 * 1024
29
+
30
+type BufioReaderPool struct {
31
+	pool sync.Pool
32
+}
33
+
34
+func init() {
35
+	BufioReader32KPool = newBufioReaderPoolWithSize(buffer32K)
36
+	BufioWriter32KPool = newBufioWriterPoolWithSize(buffer32K)
37
+}
38
+
39
+// newBufioReaderPoolWithSize is unexported because new pools should be
40
+// added here to be shared where required.
41
+func newBufioReaderPoolWithSize(size int) *BufioReaderPool {
42
+	pool := sync.Pool{
43
+		New: func() interface{} { return bufio.NewReaderSize(nil, size) },
44
+	}
45
+	return &BufioReaderPool{pool: pool}
46
+}
47
+
48
+// Get returns a bufio.Reader which reads from r. The buffer size is that of the pool.
49
+func (bufPool *BufioReaderPool) Get(r io.Reader) *bufio.Reader {
50
+	buf := bufPool.pool.Get().(*bufio.Reader)
51
+	buf.Reset(r)
52
+	return buf
53
+}
54
+
55
+// Put puts the bufio.Reader back into the pool.
56
+func (bufPool *BufioReaderPool) Put(b *bufio.Reader) {
57
+	b.Reset(nil)
58
+	bufPool.pool.Put(b)
59
+}
60
+
61
+// NewReadCloserWrapper returns a wrapper which puts the bufio.Reader back
62
+// into the pool and closes the reader if it's an io.ReadCloser.
63
+func (bufPool *BufioReaderPool) NewReadCloserWrapper(buf *bufio.Reader, r io.Reader) io.ReadCloser {
64
+	return ioutils.NewReadCloserWrapper(r, func() error {
65
+		if readCloser, ok := r.(io.ReadCloser); ok {
66
+			readCloser.Close()
67
+		}
68
+		bufPool.Put(buf)
69
+		return nil
70
+	})
71
+}
72
+
73
+type BufioWriterPool struct {
74
+	pool sync.Pool
75
+}
76
+
77
+// newBufioWriterPoolWithSize is unexported because new pools should be
78
+// added here to be shared where required.
79
+func newBufioWriterPoolWithSize(size int) *BufioWriterPool {
80
+	pool := sync.Pool{
81
+		New: func() interface{} { return bufio.NewWriterSize(nil, size) },
82
+	}
83
+	return &BufioWriterPool{pool: pool}
84
+}
85
+
86
+// Get returns a bufio.Writer which writes to w. The buffer size is that of the pool.
87
+func (bufPool *BufioWriterPool) Get(w io.Writer) *bufio.Writer {
88
+	buf := bufPool.pool.Get().(*bufio.Writer)
89
+	buf.Reset(w)
90
+	return buf
91
+}
92
+
93
+// Put puts the bufio.Writer back into the pool.
94
+func (bufPool *BufioWriterPool) Put(b *bufio.Writer) {
95
+	b.Reset(nil)
96
+	bufPool.pool.Put(b)
97
+}
98
+
99
+// NewWriteCloserWrapper returns a wrapper which puts the bufio.Writer back
100
+// into the pool and closes the writer if it's an io.Writecloser.
101
+func (bufPool *BufioWriterPool) NewWriteCloserWrapper(buf *bufio.Writer, w io.Writer) io.WriteCloser {
102
+	return ioutils.NewWriteCloserWrapper(w, func() error {
103
+		buf.Flush()
104
+		if writeCloser, ok := w.(io.WriteCloser); ok {
105
+			writeCloser.Close()
106
+		}
107
+		bufPool.Put(buf)
108
+		return nil
109
+	})
110
+}
0 111
new file mode 100644
... ...
@@ -0,0 +1,73 @@
0
+// +build !go1.3
1
+
2
+package pools
3
+
4
+import (
5
+	"bufio"
6
+	"io"
7
+
8
+	"github.com/docker/docker/pkg/ioutils"
9
+)
10
+
11
+var (
12
+	BufioReader32KPool *BufioReaderPool
13
+	BufioWriter32KPool *BufioWriterPool
14
+)
15
+
16
+const buffer32K = 32 * 1024
17
+
18
+type BufioReaderPool struct {
19
+	size int
20
+}
21
+
22
+func init() {
23
+	BufioReader32KPool = newBufioReaderPoolWithSize(buffer32K)
24
+	BufioWriter32KPool = newBufioWriterPoolWithSize(buffer32K)
25
+}
26
+
27
+func newBufioReaderPoolWithSize(size int) *BufioReaderPool {
28
+	return &BufioReaderPool{size: size}
29
+}
30
+
31
+func (bufPool *BufioReaderPool) Get(r io.Reader) *bufio.Reader {
32
+	return bufio.NewReaderSize(r, bufPool.size)
33
+}
34
+
35
+func (bufPool *BufioReaderPool) Put(b *bufio.Reader) {
36
+	b.Reset(nil)
37
+}
38
+
39
+func (bufPool *BufioReaderPool) NewReadCloserWrapper(buf *bufio.Reader, r io.Reader) io.ReadCloser {
40
+	return ioutils.NewReadCloserWrapper(r, func() error {
41
+		if readCloser, ok := r.(io.ReadCloser); ok {
42
+			return readCloser.Close()
43
+		}
44
+		return nil
45
+	})
46
+}
47
+
48
+type BufioWriterPool struct {
49
+	size int
50
+}
51
+
52
+func newBufioWriterPoolWithSize(size int) *BufioWriterPool {
53
+	return &BufioWriterPool{size: size}
54
+}
55
+
56
+func (bufPool *BufioWriterPool) Get(w io.Writer) *bufio.Writer {
57
+	return bufio.NewWriterSize(w, bufPool.size)
58
+}
59
+
60
+func (bufPool *BufioWriterPool) Put(b *bufio.Writer) {
61
+	b.Reset(nil)
62
+}
63
+
64
+func (bufPool *BufioWriterPool) NewWriteCloserWrapper(buf *bufio.Writer, w io.Writer) io.WriteCloser {
65
+	return ioutils.NewWriteCloserWrapper(w, func() error {
66
+		buf.Flush()
67
+		if writeCloser, ok := w.(io.WriteCloser); ok {
68
+			return writeCloser.Close()
69
+		}
70
+		return nil
71
+	})
72
+}