简体   繁体   中英

TPL Dataflow data recycling

I am working on an audio processing tool that I would like to build using TPL Dataflow. The Data flow itself will consist of audio samples being passed between sound processing blocks. Those samples will typically be a few Kb in size ( float[] / byte[] with 1k to 4k elements). The data flow is thus a simple pipeline which looks like this:

SourceBlock<byte[]> 
    -> TransformBlock<byte[], float[]> 
        -> TransformBlock<float[], float[]>
            -> ...

Some of the blocks can work purely "in place", ie by mutating the input data, while other have to create new samples. The processing time of each block can vary depending on the data in input.

I don't want to allocate new arrays all the time and rely on the garbage collector to take care of object recycling. I want to benefit from concurrent execution of the blocks and thus don't want to restrict the chain to process data sequentially (in which case I wouldn't need TPL anyway). I don't need processing blocks to run concurrent processing (I am fine with at most one process per block at any given time).

What would be the best scheme to control the number of samples in the pipeline at a given time and recycle samples/arrays no longer used ?

If your goal is to reuse your arrays instead of always creating new ones and having the GC collect them you need to use an ObjectPool :

The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished, it returns the object to the pool rather than destroying it; this can be done manually or automatically.

Unfortunately you would probably need to implement that yourself and make it thread-safe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM