Search code examples
c#task-parallel-libraryclonetpl-dataflow

What does Microsoft refer to when it states "cloning" messages in TPL Dataflow BroadcastBlock<T>?


I am referring to the following link in MSDN : Broadcast Block

It states that messages to linked-to targets are "cloned".

I like to know what type of clone MS is referring to here, a deep clone? How does it exactly affect memory consumption?


Solution

  • When you create a BroadcastBlock<T> you define the cloning function and are therefore completly in charge of how it works. If you want to pass by reference then don't do anything to your object in the cloning function:

    new BroadcastBlock<T>(x => x, options)

    If you want a deep copy you have to do this yourself. The BroadcastBlock knows nothing about the data it is handling. It only knows that it drops messages when full and invokes your function when it passes data down the pipeline.

    Also, the BroadcastBlock allows you to pass null for the cloningFunction this effectively lets you pass the data without manipulation.

    private TOutput CloneItem(TOutput item)
    {
        return _cloningFunction != null ?
            _cloningFunction(item) :
            item;
    } 
    

    Source

    Example