Does BoundedCapacity include items currently being processed in TPL Dataflow?
Asked Answered
R

1

15

Does the BoundedCapacity limit only includes items in the input queue waiting to be processed or does it also count items being processed at the moment?

Lets take for example this ActionBlock:

var block = new ActionBlock<int>(
    i => Console.WriteLine(i),
    new ExecutionDataflowBlockOptions
    {
        BoundedCapacity = 1000,
        MaxDegreeOfParallelism = 10,
    });

If there are currently 5 items being processed in parallel. Does that mean the input queue can hold 1000 items more on top of these, or just 995?

Reedbuck answered 27/10, 2014 at 18:43 Comment(0)
R
20

Evidently, BoundedCapacity indeed includes the items being processed on top of the items waiting in the input queue. This can be easily demonstrated with an ActionBlock that has the same ExecutionDataflowBlockOptions with an action that never finishes:

var block = new ActionBlock<int>(
    _ => Task.Delay(-1),
    new ExecutionDataflowBlockOptions
    {
        BoundedCapacity = 1000,
        MaxDegreeOfParallelism = 10,
    });

for (int i = 0; i < 1001; i++)
{
    Console.WriteLine("#{0} - InputCount={1}", i, block.InputCount);
    await block.SendAsync(i);
}

The output would be as follows and then the application would block indefinitely:

...
...
#990 - InputCount=980
#991 - InputCount=981
#992 - InputCount=982
#993 - InputCount=983
#994 - InputCount=984
#995 - InputCount=985
#996 - InputCount=986
#997 - InputCount=987
#998 - InputCount=988
#999 - InputCount=989
#1000 - InputCount=990

That's because 1000 items were added, 10 of them (MaxDegreeOfParallelism) are being processed concurrently, the other 990 are waiting in the input queue and the 1001st item could never get in.

Reedbuck answered 27/10, 2014 at 18:43 Comment(2)
So BoundedCapacity is an upper bound for the max DOP as well.Leitao
@Leitao Exactly. This is how I got to this question.Reedbuck

© 2022 - 2024 — McMap. All rights reserved.