Debian Codegraph @

leveldb/util/buffer_pool.go

raw symbols SCIP
1 // Copyright (c) 2014, Suryandaru Triandana <[email protected]>
2 // All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
6
7 package utilscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/
8
9 import (
10 "fmtscip-go gomod github.com/golang/go/src go1.14 fmt/"
11 "syncscip-go gomod github.com/golang/go/src go1.14 sync/"
12 "sync/atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/"
13 "timescip-go gomod github.com/golang/go/src go1.14 time/"
14 )
15
16 type bufferscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/buffer# struct {
17 bscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/buffer#b. []byte
18 missscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/buffer#miss. int
19 }
20
21 // BufferPool is a 'buffer pool'.
22 type BufferPoolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool# struct {
23 poolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#pool. [6]chan []byte
24 sizescip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#size. [5]uint32
25 sizeMissscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#sizeMiss. [5]uint32
26 sizeHalfscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#sizeHalf. [5]uint32
27 baselinescip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#baseline. [4]int
28 baseline0scip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#baseline0. int
29
30 muscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#mu. syncscip-go gomod github.com/golang/go/src go1.14 sync/.RWMutexscip-go gomod github.com/golang/go/src go1.14 sync/RWMutex#
31 closedscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#closed. bool
32 closeCscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#closeC. chan struct{}
33
34 getscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#get. uint32
35 putscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#put. uint32
36 halfscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#half. uint32
37 lessscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#less. uint32
38 equalscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#equal. uint32
39 greaterscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#greater. uint32
40 missscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#miss. uint32
41 }
42
43 func (p *BufferPoolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#) poolNumscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#poolNum().(n int) int {
44 if n <= p.baseline0scip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#baseline0. && n > p.baseline0scip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#baseline0./2 {
45 return 0
46 }
47 for i, x := range p.baselinescip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#baseline. {
48 if n <= x {
49 return i + 1
50 }
51 }
52 return len(p.baselinescip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#baseline.) + 1
53 }
54
55 // Get returns buffer with length of n.
56 func (p *BufferPoolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#) Getscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#Get().(n int) []byte {
57 if p == nil {
58 return make([]byte, n)
59 }
60
61 p.muscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#mu..RLockscip-go gomod github.com/golang/go/src go1.14 sync/RWMutex#RLock().()
62 defer p.muscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#mu..RUnlockscip-go gomod github.com/golang/go/src go1.14 sync/RWMutex#RUnlock().()
63
64 if p.closedscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#closed. {
65 return make([]byte, n)
66 }
67
68 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(&p.getscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#get., 1)
69
70 poolNum := p.poolNumscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#poolNum().(n)
71 pool := p.poolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#pool.[poolNum]
72 if poolNum == 0 {
73 // Fast path.
74 select {
75 case b := <-pool:
76 switch {
77 case cap(b) > n:
78 if cap(b)-n >= n {
79 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(&p.halfscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#half., 1)
80 select {
81 case pool <- b:
82 default:
83 }
84 return make([]byte, n)
85 } else {
86 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(&p.lessscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#less., 1)
87 return b[:n]
88 }
89 case cap(b) == n:
90 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(&p.equalscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#equal., 1)
91 return b[:n]
92 default:
93 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(&p.greaterscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#greater., 1)
94 }
95 default:
96 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(&p.missscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#miss., 1)
97 }
98
99 return make([]byte, n, p.baseline0scip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#baseline0.)
100 } else {
101 sizePtr := &p.sizescip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#size.[poolNum-1]
102
103 select {
104 case b := <-pool:
105 switch {
106 case cap(b) > n:
107 if cap(b)-n >= n {
108 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(&p.halfscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#half., 1)
109 sizeHalfPtr := &p.sizeHalfscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#sizeHalf.[poolNum-1]
110 if atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(sizeHalfPtr, 1) == 20 {
111 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.StoreUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/StoreUint32().(sizePtr, uint32(cap(b)/2))
112 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.StoreUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/StoreUint32().(sizeHalfPtr, 0)
113 } else {
114 select {
115 case pool <- b:
116 default:
117 }
118 }
119 return make([]byte, n)
120 } else {
121 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(&p.lessscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#less., 1)
122 return b[:n]
123 }
124 case cap(b) == n:
125 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(&p.equalscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#equal., 1)
126 return b[:n]
127 default:
128 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(&p.greaterscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#greater., 1)
129 if uint32(cap(b)) >= atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.LoadUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/LoadUint32().(sizePtr) {
130 select {
131 case pool <- b:
132 default:
133 }
134 }
135 }
136 default:
137 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(&p.missscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#miss., 1)
138 }
139
140 if size := atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.LoadUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/LoadUint32().(sizePtr); uint32(n) > size {
141 if size == 0 {
142 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.CompareAndSwapUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/CompareAndSwapUint32().(sizePtr, 0, uint32(n))
143 } else {
144 sizeMissPtr := &p.sizeMissscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#sizeMiss.[poolNum-1]
145 if atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(sizeMissPtr, 1) == 20 {
146 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.StoreUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/StoreUint32().(sizePtr, uint32(n))
147 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.StoreUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/StoreUint32().(sizeMissPtr, 0)
148 }
149 }
150 return make([]byte, n)
151 } else {
152 return make([]byte, n, size)
153 }
154 }
155 }
156
157 // Put adds given buffer to the pool.
158 func (p *BufferPoolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#) Putscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#Put().(b []byte) {
159 if p == nil {
160 return
161 }
162
163 p.muscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#mu..RLockscip-go gomod github.com/golang/go/src go1.14 sync/RWMutex#RLock().()
164 defer p.muscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#mu..RUnlockscip-go gomod github.com/golang/go/src go1.14 sync/RWMutex#RUnlock().()
165
166 if p.closedscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#closed. {
167 return
168 }
169
170 atomicscip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/.AddUint32scip-go gomod github.com/golang/go/src go1.14 `sync/atomic`/AddUint32().(&p.putscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#put., 1)
171
172 pool := p.poolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#pool.[p.poolNumscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#poolNum().(cap(b))]
173 select {
174 case pool <- b:
175 default:
176 }
177
178 }
179
180 func (p *BufferPoolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#) Closescip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#Close().() {
181 if p == nil {
182 return
183 }
184
185 p.muscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#mu..Lockscip-go gomod github.com/golang/go/src go1.14 sync/RWMutex#Lock().()
186 if !p.closedscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#closed. {
187 p.closedscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#closed. = true
188 p.closeCscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#closeC. <- struct{}{}
189 }
190 p.muscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#mu..Unlockscip-go gomod github.com/golang/go/src go1.14 sync/RWMutex#Unlock().()
191 }
192
193 func (p *BufferPoolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#) Stringscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#String().() string {
194 if p == nil {
195 return "<nil>"
196 }
197
198 p.muscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#mu..Lockscip-go gomod github.com/golang/go/src go1.14 sync/RWMutex#Lock().()
199 defer p.muscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#mu..Unlockscip-go gomod github.com/golang/go/src go1.14 sync/RWMutex#Unlock().()
200
201 return fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.14 fmt/Sprintf().("BufferPool{B·%d Z·%v Zm·%v Zh·%v G·%d P·%d H·%d <·%d =·%d >·%d M·%d}",
202 p.baseline0scip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#baseline0., p.sizescip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#size., p.sizeMissscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#sizeMiss., p.sizeHalfscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#sizeHalf., p.getscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#get., p.putscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#put., p.halfscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#half., p.lessscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#less., p.equalscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#equal., p.greaterscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#greater., p.missscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#miss.)
203 }
204
205 func (p *BufferPoolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#) drainscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#drain().() {
206 ticker := timescip-go gomod github.com/golang/go/src go1.14 time/.NewTickerscip-go gomod github.com/golang/go/src go1.14 time/NewTicker().(2 * timescip-go gomod github.com/golang/go/src go1.14 time/.Secondscip-go gomod github.com/golang/go/src go1.14 time/Second.)
207 defer ticker.Stopscip-go gomod github.com/golang/go/src go1.14 time/Ticker#Stop().()
208 for {
209 select {
210 case <-ticker.Cscip-go gomod github.com/golang/go/src go1.14 time/Ticker#C.:
211 for _, ch := range p.poolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#pool. {
212 select {
213 case <-ch:
214 default:
215 }
216 }
217 case <-p.closeCscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#closeC.:
218 close(p.closeCscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#closeC.)
219 for _, ch := range p.poolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#pool. {
220 close(ch)
221 }
222 return
223 }
224 }
225 }
226
227 // NewBufferPool creates a new initialized 'buffer pool'.
228 func NewBufferPoolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/NewBufferPool().(baseline int) *BufferPoolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool# {
229 if baseline <= 0 {
230 panic("baseline can't be <= 0")
231 }
232 p := &BufferPoolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#{
233 baseline0scip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#baseline0.: baseline,
234 baselinescip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#baseline.: [...]int{baseline / 4, baseline / 2, baseline * 2, baseline * 4},
235 closeCscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#closeC.: make(chan struct{}, 1),
236 }
237 for i, cap := range []int{2, 2, 4, 4, 2, 1} {
238 p.poolscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#pool.[i] = make(chan []byte, cap)
239 }
240 go p.drainscip-go gomod github.com/syndtr/goleveldb . `github.com/syndtr/goleveldb/leveldb/util`/BufferPool#drain().()
241 return p
242 }