manual revert a9bb9bb5 (reuse pool slice)
culprit:
https://github.com/vbauerster/mpb/blob/314158faaf7557a31a22ab69bd491799d46f2e3f/progress.go#L393
zero bars in stash pool doesn't mean zero bars on screen.
address issue #122
Vladimir Bauer
3 years ago
| 38 | 38 | heapUpdated bool |
| 39 | 39 | pMatrix map[int][]chan int |
| 40 | 40 | aMatrix map[int][]chan int |
| 41 | ||
| 42 | // for reuse purposes | |
| 43 | rows []io.Reader | |
| 44 | pool []*Bar | |
| 45 | 41 | |
| 46 | 42 | // following are provided/overrided by user |
| 47 | 43 | refreshRate time.Duration |
| 71 | 67 | // method has been called. |
| 72 | 68 | func NewWithContext(ctx context.Context, options ...ContainerOption) *Progress { |
| 73 | 69 | s := &pState{ |
| 74 | rows: make([]io.Reader, 0, 64), | |
| 75 | pool: make([]*Bar, 0, 64), | |
| 76 | 70 | refreshRate: defaultRefreshRate, |
| 77 | 71 | popPriority: math.MinInt32, |
| 78 | 72 | manualRefresh: make(chan interface{}), |
| 274 | 268 | defer close(p.shutdown) |
| 275 | 269 | |
| 276 | 270 | render := func() error { |
| 277 | if s.bHeap.Len() == 0 { | |
| 278 | return nil | |
| 279 | } | |
| 280 | 271 | return s.render(cw) |
| 281 | 272 | } |
| 282 | 273 | |
| 335 | 326 | |
| 336 | 327 | func (s *pState) flush(wg *sync.WaitGroup, cw *cwriter.Writer, height int) error { |
| 337 | 328 | var popCount int |
| 329 | rows := make([]io.Reader, 0, height) | |
| 330 | pool := make([]*Bar, 0, s.bHeap.Len()) | |
| 338 | 331 | |
| 339 | 332 | for s.bHeap.Len() > 0 { |
| 340 | 333 | b := heap.Pop(&s.bHeap).(*Bar) |
| 345 | 338 | } |
| 346 | 339 | var usedRows int |
| 347 | 340 | for i := len(frame.rows) - 1; i >= 0; i-- { |
| 348 | if row := frame.rows[i]; len(s.rows) < height { | |
| 349 | s.rows = append(s.rows, row) | |
| 341 | if row := frame.rows[i]; len(rows) < height { | |
| 342 | rows = append(rows, row) | |
| 350 | 343 | usedRows++ |
| 351 | 344 | } else { |
| 352 | 345 | wg.Add(1) |
| 361 | 354 | if qb, ok := s.queueBars[b]; ok { |
| 362 | 355 | delete(s.queueBars, b) |
| 363 | 356 | qb.priority = b.priority |
| 364 | s.pool = append(s.pool, qb) | |
| 357 | pool = append(pool, qb) | |
| 365 | 358 | s.heapUpdated = true |
| 366 | 359 | continue |
| 367 | 360 | } |
| 382 | 375 | continue |
| 383 | 376 | } |
| 384 | 377 | } |
| 385 | s.pool = append(s.pool, b) | |
| 386 | } | |
| 387 | ||
| 388 | switch len(s.pool) { | |
| 389 | case 0: | |
| 390 | if s.heapUpdated { | |
| 391 | s.updateSyncMatrix() | |
| 392 | s.heapUpdated = false | |
| 393 | } | |
| 378 | pool = append(pool, b) | |
| 379 | } | |
| 380 | ||
| 381 | switch len(pool) { | |
| 394 | 382 | case 1: |
| 395 | heap.Push(&s.bHeap, s.pool[0]) | |
| 396 | s.pool = s.pool[:0] | |
| 383 | heap.Push(&s.bHeap, pool[0]) | |
| 397 | 384 | default: |
| 398 | 385 | wg.Add(1) |
| 399 | 386 | go func() { |
| 400 | for _, b := range s.pool { | |
| 387 | for _, b := range pool { | |
| 401 | 388 | heap.Push(&s.bHeap, b) |
| 402 | 389 | } |
| 403 | s.pool = s.pool[:0] | |
| 404 | 390 | wg.Done() |
| 405 | 391 | }() |
| 406 | 392 | } |
| 407 | 393 | |
| 408 | for i := len(s.rows) - 1; i >= 0; i-- { | |
| 409 | _, err := cw.ReadFrom(s.rows[i]) | |
| 394 | for i := len(rows) - 1; i >= 0; i-- { | |
| 395 | _, err := cw.ReadFrom(rows[i]) | |
| 410 | 396 | if err != nil { |
| 411 | 397 | return err |
| 412 | 398 | } |
| 413 | 399 | } |
| 414 | 400 | |
| 415 | err := cw.Flush(len(s.rows) - popCount) | |
| 416 | s.rows = s.rows[:0] | |
| 401 | err := cw.Flush(len(rows) - popCount) | |
| 417 | 402 | return err |
| 418 | 403 | } |
| 419 | 404 | |