Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a greedy and a non-greedy dataflow block with boundedcapacity defined

I have a BatchBlock with BoundedCapacity defined on it

var _batchBlock = new BatchBlock<int>(2, new GroupingDataflowBlockOptions
                                      {BoundedCapacity = 100 });

So if the queue capacity reaches a 100 the block postpones every message received until a spot becomes available. In this case is the batch queue considered greedy or non-greedy ?

like image 521
Varunkumar Manohar Avatar asked Oct 16 '14 01:10

Varunkumar Manohar


1 Answers

The block is greedy, but not because of how it handles the items above 100, but the ones below 2. You can set the greedy value to false (true is the default) and then the block would only actually consume items when there are enough to send a batch, until then they are postponed:

var batchBlock = new BatchBlock<int>(2, new GroupingDataflowBlockOptions
{
    Greedy = false,
    BoundedCapacity = 100.
});

The BatchBlock class operates in either greedy or non-greedy mode. In greedy mode, which is the default, a BatchBlock object accepts every message that it is offered and propagates out an array after it receives the specified count of elements. In non-greedy mode, a BatchBlock object postpones all incoming messages until enough sources have offered messages to the block to form a batch

From Dataflow (Task Parallel Library)

like image 146
i3arnon Avatar answered Oct 22 '22 00:10

i3arnon