Codebase list golang-github-vbauerster-mpb / 78ba291
reduce alloc for: Completed, Total, Current, ID, NumOfAppenders, NumOfPrependers Vladimir Bauer 7 years ago
1 changed file(s) with 51 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
2424 formatLen = 5
2525 )
2626
27 const (
28 cmdId = 1 << iota
29 cmdTotal
30 cmdCurrent
31 cmdALen
32 cmdPLen
33 cmdCompleted
34 )
35
2736 type barRunes [formatLen]rune
2837
2938 // Bar represents a progress Bar
3443 runningBar *Bar
3544 cacheState *bState
3645 operateState chan func(*bState)
46 cmdValue chan int
3747 frameReaderCh chan io.Reader
3848
3949 // done is closed by Bar's goroutine, after cacheState is written
4656 bState struct {
4757 id int
4858 width int
49 runes barRunes
5059 total int64
5160 current int64
5261 totalAutoIncrTrigger int64
5362 totalAutoIncrBy int64
63 runes barRunes
5464 trimLeftSpace bool
5565 trimRightSpace bool
5666 toComplete bool
108118 priority: s.priority,
109119 runningBar: s.runningBar,
110120 operateState: make(chan func(*bState)),
121 cmdValue: make(chan int),
111122 frameReaderCh: make(chan io.Reader, 1),
112123 done: make(chan struct{}),
113124 shutdown: make(chan struct{}),
148159
149160 // NumOfAppenders returns current number of append decorators.
150161 func (b *Bar) NumOfAppenders() int {
151 result := make(chan int)
152 select {
153 case b.operateState <- func(s *bState) { result <- len(s.aDecorators) }:
154 return <-result
162 select {
163 case b.cmdValue <- cmdALen:
164 return <-b.cmdValue
155165 case <-b.done:
156166 return len(b.cacheState.aDecorators)
157167 }
159169
160170 // NumOfPrependers returns current number of prepend decorators.
161171 func (b *Bar) NumOfPrependers() int {
162 result := make(chan int)
163 select {
164 case b.operateState <- func(s *bState) { result <- len(s.pDecorators) }:
165 return <-result
172 select {
173 case b.cmdValue <- cmdPLen:
174 return <-b.cmdValue
166175 case <-b.done:
167176 return len(b.cacheState.pDecorators)
168177 }
170179
171180 // ID returs id of the bar.
172181 func (b *Bar) ID() int {
173 result := make(chan int)
174 select {
175 case b.operateState <- func(s *bState) { result <- s.id }:
176 return <-result
182 select {
183 case b.cmdValue <- cmdId:
184 return <-b.cmdValue
177185 case <-b.done:
178186 return b.cacheState.id
179187 }
181189
182190 // Current returns bar's current number, in other words sum of all increments.
183191 func (b *Bar) Current() int64 {
184 result := make(chan int64)
185 select {
186 case b.operateState <- func(s *bState) { result <- s.current }:
187 return <-result
192 select {
193 case b.cmdValue <- cmdCurrent:
194 return int64(<-b.cmdValue)
188195 case <-b.done:
189196 return b.cacheState.current
190197 }
192199
193200 // Total returns bar's total number.
194201 func (b *Bar) Total() int64 {
195 result := make(chan int64)
196 select {
197 case b.operateState <- func(s *bState) { result <- s.total }:
198 return <-result
202 select {
203 case b.cmdValue <- cmdTotal:
204 return int64(<-b.cmdValue)
199205 case <-b.done:
200206 return b.cacheState.total
201207 }
252258
253259 // Completed reports whether the bar is in completed state.
254260 func (b *Bar) Completed() bool {
255 result := make(chan bool)
256 b.operateState <- func(s *bState) { result <- s.toComplete }
257 return <-result
261 b.cmdValue <- cmdCompleted
262 if v := <-b.cmdValue; v == 1 {
263 return true
264 }
265 return false
258266 }
259267
260268 func (b *Bar) serve(wg *sync.WaitGroup, s *bState, cancel <-chan struct{}) {
263271 select {
264272 case op := <-b.operateState:
265273 op(s)
274 case cmd := <-b.cmdValue:
275 switch {
276 case cmd&cmdId != 0:
277 b.cmdValue <- s.id
278 case cmd&cmdTotal != 0:
279 b.cmdValue <- int(s.total)
280 case cmd&cmdCurrent != 0:
281 b.cmdValue <- int(s.current)
282 case cmd&cmdPLen != 0:
283 b.cmdValue <- len(s.pDecorators)
284 case cmd&cmdALen != 0:
285 b.cmdValue <- len(s.aDecorators)
286 case cmd&cmdCompleted != 0:
287 var v int
288 if s.toComplete {
289 v = 1
290 }
291 b.cmdValue <- v
292 }
266293 case <-cancel:
267294 s.toComplete = true
268295 cancel = nil