Perf: Hoist ToList() out of loops in CartesianChartArea.UpdateSBS and GetTotalWidth#373
Open
PaulAndersonS wants to merge 1 commit into
Open
Perf: Hoist ToList() out of loops in CartesianChartArea.UpdateSBS and GetTotalWidth#373PaulAndersonS wants to merge 1 commit into
PaulAndersonS wants to merge 1 commit into
Conversation
In UpdateSBS() and GetTotalWidth(), SideBySideSeriesPosition.Values.ToList() was called inside a for-loop, creating a new List<> allocation on every iteration. This is O(n) allocations where only 1 is needed. Hoisted the ToList() call before both loops so the dictionary values are materialized once and reused. Also adds a unit test for UpdateSBS with multiple series groups. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Root Cause of the Issue
In
CartesianChartArea.cs, bothUpdateSBS()andGetTotalWidth()calledSideBySideSeriesPosition.Values.ToList()[i]inside a for-loop. This creates a brand newList<List<CartesianSeries>>on every iteration just to index into it — resulting in O(n) unnecessary heap allocations where only 1 is needed.For charts with many series groups, this wastes memory and puts pressure on the garbage collector during layout calculations.
Description of Change
Hoisted the
.Values.ToList()call before the loop in both methods, materializing the dictionary values once and reusing the cached list for indexed access.Before (called N times):
After (called once):
Also added a unit test
UpdateSBS_WithMultipleSeriesGroups_ShouldComputeSbsInfoto verify correct behavior with multiple series groups.Issues Fixed
Performance improvement — no specific issue.
Screenshots
N/A — internal performance optimization with no visual change.