Memory<T> and Span<T>

Need a new feature to LightningChart Ultimate? Post it here and perhaps it will be implemented in the next version...

Moderator: Arction_LasseP

Leodox
Posts: 3
Joined: Sat Apr 16, 2016 6:03 am

Memory<T> and Span<T>

Post by Leodox » Thu Sep 30, 2021 10:20 pm

Hello,

Is there any plan to make the charts support Memory<T> and Span<T> structures? These are extremely useful for optimizing memory usage, for example, by using ArrayPool<T>. Since the ArrayPool might not give you a buffer of the exact size requested, you can wrap that in a Span or Memory structure.

I have a monitor application that updates SampleDataSeries rapidly using data from a remote source. The data acquisition tasks use the ArrayPool<T> for memory efficiency and to minimize new memory allocations and garbage collection. Unfortunately, right when I add the samples to the series, it has to be moved to a different buffer or use the .ToArray() method, which potentially causes more allocations. It would be great if LightningCharts accepted these structure directly.

For example (Pseudo code):

Code: Select all

var pool = new ArrayPool<float>.Shared;
var buf = pool.Rent(5000); //buf might not be exactly length 5000 (it's probably 8192)
var mem = new Memory<float>(buf, 5000); //Wrap it with a Memory<T> of length 5000.

for (var i=0; i < mem.Length; i++){
     //Populate buffer with some data
     mem[i] = i;
}

//Plot the 5000 samples
_chart.ViewXY.SampleDataSeries[0].AddSamples(mem.toArray(), true);//This is inefficient and causes allocations
_chart.ViewXY.SampleDataSeries[0].AddSamples(mem, true); //This would be better!


Thanks!
Jon

ArctionKestutis
Posts: 421
Joined: Mon Mar 14, 2016 9:22 am

Re: Memory<T> and Span<T>

Post by ArctionKestutis » Fri Oct 01, 2021 8:40 am

Hello,

Thank you for the idea. It is interesting and we will consider it in the future.
However, there are other ways to improve Charting application. Please check new feature of LightningChart .NET - SampleDataBlockSeries.
In LightningChart® .NET v.10.1.1 a new, super-fast, line series was introduced, called SampleDataBlockSeries. The data is stored as memory blocks, which disposes old data and appends new data, easier on memory and CPU.
When compared to SampleDataSeries, SampleDataBlockSeries is far more superior. SampleDataBlockSeries has 60 times better FPS and 160 times smoother scrolling/sweeping (as measured by interval's standard deviation). The full report about this internal comparison could be found here. Check also a quick video overview.
Even more, SampleDataBlockSeries could be 1000's time faster and smoother then any competitors solution for most demanding data point count. [the report will come shortly]

All the best,
Kestutis

Leodox
Posts: 3
Joined: Sat Apr 16, 2016 6:03 am

Re: Memory<T> and Span<T>

Post by Leodox » Fri Oct 01, 2021 4:04 pm

Thanks for the reply! Yes, I am in the process of moving my application to use the new SampleDataBlockSeries. I am very interested to see the performance difference, as this seems to be exactly what my application needs.

The Memory<T> and Span<T> request is more geared toward making LightningCharts more compatible with external API's. Many new libraries are using memory pooling and pipelines (PipeReader.TryRead(ReadResult)) which utilize the Memory<T>, Span<T>, ReadOnlySequence<T>, and allow for taking Slices, without needing to allocate more memory and perform copies. Maintaining this process all the way through to the graph will help make the application more efficient as a whole, and make it easier on the coder. :)

Thanks,
Jon

ArctionKestutis
Posts: 421
Joined: Mon Mar 14, 2016 9:22 am

Re: Memory<T> and Span<T>

Post by ArctionKestutis » Mon Oct 04, 2021 2:50 pm

The buffering design for SampleDataBlockSeries probably prevents such implementation. However, Memory<T> and Span<T> support could be implemented for other series. We could look to those options, if SampleDataBlockSeries does not satisfy your needs.

All the best.