Codebase list golang-github-vbauerster-mpb / bf2bcc3
Improved perf of bar.Completed method Vladimir Bauer 7 years ago
1 changed file(s) with 14 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
2020 rRight
2121 )
2222
23 const (
24 cmdId = 1 << iota
25 cmdCurrent
26 cmdCompleted
27 )
28
2923 const formatLen = 5
3024
3125 type barRunes [formatLen]rune
3832 runningBar *Bar
3933 cacheState *bState
4034 operateState chan func(*bState)
41 cmdValue chan int
35 int64Ch chan int64
36 boolCh chan bool
4237 frameReaderCh chan io.Reader
4338 syncTableCh chan [][]chan int
4439 bufNL *bytes.Buffer
111106 priority: s.priority,
112107 runningBar: s.runningBar,
113108 operateState: make(chan func(*bState)),
114 cmdValue: make(chan int),
109 int64Ch: make(chan int64),
110 boolCh: make(chan bool),
115111 frameReaderCh: make(chan io.Reader, 1),
116112 syncTableCh: make(chan [][]chan int),
117113 done: make(chan struct{}),
158154 // ID returs id of the bar.
159155 func (b *Bar) ID() int {
160156 select {
161 case b.cmdValue <- cmdId:
162 return <-b.cmdValue
157 case b.operateState <- func(s *bState) { b.int64Ch <- int64(s.id) }:
158 return int(<-b.int64Ch)
163159 case <-b.done:
164160 return b.cacheState.id
165161 }
168164 // Current returns bar's current number, in other words sum of all increments.
169165 func (b *Bar) Current() int64 {
170166 select {
171 case b.cmdValue <- cmdCurrent:
172 return int64(<-b.cmdValue)
167 case b.operateState <- func(s *bState) { b.int64Ch <- s.current }:
168 return <-b.int64Ch
173169 case <-b.done:
174170 return b.cacheState.current
175171 }
223219
224220 // Completed reports whether the bar is in completed state.
225221 func (b *Bar) Completed() bool {
226 b.cmdValue <- cmdCompleted
227 if v := <-b.cmdValue; v == 1 {
228 return true
229 }
230 return false
222 // omit select here, because primary usage of the method is for loop
223 // condition, like for !bar.Completed() {...}
224 // so when toComplete=true it is called once (at which time, the bar is still alive),
225 // then quits the loop and never suppose to be called afterwards.
226 return <-b.boolCh
231227 }
232228
233229 func (b *Bar) wSyncTable() [][]chan int {
245241 select {
246242 case op := <-b.operateState:
247243 op(s)
248 case cmd := <-b.cmdValue:
249 switch {
250 case (cmd & cmdId) != 0:
251 b.cmdValue <- s.id
252 case (cmd & cmdCurrent) != 0:
253 b.cmdValue <- int(s.current)
254 case (cmd & cmdCompleted) != 0:
255 var v int
256 if s.toComplete {
257 v = 1
258 }
259 b.cmdValue <- v
260 }
244 case b.boolCh <- s.toComplete:
261245 case <-cancel:
262246 s.toComplete = true
263247 cancel = nil