Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between DataflowBlockOptions.BoundedCapacity and BufferBlock<T>

Let's assume i have a simple ActionBlock<int>

var actionBlock = new ActionBlock<int>(_ => Console.WriteLine(_));

I can specify a bounded capacity to enable buffering:

var actionBlock = new ActionBlock<int>(
    _ => Console.WriteLine(_),
    new ExecutionDataflowBlockOptions
    { 
        BoundedCapacity = 1000
    });

Is it better to create a BufferBlock<T> and link it to the actionBlock, is it the same, Or is it redundant?

like image 856
i3arnon Avatar asked Jan 02 '14 16:01

i3arnon


1 Answers

If you just added the bounded BufferBlock before your ActionBlock, then that wouldn't work correctly, because the ActionBlock would still be unbounded. So, items would keep accumulating in the ActionBlock's input queue, achieving nothing.

If you added the bounded BufferBlock and also set BoundedCapacity of the ActionBlock to 1, then that would work (plus minus 1).

Doing it this way doesn't give you much (except adding some small overhead), so generally speaking, you should just set BoundedCapacity of the ActionBlock. But there might be some cases where the combination of a bounded BufferBlock and an ActionBlock bounded to 1 might make sense. For example, when you want to set the capacity only after creating the ActionBlock.

like image 174
svick Avatar answered Oct 13 '22 15:10

svick