Reduce Scatter Graph Memory Allocations #5129
Open
+284
−64
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The Plan
I've been using the scatter graph and noticed that there were quite a few memory allocations (arrays/lists of size N - where N is the number of points) in both the
IScatterSourceand the rendering of theIPlottable.This pull request makes use of the decorator design pattern to create list views that drastically improve the memory performance of scatter graphs, effectively removing any need for garbage collection in the rendering code. Theoretically, the changes should improve the speed at which rendering can take place (given the fact there are less iterations over the points), however, my benchmarks have been all over the place on my laptop so I'm unable to provide evidence of that.
Examples of Changes
How
IScatterSource#GetScatterPointsused to be implemented:How I've changed
IScatterSource#GetScatterPoints:All the list view extension methods can be found in
ScottPlot.DataSources.ReadOnlyListExtensions.Iterations over the data were also removed from
ScottPlot.Plottables.Scatter.Here's an extract from the old implementation:
And here's how I changed it:
Benchmarks
I haven't been able to see improvements from a time perspective despite the notable reduction in iterations over the coordinates - this is either due to the lack of repeatability on my laptop, or the code isn't as fast as I think it is.
However, one thing that is consistent, is the significant reduction in memory footprint for a larger number of points. This approach ends up yielding an O(1) space complexity instead of the previous O(N).
Benchmarks Before Change:
Benchmarks After Change:
Garbage Collection During Rendering (at 10,000 points) Before Change:

Garbage Collection During Rendering (at 10,000 points) After Change:
