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: Queue Moderators

Post Reply
Leodox
Posts: 5
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: 552
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: 5
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: 552
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.

aelbuz
Posts: 12
Joined: Fri Sep 15, 2023 1:56 pm

Re: Memory<T> and Span<T>

Post by aelbuz » Mon Nov 27, 2023 8:38 am

Hi,

It's been 2 years since the first post but are there any progress or development about this feature? It would be great if the charts support Span<T> and Memory<T>, or at least ArraySegment<T>.

Thanks.

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

Re: Memory<T> and Span<T>

Post by ArctionKestutis » Mon Nov 27, 2023 2:43 pm

No, it was not implemented.
What is expected usage /benefit of ArraySegment<T>?
The original question about this topic was about memory optimization. LightningChart did memory optimization in different way - with new 'block series'. See explanation about Optimize memory and GC usage in our article (https://lightningchart.com/blog/choosin ... ne-series/).
Note that SampleDataBlockSeries, LiteLineSeries, LiteFreeformLineSeries or DigitalLineSeries could render only line-segments, not point-symbols (and has less features in general).

aelbuz
Posts: 12
Joined: Fri Sep 15, 2023 1:56 pm

Re: Memory<T> and Span<T>

Post by aelbuz » Tue Nov 28, 2023 9:16 am

Hi Kestutis,

Of course the benefit of ArraySegment<T> is memory optimization, because of the array pooling.
Also, as you mentioned in your post, these memory opitimized series can render only lines, not points.

We need to show points via PointLineSeries and FreeformPointLineSeries in our application, so we want to create our data arrays with array pooling and give to chart as an ArraySegment.

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

Re: Memory<T> and Span<T>

Post by ArctionKestutis » Tue Nov 28, 2023 2:41 pm

You probably could check the answer I gave to your colleague in Portal. The answer also includes library customization service offer (new types of series could be created). If you interested in such service and/or want to discuss more, please write directly to Support at LightningChart.com email.

Post Reply