perf: Reduce allocations and improve algorithmic efficiency across Chart controls#381
Open
PaulAndersonS wants to merge 1 commit into
Open
perf: Reduce allocations and improve algorithmic efficiency across Chart controls#381PaulAndersonS wants to merge 1 commit into
PaulAndersonS wants to merge 1 commit into
Conversation
…art controls Performance improvements: 1. AxisLabelLayout: Replace Keys.ToArray()[lastIndex] with Keys.ElementAt() to avoid allocating an entire array just to access one element. 2. CategoryAxis.GetLabelContent: Replace string concatenation in loop with StringBuilder to reduce O(n) string allocations to O(1). 3. PolarAreaSegment: Pre-allocate List<float> with known capacity in GenerateInteriorPoints and GenerateStrokePoints to avoid repeated resizing during rendering. 4. BoxAndWhiskerSeries: Replace string concatenation with string interpolation in tooltip text generation. 5. HiLoOpenCloseSeries: Replace string concatenation with string interpolation in tooltip text generation. 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
Multiple performance anti-patterns exist in Chart control rendering and layout paths that cause unnecessary memory allocations and CPU usage:
Description of Change
Five targeted performance optimizations:
AxisLabelLayout.cs — Replace
Keys.ToArray()[lastIndex]withKeys.ElementAt(lastIndex)to avoid allocating an entire array just to access the last key during axis label intersection checks.CategoryAxis.cs — Replace
string + ", " + labelconcatenation in a loop withStringBuilderinGetLabelContent(). This reduces string allocations from O(n) to O(1) when generating grouped category labels.PolarAreaSegment.cs — Pre-allocate
List<float>with known capacity(_pointsCount + 2) * 2in bothGenerateInteriorPoints()andGenerateStrokePoints(). These methods run on every frame during animation, so avoiding List resizing directly reduces GC pressure.BoxAndWhiskerSeries.cs — Replace 5-way string concatenation with string interpolation in tooltip text generation to reduce intermediate string allocations.
HiLoOpenCloseSeries.cs — Replace 4-way string concatenation with string interpolation in tooltip text generation.
Issues Fixed
N/A — Proactive performance improvement
Screenshots
N/A — Internal optimization with no visual changes