Pipelines, multiplexing, and unbounded buffering

后端 未结 4 1522
猫巷女王i
猫巷女王i 2020-12-10 07:19

(NOTE: I\'m using .Net 4, not .Net 4.5, so I cannot use the TPL\'s DataflowBlock classes.)

TL;DR Version

Ultimately, I\'m just look

4条回答
  •  眼角桃花
    2020-12-10 07:40

    Create a pool of items at startup, 1000, say. Store them on a BlockingCollection - a 'pool queue'.

    The supplier gets items from the pool queue, loads them from the file, loads in the sequence-number/whatever and submits them to the processors threadpool.

    The processors do their stuff and sends the output to the multiplexer. The multiplexer does it job of storing any out-of-order items until earlier items have been processed.

    When an item has been completely consumed by whatever the multiplexer outputs to, they are returned to the pool queue for re-use by the supplier.

    If one 'slow item' does require enormous amounts of processing, the out-of-order collection in the multiplexer will grow as the 'quick items' slip through on the other pool threads, but because the multiplexer is not actually feeding its items to its output, the pool queue is not being replenished.

    When the pool empties, the supplier will block on it and will be unable to supply any more items.

    The 'quick items' remaining on the processing pool input will get processed and then processing will stop except for the 'slow item'. The supplier is blocked, the multiplexer has [poolSize-1] items in its collection. No extra memory is being used, no CPU is being wasted, the only thing happening is the processing of the 'slow item'.

    When the 'slow item' is finally done, it gets output to the multiplexer.

    The multiplexer can now output all [poolSize] items in the required sequential order. As these items are consumed, the pool gets filled up again and the supplier, now able to get items from the pool, runs on, again reading its file an queueing up items to the processor pool.

    Auto-regulating, no bounded buffers required, no memory runaway.

    Edit: I meant 'no bounded buffers required' :)

    Also, no GC holdups - since the items are re-used, they don't need GC'ing.

提交回复
热议问题