Codebase list golang-github-vbauerster-mpb / a9bb9bb
reuse pool slice Vladimir Bauer 3 years ago
1 changed file(s) with 18 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
3939 heapUpdated bool
4040 pMatrix map[int][]chan int
4141 aMatrix map[int][]chan int
42 rows []io.Reader
42
43 // for reuse purposes
44 rows []io.Reader
45 pool []*Bar
4346
4447 // following are provided/overrided by user
4548 idCount int
6972 func NewWithContext(ctx context.Context, options ...ContainerOption) *Progress {
7073 s := &pState{
7174 bHeap: priorityQueue{},
72 rows: make([]io.Reader, 128),
75 rows: make([]io.Reader, 0, 128),
76 pool: make([]*Bar, 0, 128),
7377 rr: prr,
7478 queueBars: make(map[*Bar]*Bar),
7579 output: os.Stdout,
287291 func (s *pState) flush(cw *cwriter.Writer, height int) error {
288292 var wg sync.WaitGroup
289293 var popCount int
290 rows := s.rows[:0]
291 pool := make([]*Bar, 0, s.bHeap.Len())
294
292295 for s.bHeap.Len() > 0 {
293296 var usedRows int
294297 b := heap.Pop(&s.bHeap).(*Bar)
298301 continue
299302 }
300303 for i := len(frame.rows) - 1; i >= 0; i-- {
301 if row := frame.rows[i]; len(rows) < height {
302 rows = append(rows, row)
304 if row := frame.rows[i]; len(s.rows) < height {
305 s.rows = append(s.rows, row)
303306 usedRows++
304307 } else {
305308 wg.Add(1)
315318 if qb, ok := s.queueBars[b]; ok {
316319 delete(s.queueBars, b)
317320 qb.priority = b.priority
318 pool = append(pool, qb)
321 s.pool = append(s.pool, qb)
319322 drop = true
320323 } else if s.popCompleted && !b.bs.noPop {
321324 if frame.shutdown > 1 {
331334 continue
332335 }
333336 }
334 pool = append(pool, b)
337 s.pool = append(s.pool, b)
335338 }
336339
337340 for _, b := range pool {
338341 heap.Push(&s.bHeap, b)
339342 }
340343
341 for i := len(rows) - 1; i >= 0; i-- {
342 _, err := cw.ReadFrom(rows[i])
344 readRows := len(s.rows)
345 for i := readRows - 1; i >= 0; i-- {
346 _, err := cw.ReadFrom(s.rows[i])
343347 if err != nil {
344 wg.Wait()
345 return err
348 readRows--
346349 }
347350 }
348351
349352 wg.Wait()
350 return cw.Flush(len(rows) - popCount)
353 s.rows = s.rows[:0]
354 s.pool = s.pool[:0]
355 return cw.Flush(readRows - popCount)
351356 }
352357
353358 func (s *pState) newTicker(done <-chan struct{}) chan time.Time {