reuse pool slice
Vladimir Bauer
3 years ago
| 39 | 39 | heapUpdated bool |
| 40 | 40 | pMatrix map[int][]chan int |
| 41 | 41 | aMatrix map[int][]chan int |
| 42 | rows []io.Reader | |
| 42 | ||
| 43 | // for reuse purposes | |
| 44 | rows []io.Reader | |
| 45 | pool []*Bar | |
| 43 | 46 | |
| 44 | 47 | // following are provided/overrided by user |
| 45 | 48 | idCount int |
| 69 | 72 | func NewWithContext(ctx context.Context, options ...ContainerOption) *Progress { |
| 70 | 73 | s := &pState{ |
| 71 | 74 | bHeap: priorityQueue{}, |
| 72 | rows: make([]io.Reader, 128), | |
| 75 | rows: make([]io.Reader, 0, 128), | |
| 76 | pool: make([]*Bar, 0, 128), | |
| 73 | 77 | rr: prr, |
| 74 | 78 | queueBars: make(map[*Bar]*Bar), |
| 75 | 79 | output: os.Stdout, |
| 287 | 291 | func (s *pState) flush(cw *cwriter.Writer, height int) error { |
| 288 | 292 | var wg sync.WaitGroup |
| 289 | 293 | var popCount int |
| 290 | rows := s.rows[:0] | |
| 291 | pool := make([]*Bar, 0, s.bHeap.Len()) | |
| 294 | ||
| 292 | 295 | for s.bHeap.Len() > 0 { |
| 293 | 296 | var usedRows int |
| 294 | 297 | b := heap.Pop(&s.bHeap).(*Bar) |
| 298 | 301 | continue |
| 299 | 302 | } |
| 300 | 303 | 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) | |
| 303 | 306 | usedRows++ |
| 304 | 307 | } else { |
| 305 | 308 | wg.Add(1) |
| 315 | 318 | if qb, ok := s.queueBars[b]; ok { |
| 316 | 319 | delete(s.queueBars, b) |
| 317 | 320 | qb.priority = b.priority |
| 318 | pool = append(pool, qb) | |
| 321 | s.pool = append(s.pool, qb) | |
| 319 | 322 | drop = true |
| 320 | 323 | } else if s.popCompleted && !b.bs.noPop { |
| 321 | 324 | if frame.shutdown > 1 { |
| 331 | 334 | continue |
| 332 | 335 | } |
| 333 | 336 | } |
| 334 | pool = append(pool, b) | |
| 337 | s.pool = append(s.pool, b) | |
| 335 | 338 | } |
| 336 | 339 | |
| 337 | 340 | for _, b := range pool { |
| 338 | 341 | heap.Push(&s.bHeap, b) |
| 339 | 342 | } |
| 340 | 343 | |
| 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]) | |
| 343 | 347 | if err != nil { |
| 344 | wg.Wait() | |
| 345 | return err | |
| 348 | readRows-- | |
| 346 | 349 | } |
| 347 | 350 | } |
| 348 | 351 | |
| 349 | 352 | 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) | |
| 351 | 356 | } |
| 352 | 357 | |
| 353 | 358 | func (s *pState) newTicker(done <-chan struct{}) chan time.Time { |